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
的实例. reports
& 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:
致命:[localhost]:失败! => {"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屋!