2

I'm trying to use with_items to specify a list of key/value pairs to be passed to a custom ansible module.

Problem arises when key or value string has list-like format. E.g., "[('a', 'b'), ('c', 'd')]". In such a case with_items presumably converts a string to a list and wrecks a havoc on my configuration.

Minimal example to reproduce (I'm using debug module, but with custom module the behavior is the same):

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

Without with_items such a string is passed just fine:

- name: with_items_test
  debug: 
    msg: "[('a', 'b'), ('c', 'd')]"
TASK [with_items_test] *********************************************************
ok: [localhost] => {
    "msg": "[('a', 'b'), ('c', 'd')]"
}
β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
Boris
  • 23
  • 3

1 Answers1

3

This is not about how with_items work, but about Ansible template engine.
Use string filter to prevent conversion. See my other answer for details.

- name: with_items_test
  debug: 
    msg: "{{ item.value | string }}"
  with_items:
    - { value: "[('a', 'b'), ('c', 'd')]" }
β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
Konstantin Suvorov
  • 65,183
  • 9
  • 162
  • 193