防止Ansible在传递WITH_ITEMS时解析字符串 [英] Prevent ansible from parsing strings when passed through with_items

查看:31
本文介绍了防止Ansible在传递WITH_ITEMS时解析字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用with_items指定要传递给自定义Ansible模块的键/值对列表。

当键或值字符串具有类似列表的格式时,会出现问题。例如,"[('a', 'b'), ('c', 'd')]"。在这种情况下,with_items可能会将字符串转换为列表,并破坏我的配置。

要再现的最小示例(我使用的是debug模块,但与自定义模块的行为相同):

- name: with_items_test
  debug: 
    msg: "{{ item.value }}"
  with_items:
    - { value: "[('a', 'b'), ('c', 'd')]" }
TASK [with_items_test] *********************************************************
ok: [localhost] => (item={u'value': u"[('a', 'b'), ('c', 'd')]"}) => {
    "item": {
        "value": "[('a', 'b'), ('c', 'd')]"
    }, 
    "msg": [
        [
            "a", 
            "b"
        ], 
        [
            "c", 
            "d"
        ]
    ]
}

如果没有with_items,这样的字符串可以很好地传递:

- name: with_items_test
  debug: 
    msg: "[('a', 'b'), ('c', 'd')]"
TASK [with_items_test] *********************************************************
ok: [localhost] => {
    "msg": "[('a', 'b'), ('c', 'd')]"
}

推荐答案

这不是关于with_items如何工作,而是关于可选模板引擎。
使用string过滤阻止转换。有关详细信息,请参阅我的other答案。

- name: with_items_test
  debug: 
    msg: "{{ item.value | string }}"
  with_items:
    - { value: "[('a', 'b'), ('c', 'd')]" }

这篇关于防止Ansible在传递WITH_ITEMS时解析字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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