0
votes

Prior to Ansible 2.0, default filters were allowed

"{{ oracle1.instance.reports|d().forecast|d().email|d('[email protected]') }}"

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 [email protected]) 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('[email protected]') }}"

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
"Prior to Ansible 2.0, default filters were allowed" -- really? What version does your template resolve successfully on? I just tried with 1.9.5 and it fails with template error while templating string: expected token 'end of print statement', got '.' - techraf

1 Answers

0
votes

I do it like this:

"{{ ((((oracle1 | default({})).instance | default({})).reports | default({})).forecast | default({})).email | default('[email protected]') }}"