Ansible 2: cannot use undef vars in templates

In ansible 1.9, I have some roles in which I can use undefined variables (error_on_undefined_vars = False at ansible.cfg) in templates without problems on the way:

template.yml:

{{ var1 }}{{ var2 }}{{ var3 }}

If any of these vars is not defined, then nothing is replaced. Thus, you can simply indicate in your notebook some of these vars, and not others, as desired.

But after updating to version 2.2.0.0, I found that if any of these vars is not defined, they do not replace any of the template vars, but the given template: {{var1}} {{var2}} {{var3} }

eg:.

Playbook:

- hosts: myhost
   vars:
     var1=1
     var3=3
   roles:
     - myrole

tasks:

- name: copy template
  become: true
  template: src=test.j2 dest=/tmp/test owner=user group=user

After starting this play, the final / tmp / test run with 1.9 available will be

13

and with available 2.2.0.0

{{ var1 }}{{ var2 }}{{ var3 }}

Thus, no varna is replaced.

But if:

Playbook:

- hosts: myhost
   vars:
     var1=1
     var2=2
     var3=3
   roles:
     - myrole

/tmp/ 1.9/2.2.0.0

123

- ?

+6
2

, , - , . , , . :

{% if var1 is defined and var2 is defined and var3 is defined %}
        {{ var1 }}{{ var2 }}{{ var3 }}
{% endif %}

, 123 . undefined, .

+1

Jinja, , .

default, ,

{{ var1 }}{{ var2 | default(None) }}{{ var3 }}

var2 "", var 2 . , , , .

omit Jinja, .

{{ var1 }}{{ var2 | default(omit) }}{{ var3 }}

Jinja . https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html

+1

Source: https://habr.com/ru/post/1670519/


All Articles