Impossible - if the statements should not contain restrictions on jinja2 templating delimiters

I hope someone can help. I was wondering what is the correct syntax for using the when statement? I have this notebook:

 - 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 at launch, he gives me a warning, but actually accepts it. I'm not sure if this is normal 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 

This 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 am using ansible 2.3.0 (devel cbedc4a12a) by the way. Thanks

+5
source share
1 answer

The β€œcorrect” syntax is to not include jinja patterns as indicated in the warning. Your condition does not work otherwise, because the types are incompatible.

You can try the type of coercion:

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

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

+6
source

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


All Articles