How to use omit with Ansible and avoid any errors?

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.


Not super elegant, but 100% working solution to handle keys of possibly undefined parent dicts:

id: "{{ (openstack_networks | default({})).id | default(omit) }}"

This will give you omit if openstack_networks is defined but has no id key or if openstack_networks is undefined.