Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to set a fact of an array in Ansible?

Tags:

ansible

Is it possible to set a fact containing a list in Ansible using set_fact? What's the correct syntax for it?

like image 874
xiamx Avatar asked May 07 '14 02:05

xiamx


People also ask

Can Ansible playbook set fact?

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.

How do you assign values in Ansible?

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!

What does fact mean in Ansible?

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.


2 Answers

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    
like image 93
lindes-hw Avatar answered Sep 20 '22 12:09

lindes-hw


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" } 
like image 27
Bruce P Avatar answered Sep 18 '22 12:09

Bruce P