Is it possible to set a fact containing an array in ansible using set_fact
? What's the correct syntax for it?
相关问题
- Access ansible.cfg variable in task
- Unable to get “exclude” option working with unarch
- Ansible: find file and loop over paths
- Generating tuples variables in ansible templates
- Ansible: setting user on dynamic ec2
相关文章
- Ansible create postgresql user with access to all
- Difference between shell and command in ansible
- How to make Ansible run one certain task only on o
- clone a specific branch from git through ansible p
- Check if a list contains an item in Ansible
- Defining host as variable in Ansible hosts file
- ansible/jinja2 get unique subelements
- Custom credentials in Ansible Tower with Custom Py
Adding to the already given answers I would like to show you a different way to define a list with
set_fact
using the regular YAML syntax instead of the custom format which Ansible core folks like to use in their docs. This custom format, like in this case, has shown lead to confusion.Consider this example:
Straight forward, right? Just as you would do in any normal YAML document. So why not use it in Ansible YAML tasks file too?
About the combination of lists mentioned by @lindes-hw. There is more than one way to do this. The following examples use Jinja2 syntax to define the list:
The second example uses the
union
filter. Refer to the set theory filters in the Ansible docs.I had a similar requirement to create list of server objects based on list of IP addresses.
Gives following output
Indeed it is. You need to quote the entire array though:
The above tasks should generate the following output:
Yes, this is possible. As mentioned in another answer, you can set an array using double quotes, like so:
However, I thought I'd create another answer to indicate that it's also possible to add to an existing array, like so:
Combining these and adding debugging as a playbook (which I'm calling
facts.yml
) like so:Produces (via
ansible-playbook facts.yml
) the following:I don't know if the functionality changed --- it possibly did in 2.5 --- but it looks to me like this code
in the example doesn't work any more you have to use
I'm using Ansible 2.5.2. I was getting this (incorrect) output:
I found the updated syntax in the docs here:
https://docs.ansible.com/ansible/latest/user_guide/playbooks_loops.html#with-items