Ansible 2.0 升级 - 默认过滤器链错误 [英] Ansible 2.0 upgrade - default filter chain error
问题描述
在 Ansible 2.0 之前,允许使用默认过滤器
Prior to Ansible 2.0, default filters were allowed
"{{ oracle1.instance.reports|d().forecast|d().email|d('testing@gmail.com') }}"
其中 |d()
将允许变量(例如 reports
或 forecast
)在末尾默认为默认变量(在这种情况下,默认变量是 testing@gmail.com
)如果程序找不到 reports
或 forecast
的实例.报告
&forecasts
在某些环境中定义,但不是在所有环境中定义,因此我无法从脚本行中删除这些变量.在 Ansible 2.X 中,默认过滤器 |d()
不是必需的,代码可以这样编写:
where |d()
would allow a variable (such as reports
or forecast
) to be defaulted to the default variable at the end (in this case, the default variable is testing@gmail.com
) if the program couldn't find an instance for reports
or forecast
. reports
& forecasts
are defined in some environments, but not in all, so I cannot remove these variables from the script line. In Ansible 2.X, the default filter |d()
is not necessary and the code can be written like this:
"{{ oracle1.instance.reports.forecast.email|d('testing@gmail.com') }}"
运行上面的脚本时,我收到此错误:
When running the script above, I am getting this error:
致命:[本地主机]:失败!=> {"failed": true, "msg": "字段 'args' 有一个无效值,它似乎包含一个未定义的变量.错误是:'dict object' 没有属性 'reports'\n\n错误似乎在 '/home/ansible/svn/stable-1.6-ansible2_other/playbooks/buildEnvironment/temp2.yml':第 21 行,第 7 列,但可能\n在文件中的其他地方,具体取决于确切的语法问题.\n\n违规行似乎是:\n\n ignore_errors: false\n - 调试:\n ^ here\n"}
fatal: [localhost]: FAILED! => {"failed": true, "msg": "the field 'args' has an invalid value, which appears to include a variable that is undefined. The error was: 'dict object' has no attribute 'reports'\n\nThe error appears to have been in '/home/ansible/svn/stable-1.6-ansible2_other/playbooks/buildEnvironment/temp2.yml': line 21, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n ignore_errors: false\n - debug:\n ^ here\n"}
任何有关此问题或如何为 Ansible 2.X 使用默认变量过滤器的帮助将不胜感激!
Any help on this matter or how to use default variable filters for Ansible 2.X would be much appreciated!
推荐答案
我是这样做的:
"{{ ((((oracle1 | default({})).instance | default({})).reports | default({})).forecast | default({})).email | default('testing@gmail.com') }}"
这篇关于Ansible 2.0 升级 - 默认过滤器链错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!