Is it possible to set a fact containing a list in Ansible using set_fact
? What's the correct syntax for it?
This module allows setting new variables. Variables are set on a host-by-host basis just like facts discovered by the setup module. These variables will be available to subsequent plays during an ansible-playbook run. Set cacheable to yes to save variables across executions using a fact cache.
Assigning a value to a variable in a playbook is quite easy and straightforward. Begin by calling the vars keyword then call the variable name followed by the value as shown. In the playbook above, the variable name is salutations and the value is Hello world!
Ansible facts are data gathered about target nodes (host nodes to be configured) and returned back to controller nodes. Ansible facts are stored in JSON format and are used to make important decisions about tasks based on their statistics. Facts are in an ansible_facts variable, which is managed by Ansible Engine.
Yes, this is possible. As mentioned in another answer, you can set an array using double quotes, like so:
- name: set foo fact to a list set_fact: foo: "[ 'one', 'two', 'three' ]"
However, I thought I'd create another answer to indicate that it's also possible to add to an existing array, like so:
- name: add items to foo list fact set_fact: foo: "{{foo}} + [ 'four' ]"
Combining these and adding debugging as a playbook (which I'm calling facts.yml
) like so:
--- - name: test playbook gather_facts: false hosts: localhost tasks: - name: set foo fact to an array set_fact: foo: "[ 'one', 'two', 'three' ]" - debug: var: foo - name: add items to foo array fact set_fact: foo: "{{foo}} + [ 'four' ]" - debug: var: foo
Produces (via ansible-playbook facts.yml
) the following:
PLAY [test playbook] ******************************************************** TASK: [set foo fact to an array] ******************************************** ok: [localhost] TASK: [debug var=foo] ******************************************************* ok: [localhost] => { "foo": [ "one", "two", "three" ] } TASK: [add items to foo array fact] ***************************************** ok: [localhost] TASK: [debug var=foo] ******************************************************* ok: [localhost] => { "foo": [ "one", "two", "three", "four" ] } PLAY RECAP ****************************************************************** localhost : ok=4 changed=0 unreachable=0 failed=0
Indeed it is. You need to quote the entire list though:
- 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:
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" }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With