ansible - when 语句不应包含 jinja2 模板分隔符 [英] ansible - when statements should not include jinja2 templating delimiters

查看:36
本文介绍了ansible - when 语句不应包含 jinja2 模板分隔符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望有人可以提供帮助.我想知道使用 when 语句的正确语法是什么?我有这个剧本:

- set_fact:sh_vlan_id: "{{ output.response|map(attribute='vlan_id')|list|join(',') }}"- 名称:创建 vlanios_config:提供者:{{ 提供者}}"父母:vlan {{ item.id }}行:名称{{ item.name }}with_items: "{{ vlans }}"注册:结果当:'"{{ item.id }}" 不在 sh_vlan_id'

并且在运行期间它给了我一个警告,但它实际上接受了它.我不确定这是否可以.

TASK [set_fact] **********************************************************************************************************************************好的:[acc_sw_01]任务 [创建 vlans] *************************************************************************************************************************[警告]:when 语句不应包含 jinja2 模板分隔符,例如 {{ }} 或 {% %}.发现:{{ item.id }}"不在 sh_vlan_id 中跳过:[acc_sw_01] =>(item={u'id': 10, u'name': u'voice-1'})跳过:[acc_sw_01] =>(item={u'id': 101, u'name': u'data-2'})跳过:[acc_sw_01] =>(item={u'id': 100, u'name': u'data-1'})更改:[acc_sw_01] =>(item={u'id': 11, u'name': u'voice-2'})

但是如果我在 when 语句中删除 item.id 中的花括号

when: item.id 不在 sh_vlan_id 中

它给了我一个错误:

TASK [set_fact] **********************************************************************************************************************************好的:[acc_sw_01]任务 [创建 vlans] *************************************************************************************************************************致命:[acc_sw_01]:失败!=>{"failed": true, "msg": "条件检查 'item.id not in sh_vlan_id' 失败.错误是:发生意外模板类型错误 ({% if item.id not in sh_vlan_id %} True {%else %} False {% endif %}): coercing to Unicode: need string or buffer, int found\n\n错误似乎在'/ansible/cisco-ansible/config_tasks/vlan.yml': line 16,第 3 列,但可能\n位于文件中的其他位置,具体取决于确切的语法问题.\n\n有问题的行似乎是:\n\n\n- 名称:创建 vlans\n ^ here\n"}

顺便说一句,我正在使用 ansible 2.3.0(开发 cbedc4a12a).谢谢

解决方案

正确"的语法是不包括警告所指示的 jinja 模板.否则您的条件不起作用,因为类型不兼容.

你可以试试强制类型:

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

<块引用>

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

I'm hoping someone could help. I was wondering what is the correct syntax in using the when statement? I have this playbook:

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

and during run it gives me a warning but it actually accept it. I not sure if this is ok or not.

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

But if I remove the curly braces in item.id in when statement

when: item.id not in sh_vlan_id

It gives me an error:

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

I'm using ansible 2.3.0 (devel cbedc4a12a) btw. Thanks

解决方案

The "correct" syntax is to not include jinja templates as indicated by the warning. Your condition doesn't work otherwise because the types are not compatible.

You could try type coercion:

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

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

这篇关于ansible - when 语句不应包含 jinja2 模板分隔符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆