Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use omit with Ansible and avoid any errors?

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.

like image 247
sorin Avatar asked Jan 18 '17 17:01

sorin


People also ask

What is omit in Ansible?

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.

What does pipe mean in Ansible?

With the pipe character you pass a value to a filter. There are numerous Jinja 2 filters but Ansible brings some additional filters.

Which filter is used to provide default values to variables Ansible?

So if you want to define a default value for a variable you should set it in role/defaults/main.


1 Answers

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.

like image 181
Kuba hasn't forgotten Monica Avatar answered Sep 28 '22 08:09

Kuba hasn't forgotten Monica