我尝试使用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')]" }
答案 0 :(得分:1)
这不是关于with_items
如何工作,而是关于Ansible模板引擎
使用string
过滤器来阻止转换。有关详细信息,请参阅我的other答案。
- name: with_items_test
debug: msg="{{ item.value | string }}"
with_items:
- { value: "[('a', 'b'), ('c', 'd')]" }