How to use Variables in Ansible

Reading Time: 4 minutes

                  

In programming, a variable is a value that can change, depending on conditions or on information passed to the program. Similarly, variables in Ansible are very similar to using variables in any programming language. It helps you to use and assign a value to a variable and use that anywhere in the playbook.therefore one can put conditions around the value of the variables and accordingly use them in the playbook in Ansible.

Valid Variable Names in Ansible:

Not all strings are valid Ansible variable names. A variable name can only include letters, numbers, and underscores. It cannot begin with a number. Some of the valid variable names are –

foo_port , region5 , _tomcat_port , and many more.

Defining Simple Variables in your Playbook:

You can define a simple variable using standard YAML syntax. For instance:

Calling Simple Variables:

List Variables:

A list variable combines a variable name with multiple values. You can define variables as list in following way:

And will reference this variable as:

Dictionary Variables:

A dictionary stores the data in key-value pairs. However, dictionaries are used to store related data. therefore, you can define variables as dictionary in following way:

To access the dictionary, simply add the task as below:

Variables in Inventory files

If you have different host systems that share similar attributes or values, you can define what we call group variables. These are used  to assign the attributes which are common to all the hosts present in the group. For instance:Let’s,assume we have 2 web servers: my_webserver_1 and my_webserver_2 which are both listening to port 445 and whose  ntp server is my.ntp.org. The inventory file will appear as shown:

However, the YAML playbook file, this would be represented as shown:

The first section defines the remote hosts which are my_webserver_1 and my_webserver_2, while the second section represents  the common attributes between the two servers. This comes as a very convenient method of applying variables that are common to multiple host systems.

Special Variables in Ansible:

Another category of variables are special variables. These are builtin variables  which are predefined and which cannot be set by the user. Some of these variables are:

inventory_hostname : The inventory name for the ‘current’ host being iterated over in the play

hostvars : A dictionary/map with all the hosts in inventory and variables assigned to them

ansible_inventory_sources : List of sources used as inventory

group_names : List of groups the current host is part of

After that,there are many more special variables.

Defining Variables at Runtime:

You can define variables when you run your playbook by passing variables at the command line using the –extra-vars (or -e) argument. 

Syntax For Passing Extra Variables:

Or

In addition,If we want to pass multiple variables, we need to enclose our vars and their values in quotation marks:

Variable Precedence:

However ansible has a precedence for loading variable data, and thus it has an order and a definition to decide which variable will win. In Ansible, Extra vars (from command-line) always win i.e it has highest precedence. It will override all the variables.

Conclusion:

In conclusion,we have seen variables play a very important role in Ansible. We have learnt  what are variables in Ansible, types of variables in Ansible and how to declare variables in different ways and also the precedence in Variables. 

Leave a Reply