我希望有人能帮上忙。我想知道使用when语句的正确语法是什么?我有这样一本手册:
- 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'})但是如果我在when语句中删除item.id中的花括号
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) btw。谢谢
发布于 2017-03-08 22:33:28
“正确”的语法是不包含jinja模板,如警告所示。否则,您的条件将无法工作,因为类型不兼容。
您可以尝试类型强制:
when: ' item.id|string not in sh_vlan_id'请参阅:http://jinja.pocoo.org/docs/dev/templates/#list-of-builtin-filters
发布于 2021-01-07 04:19:24
另一个例子供参考..
- name: Sudoers | update sudoers file and validate
lineinfile: "dest=/etc/sudoers
insertafter=EOF
line='{{ item.username }} ALL=(ALL) NOPASSWD: ALL'
regexp='^{{ item.username }} .*'
state=present"
when: '{{ item.use_sudo }} == True'
with_items: '{{users}}'我都快不行了。
[WARNING]: conditional statements should not include jinja2 templating delimiters such as {{ }} or {% %}. Found: "{{ item.use_sudo}}" == True在对攻略做了以下更改之后,它工作得很好。
- name: Sudoers | update sudoers file and validate
lineinfile: "dest=/etc/sudoers
insertafter=EOF
line='{{ item.username }} ALL=(ALL) NOPASSWD: ALL'
regexp='^{{ item.username }} .*'
state=present"
when: 'item.use_sudo|bool'
with_items: '{{users}}'更新:如果使用== True不需要的变量类型boolean,则为
https://stackoverflow.com/questions/42673045
复制相似问题