gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 13:31:38 25 4
gpt4 key购买 nike

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

最佳答案

如警告所示,“正确”语法不包括Jinja模板。否则您的条件将不起作用,因为类型不兼容。

您可以尝试输入强制:

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

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

关于ansible - ansible-语句不应包含jinja2模板分隔符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42673045/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com