ansible - when语句不应包含jinja2模板分隔符

时间:2017-03-08 13:46:28

标签: ansible

我希望有人可以提供帮助。我想知道使用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'})

但是如果我在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)btw。感谢

2 个答案:

答案 0 :(得分:17)

“正确”语法不包括警告所指示的jinja模板。由于类型不兼容,您的情况不起作用。

您可以尝试输入强制:

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

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

答案 1 :(得分:3)

另一个参考例子..

  - 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}}'

UPD:如果您使用boolean不需要的变量类型== True