I tried to use omit with an expression like this:
id: "{{ openstack_networks.id | default(omit) }}"
But it seems that it keeps failing with an exception when openstack_networks
variable is not defined.
What is the correct way to write this jinja2 filter?
I want to omit the parameter in case openstack_networks.id does not exists.
Defined by the variable in use. If we leave one of both empty, Ansible will see those empty as defined but “None” (Python null) as content. With the omit filter we can remove the parameter from the play, so if the parameter is empty it won't be used.
With the pipe character you pass a value to a filter. There are numerous Jinja 2 filters but Ansible brings some additional filters.
So if you want to define a default value for a variable you should set it in role/defaults/main.
Interestingly enough, Ansible will take something that reads like plain English:
id: "{{ omit if openstack_networks.id is not defined or openstack_networks.id }}"
The benefit here is that there are no additional parentheses.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With