我希望有人可以提供帮助.我想知道使用该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'
Run Code Online (Sandbox Code Playgroud)
并且在运行期间它给了我一个警告,但它实际上接受了它.我不确定这是否合适.
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'})
Run Code Online (Sandbox Code Playgroud)
但是如果我item.id
在when
声明中删除花括号
when: item.id not in sh_vlan_id
Run Code Online (Sandbox Code Playgroud)
它给了我一个错误:
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"}
Run Code Online (Sandbox Code Playgroud)
我正在使用ansible 2.3.0(devel cbedc4a12a)顺便说一句.谢谢
小智 18
"正确"语法不包括警告所指示的jinja模板.您的情况不起作用,因为类型不兼容.
您可以尝试输入强制:
when: ' item.id|string not in sh_vlan_id'
Run Code Online (Sandbox Code Playgroud)
请参阅:http://jinja.pocoo.org/docs/dev/templates/#list-of-builtin-filters
另一个例子供参考..
- 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}}'
Run Code Online (Sandbox Code Playgroud)
我已经到了凌晨。
[WARNING]: conditional statements should not include jinja2 templating delimiters such as {{ }} or {% %}. Found: "{{ item.use_sudo}}" == True
Run Code Online (Sandbox Code Playgroud)
在对剧本进行以下更改后,它工作正常。
- 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}}'
Run Code Online (Sandbox Code Playgroud)
UPD:如果您使用boolean
不需要== True
的变量类型
归档时间: |
|
查看次数: |
13193 次 |
最近记录: |