4
我希望有人可以提供幫助。我想知道在使用when
聲明時,什麼是正確的語法?我有這個劇本:ansible - 當語句不應該包含jinja2模板分隔符
- 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'
在運行期間,它給了我一個警告,但它實際上接受它。我不確定這是否正常。
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'})
但如果我刪除item.id
花括號中when
聲明
when: item.id not in sh_vlan_id
它給了我一個錯誤:
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"}
我使用ansible 2.3.0(devel的cbedc4a12a )順便說一句。謝謝
您好,感謝快速回復,這完美的作品 – reynold
請記住,這種方法不會更換工作'{{item.id}}是defined'。 – spydon