Ansible 2.0 upgrade - default filter chain error

2019-09-15 11:25发布

Prior to Ansible 2.0, default filters were allowed

"{{ oracle1.instance.reports|d().forecast|d().email|d('testing@gmail.com') }}"

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:

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"}

Any help on this matter or how to use default variable filters for Ansible 2.X would be much appreciated!

1条回答
我只想做你的唯一
2楼-- · 2019-09-15 11:50

I do it like this:

"{{ ((((oracle1 | default({})).instance | default({})).reports | default({})).forecast | default({})).email | default('testing@gmail.com') }}"
查看更多
登录 后发表回答