How specify a list value as variable in ansible inventory file?

ListVariablesAnsibleInventory

List Problem Overview


I need something like (ansible inventory file):

[example]
127.0.0.1 timezone="Europe/Amsterdam" locales="en_US","nl_NL"

However, ansible does not recognize 'locales' as a list.

List Solutions


Solution 1 - List

You can pass a list or object like this:

[example]
127.0.0.1 timezone="Europe/Amsterdam" locales='["en_US", "nl_NL"]'

Solution 2 - List

With complex variables, it's best to define them in a host_vars file rather than in the inventory file, since host_vars files support YAML syntax.

Try creating a host_vars/127.0.0.1 file with the following content:

timezone: Europe/Amsterdam
locales:
    - en_US
    - nl_NL

Solution 3 - List

Ryler's answer is good in this specific case but I ran into problems using other variations with the template module.

[example]
127.0.0.1 timezone="Europe/Amsterdam" locales='["en_US", "nl_NL"]'

Is his original example and works fine.

The following variations work with template. Basically if it's a string you must remember to use the internal double quotes or the entire structure is parsed as a single string. If it's only numbers or "True" or "False" (not "yes") then you're fine. In this variation I couldn't make it work with template if it had external quotes.

I haven't done an exhaustive check of which internal use cases they do and do not break other than the template module.

I am using Ansible 2.2.1.

[example:vars]
# these work
myvar1=["foo", "bar"]
myvar2=[1,2]
myvar3=[True,False]

# These fail, they get interpreted as a single string.
myvar4=[yes, no]
myvar5=[foo,bar]
myvar6='["foo", "bar"]'

Solution 4 - List

you can try split

#inventory file
[example]
127.0.0.1 timezone="Europe/Amsterdam" locales="en_US","nl_NL"

#role file
---
- debug: msg="{{ item }}"
  with_items: locales.split(',')

Solution 5 - List

I believe the case is where you define your variable.

if it is under a

[host:vars] var=["a", "b"]

otherwise:

[hosts] host1 var='["a", "b"]'

Solution 6 - List

INI file with variables looks like this

$ cat ./vars/vars.yml

lvol_names=['2g-1','2g-2','2g-3']

the variable represents the list type

 lvol_names:
       - 2g-1
       - 2g-2
       - 2g-3

the variable can be read from a playbook via lookup:

$ cat ./play.yml

- name: play1
  hosts: kub2_data_nodes
  become: yes


  vars:
 
     - lvol_names: "{{ lookup('ini', 'lvol_names type=properties file=./vars/vars.yml') }}"
     

Solution 7 - List

You can custom a filter, to split string to list

Github ansible example show how to create custom filter.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionrmullerView Question on Stackoverflow
Solution 1 - ListRyler HockenburyView Answer on Stackoverflow
Solution 2 - ListLorin HochsteinView Answer on Stackoverflow
Solution 3 - ListSparkyView Answer on Stackoverflow
Solution 4 - ListjollychangView Answer on Stackoverflow
Solution 5 - ListBrandon KauffmanView Answer on Stackoverflow
Solution 6 - ListAlexView Answer on Stackoverflow
Solution 7 - ListlinboView Answer on Stackoverflow