角色的默认变量中的Ansible递归循环

Mar*_*zer 4 variables ansible

如何在不引起递归循环的情况下在Ansible(2.1.x)中的其他变量中重用变量?

设定

考虑以下roles/<role>/defaults/main.yml文件:

---
liquibase:
  version: "3.5.3"
  download_file: "liquibase-{{liquibase.version}}-bin.tar.gz"

  # I also tried this alternative with a similar result:
  # download_file: "liquibase-{{liquibase[version]}}-bin.tar.gz
...
Run Code Online (Sandbox Code Playgroud)

和这个roles/<role>/tasks/main.yml文件:

---
- name: Liquibase | Download
  debug:
    msg: "download_file: {{liquibase.download_file}}"
...
Run Code Online (Sandbox Code Playgroud)

错误

我希望变量liquibase.download_file具有值,liquibase-3.5.3-bin.tar.gz但是当我使用此角色运行剧本时,出现以下错误:

...
TASK [liquibase : Liquibase | Download] *******************************************
fatal: [localhost]: FAILED! => {"failed": true, "msg": "...: recursive loop detected in template string: liquibase-{{liquibase.version}}-bin.tar.gz"}
...
Run Code Online (Sandbox Code Playgroud)

我的用例

显然,我要下载Liquibase,并且要让角色的用户决定要使用哪个版本。我还想提供完全覆盖下载位置(文件,URL等)的可能性,例如,使用公司的FTP服务器或类似服务器。

Kon*_*rov 5

不支持在同一父字典中引用其他字典键。看到这个问题

你只能重构你的变量,使versiondownload_file不同变量的树木,如:

liquibase_version: "3.5.3"
liquibase_download_file: "liquibase-{{liquibase_version}}-bin.tar.gz"
Run Code Online (Sandbox Code Playgroud)

PS,如果这是您角色的默认设置,则将其liquibase_version分成独立变量会更有意义。这样,用户将只能重新定义liquibase_versionliquibase_download_file将得到改变; 而在使用字典的情况下(如您的问题),您不能仅覆盖一个键,而用户必须使用versiondownload_file键设置完整的字典。