How to Use Variables in Ansible Playbook

If you have done some programming in Python, Java, C, C++ or in any other programming language, then variables are not alien. Just like in programming languages, variables in Ansible are used to store a value.

In Ansible, variables provide the much-needed flexibility in Playbooks, templates and inventories as we shall later see in this tutorial. Built-in variables can be used to provide system information such as hostname, system architecture, interfaces etc. Variables can also be used to replace strings and also in loops to loop through a given set of values.

A valid variable name is made of the following characters

  • Letters
  • Numbers
  • Underscores
  • A combination of any two or all of the above


Examples of acceptable variable names include:

  •  turntable
  •  turn_table
  •  turntable01
  •  turntable_01

The names below do not qualify as valid variable names

  •   turn table
  •  turn-table
  •  01turntable
  •  01

Let’s now look at different use cases of Variables in Ansible

Variables in Playbooks

As you know, A playbook is a collection of plays or tasks to be performed on a remote system. In playbooks, variables are handy in determining how tasks can be executed. 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.

 - hosts: all
     salutations: Hello guys!
   - name: Ansible Variable Basic Usage
       msg: "{{ salutations }}"

In the playbook above, the variable name is  salutations  and the value is Hello world! When the playbook is run, the value of the playbook is accessed by placing the variable between curly braces as shown above. When executed, the playbook prints the message ‘Hello guys’ on the terminal.


Variables with Arrays

You can use arrays and assign them to variables as shown in the syntax below:



        – value1

        – value2

For example, the playbook below contains an array of 5 student names stored in a variable called students. You can access the student names through indexing ( as you would in arrays in any other programming language)  For example, to retrieve the name ‘Arthur’ from the array list use the syntax students [2] as shown below.

- hosts: all
      - Mark
      - Melisa
      - Arthur
      - Kevin
      - Lisa

  - name: Ansible List variable Example
      msg: "{{ students[2] }}"

Variables with dictionaries

From the Array list, you can further define each of the entries using a key value pair forming what we call a dictionary list. The syntax is shown below




              value1: itemvalue1

              value2: itemvalue2


            value1: itemvalue1

            value2: itemvalue2

Using our previous example, we can have dictionary lists below

- hosts: all
         - Mark: 
             city: Melbourne
             address: 0045-0987-8642
         - Angela: 
             city: Sydney
             address: 3456-7685-9087

To access the dictionary, simply add the task below

    - debug:
        var: students


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.

For example:

Let’s assume we have 2 web servers: webserver_1 and webserver_2 which are both listening to port 443 and whose  ntp server is The inventory file will appear as shown:



In the YAML playbook file, this would be represented as shown


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

Host and Group variables

Although you can specify variables in the inventory file, standard practice discourages  storing variables in the inventory file. This is where the host and group variable files come in.

In a host variable file, the variable applier only to one host system in the inventory file. The host variable file is usually stored in the host_vars directory which is usually specified in /etc/ansible/ path.

Consider the inventory file below where we have 2 servers each using different ntp servers


Instead of  specifying the variable ntp_server in the inventory file, create 2 host variable files in the host_vars directory with the file name matching each of the hostnames of the host systems:

# vim /etc/ansible/host_vars/webserver_1

# vim /etc/ansible/host_vars/webserver_2

If the host systems share the same values, create a group variable file in the  group_vars directory. The group variable file name should match the group of the hosts

Consider the inventory file below


The second section can be defined as a file in the group_vars directory as shown. This should be in the /etc/ansible directory. Remember that the name of the file should be the same as the group name.

# vim /etc/ansible/group_vars/web_servers

Here, the variables will be accessible to all the hosts of the group called  web_servers.

Special variables in Ansible Playbook

Another category of variables are special variables. These are builtin variables  which cannot be set by the user and which Ansible will always override. To get a list of unique system variable names run the command

# ansible -m setup hostname

Examples of such special variables include:

  • ansible_all_ipv4_addresses
  • ansible_architecture ,
  • ansible_bios_version ,
  • ansible_os_family ,
  • ansible_distribution,

This will display an  output in JSON format

# ansible -m setup localhost


With the flexibility and ease of use, variables serve a quintessential purpose in both playbooks and inventory files. They avoid unnecessary repetition and make the life of the systems administrator much easier.

Also Read: How to Use Loops in Ansible Playbook

Also Read : How to Use Ansible Vault to Secure Sensitive Data

2 Responses

  1. Jones says:

    Great overview, however the part about how to access/use the shown dictionary is missing?

Leave a Reply

Your email address will not be published. Required fields are marked *

Pin It on Pinterest