Skip to main content

RHCE Certification Course

Chapter #23: How to Work with Ansible Variables and Facts

In this chapter, learn how to define and use Ansible variables and system facts in playbooks. Includes examples of lists, dictionaries, inventory variables, and custom facts.

In this chapter, we will going to discuss how to work with Ansible Variables and Facts. A variable, just like in many programming languages, is essentially a key that represents a value.

What Constitutes a Valid Variable Name?

A variable name includes letters, numbers, underscores, or a mix of either 2 or any of them. However, bear in mind that a variable name must always begin with a letter and should not contain spaces.

Let’s take a look at a few examples of valid and unacceptable variable names:

Valid Variable Name Examples:

football  
foot_ball  
football20  
foot_ball20  

Non-valid Variable Name Examples:

foot ball  
20  
foot-ball  

Let’s discuss the variable types:

Playbook Variables

Playbook variables are quite easy and straightforward. 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.

Here’s a simple playbook example:

- hosts: all
  vars:
    greeting: Hello world! 

  tasks:
  - name: Ansible Basic Variable Example
    debug:
      msg: "{{ greeting }}"

In the above playbook, the greeting variable is substituted by the value Hello world! when the playbook is run. The playbook simply prints the message Hello world! when executed.

Fig 1: Playbook Variables in Ansible

Additionally, you can have a list or an array of variables as shown:

The playbook below shows a variable called continents. The variable holds 5 different values – continent names. Each of these values can easily be accessed using index 0 as the first variable.

The example of the playbook below retrieves and displays Asia (Index 1).

- hosts: all
  vars:
    continents:
      - Africa
      - Asia
      - South America
      - North America
      - Europe
      
  tasks:
  - name: Ansible List variable Example
    debug:
      msg: "{{ continents [1] }}"
Fig 2: Array of Variables in Ansible

The variable list can similarly be structured as shown:

vars:
    Continents: [Africa, Asia, South America, North America, Europe]

Note: Variable names are case-sensitive in Ansible. So Continents is different from continents. Be consistent when referencing them.

To list all the items on the list, use the with_items module. This will loop through all the values in the array.

- hosts: all
  vars:
    continents: [Africa, Asia, South America, North America, Europe]

  tasks:
  - name: Ansible array variables example
    debug: 
      msg: "{{ item }}"
    with_items:
      - "{{ continents }}"
Fig 3: List Ansible Array Variables

Dictionary Variables

Another type of Ansible variable is the dictionary variable.

Dictionary variables are additionally supported in the playbook. To define the dictionary variable, simply indent the key-value pair just below the dictionary variable name.

hosts: switch_f01

vars:
   http_port: 8080
   default_gateway: 10.200.50.1
   vlans:
       id: 10
       port: 2

In the example above, vlans is the dictionary variable while id and port are the key-value pairs.

hosts: switch_f01

vars:
   http_port: 8080
   default_gateway: 
   vlans:
      id: 10
      port: 20

 tasks:
   name: Configure default gateway
   system_configs:
     default_gateway_ip: β€œ{{ default_gateway  }}β€œ

   name: Label port on vlan 10
   vlan_config:
     vlan_id: β€œ{{ vlans['id']  }}β€œ
     port_id: 1/1/{{ vlans['port']  }}
πŸ’‘
Always use straight double quotes (") for variables and avoid curly quotes (β€œ ”) which can cause YAML syntax errors.

Also, dictionary keys in square brackets should use single quotes around the key when needed, as shown above.

For port_id, since we are starting the value with text and not the variable, quotation marks are not necessary to surround the curly braces.

Special Variables

Ansible provides a list of predefined variables that can be referenced in Jinja2 templates and playbooks, but cannot be altered or defined by the user.