Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 705
rmuller Avatar asked Sep 02 '13 11:09

rmuller


People also ask

How do you declare variables in Ansible inventory?

You can define these variables in your playbooks, in your inventory, in re-usable files or roles, or at the command line. You can also create variables during a playbook run by registering the return value or values of a task as a new variable.

How do you use inventory variables in Ansible playbook?

To try this playbook on servers from your inventory file, run ansible-playbook with the same connection arguments you've used before when running our first example. Again, we'll be using an inventory file named inventory and the sammy user to connect to the remote servers: ansible-playbook -i inventory playbook-02.

How do you mention variables in Ansible?

To define a variable in a playbook, simply use the keyword vars before writing your variables with indentation. To access the value of the variable, place it between the double curly braces enclosed with quotation marks. In the above playbook, the greeting variable is substituted by the value Hello world!

Which command is used to access the list of Ansible variables?

to use the command on a single IP/host not given in an inventory, invoke: ansible <ip> -m setup -i <ip>, - don't forget the comma at the end to make it a list and hence an inventory.


2 Answers

You can pass a list or object like this:

[example] 127.0.0.1 timezone="Europe/Amsterdam" locales='["en_US", "nl_NL"]' 
like image 114
Ryler Hockenbury Avatar answered Sep 20 '22 18:09

Ryler Hockenbury


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 
like image 28
Lorin Hochstein Avatar answered Sep 22 '22 18:09

Lorin Hochstein