I am having an issue with the default value not being honored for an undefined dictionary. For example, if the setup is as follows
- name: something
file:
path: /somepath
owner: “{{ dir[‘owner’] | default(‘root’) }}”
and I have a variable of
dir:
owner: someowner
when I run this, a file called /somepath is created with the owner someowner, as expected. However, if the dictionary dir does not exist, I get an error of
"the field ‘args’ has an invalid value, which appears to include a variable that is undefined. The error was: ‘dir’ is undefined
instead of running with the default as I would have expected. if I flatten the variable structure to be
dir_owner: someowner
and change the task to be
- name: something
file:
path: /somepath
owner: “{{ dir_owner | default(‘root’) }}”
this works as expected. The owner of the file is either someowner if defined or root if not.