Question:
Is it possible to set a fact containing a list in Ansible using set_fact
? What’s the correct syntax for it?
Answer:
Indeed it is. You need to quote the entire list though:
1 2 3 4 5 6 7 |
- name: set fact set_fact: foo="[ 'one', 'two', 'three' ]" - name: debug debug: msg={{ item }} with_items: foo |
The above tasks should generate the following output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
TASK: [set fact] ************************************************************** ok: [localhost] TASK: [debug] ***************************************************************** ok: [localhost] => (item=one) => { "item": "one", "msg": "one" } ok: [localhost] => (item=two) => { "item": "two", "msg": "two" } ok: [localhost] => (item=three) => { "item": "three", "msg": "three" } |