Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ansible - when statements should not include jinja2 templating delimiters

Tags:

ansible

I'm hoping someone could help. I was wondering what is the correct syntax in using the when statement? I have this playbook:

- set_fact:    sh_vlan_id: "{{ output.response|map(attribute='vlan_id')|list|join(',') }}"  - name: create vlans   ios_config:     provider: "{{ provider }}"     parents: vlan {{ item.id }}     lines: name {{ item.name }}     with_items: "{{ vlans }}"   register: result   when: '"{{ item.id }}" not in sh_vlan_id' 

and during run it gives me a warning but it actually accept it. I not sure if this is ok or not.

TASK [set_fact]    ************************************************************************************************************************* ok: [acc_sw_01]  TASK [create vlans] ********************************************************************************************************************* [WARNING]: when statements should not include jinja2 templating delimiters such as {{ }} or {% %}. Found: "{{ item.id }}" not in sh_vlan_id  skipping: [acc_sw_01] => (item={u'id': 10, u'name': u'voice-1'})  skipping: [acc_sw_01] => (item={u'id': 101, u'name': u'data-2'})  skipping: [acc_sw_01] => (item={u'id': 100, u'name': u'data-1'})  changed: [acc_sw_01] => (item={u'id': 11, u'name': u'voice-2'}) 

But if I remove the curly braces in item.id in when statement

when: item.id not in sh_vlan_id 

It gives me an error:

TASK [set_fact] ************************************************************************************************************************* ok: [acc_sw_01]  TASK [create vlans] ********************************************************************************************************************* fatal: [acc_sw_01]: FAILED! => {"failed": true, "msg": "The conditional check 'item.id not in sh_vlan_id' failed. The error was: Unexpected templating type error occurred on ({% if item.id not in sh_vlan_id %} True {% else %} False {% endif %}): coercing to Unicode: need string or buffer, int found\n\nThe error appears to have been in '/ansible/cisco-ansible/config_tasks/vlan.yml': line 16, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: create vlans\n  ^ here\n"} 

I'm using ansible 2.3.0 (devel cbedc4a12a) btw. Thanks

like image 432
reynold Avatar asked Mar 08 '17 13:03

reynold


People also ask

Does Ansible use Jinja2 template?

Ansible uses Jinja2 templating to enable dynamic expressions and access to variables and facts. You can use templating with the template module.

What is the main advantage purpose of using Jinja2 template within Ansible?

Jinja2 templates are simple template files that store variables that can change from time to time. When Playbooks are executed, these variables get replaced by actual values defined in Ansible Playbooks. This way, templating offers an efficient and flexible solution to create or alter configuration file with ease.


1 Answers

The "correct" syntax is to not include jinja templates as indicated by the warning. Your condition doesn't work otherwise because the types are not compatible.

You could try type coercion:

when: ' item.id|string not in sh_vlan_id' 

See: http://jinja.pocoo.org/docs/dev/templates/#list-of-builtin-filters

like image 137
user7466858 Avatar answered Oct 06 '22 17:10

user7466858