Sep 23, 2021 11 min read

RHCE Ansible Series #4: Ansible Variables, Facts and Registers

In the fourth chapter of RHCE Ansible EX 294 exam preparation series, you'll learn about variables, facts and registers in this chapter.

There will always be a lot of variances across your managed systems. For this reason, you need to learn how to work with Ansible variables.

In this tutorial, you will learn how to define and reference variables Ansible. You will also learn how to use Ansible facts to retrieve information on your managed nodes.

Furthermore, you will also learn how to use registers to capture task output.

If it is your first time here, please have a look at previous chapters in this series:

Part 1: Working with variables in Ansible

Let's start with variables first. Keep in mind that all this is written in your YML file.

Defining and referencing variables

You can use the vars keyword to define variables directly inside a playbook.

For example, you can define a fav_color variable and set its value to yellow as follows:

---
- name: Working with variables
  hosts: all
  vars:
    fav_color: yellow

Now how do you use (reference) the fav_color variable? Ansible uses the Jinja2 templating system to work with variables. There will be a dedicated tutorial that discusses Jinja2 in this series but for now you just need to know the very basics.

To get the value of the fav_color variable; you need to surround it by a pair of curly brackets as follows:

My favorite color is {{ fav_color }}

Notice that if your variable is the first element (or only element) in the line, then using quotes is mandatory as follows:

"{{ fav_color }} is my favorite color."

Now let’s write a playbook named variables-playbook.yml that puts all this together:

[[email protected] plays]$ cat variables-playbook.yml 
---
- name: Working with variables
  hosts: node1
  vars:
    fav_color: yellow
  tasks:
    - name: Show me fav_color value
      debug:
        msg: My favorite color is {{ fav_color }}.

I have used the debug module along with the msg module option to print the value of the fav_color variable.

Now run the playbook and you shall see your favorite color displayed as follows:

[[email protected] plays]$ ansible-playbook variables-playbook.yml 

PLAY [Working with variables] **************************************************

TASK [Gathering Facts] *********************************************************
ok: [node1]

TASK [Show me fav_color value] *************************************************
ok: [node1] => {
    "msg": "My favorite color is yellow."
}

PLAY RECAP *********************************************************************
node1                      : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

Creating lists and dictionaries

You can also use lists and dictionaries to define multivalued variables. For example, you may define a list named port_nums and set its value as follows:

  vars:
    port_nums: [21,22,23,25,80,443]

You could have also used the following way to define port_nums which is equivalent:

vars:
    port_nums:
      - 21
      - 22
      - 23
      - 25
      - 80
      - 443

You can print all the values in port_nums as follows:

All the ports {{ port_nums }}

You can also access a specific list element:

First port is {{ port_nums[0] }}

Notice that you use an index (position) to access list elements.

You can also define a dictionary named users as follows:

vars:
    users:
      bob:
        username: bob
        uid: 1122
        shell: /bin/bash
      lisa:
        username: lisa
        uid: 2233
        shell: /bin/sh

There are two different ways you can use to access dictionary elements:

  • dict_name['key'] -> users['bob']['shell']
  • dict_name.key -> users.bob.shell

Notice that you use a key to access dictionary elements.

Now you can edit the variables-playbook.yml playbook to show lists and dictionaries in action:

[[email protected] plays]$ cat variables-playbook.yml 
---
- name: Working with variables
  hosts: node1
  vars:
    port_nums: [21,22,23,25,80,443]
 
    users: 
      bob:
        username: bob
        uid: 1122
        shell: /bin/bash
      lisa:
        username: lisa
        uid: 2233
        shell: /bin/sh
  tasks:
    - name: Show 2nd item in port_nums
      debug:
        msg: SSH port is {{ port_nums[1] }}

    - name: Show the uid of bob
      debug:
        msg: UID of bob is {{ users.bob.uid }}

You can now run the playbook to display the second element in port_nums and show bob’s uid:

[[email protected] plays]$ ansible-playbook variables-playbook.yml 

PLAY [Working with variables] **************************************************

TASK [Show 2nd item in port_nums] **********************************************
ok: [node1] => {
    "msg": "SSH port is 22"
}

TASK [Show the uid of bob] *****************************************************
ok: [node1] => {
    "msg": "UID of bob is 1122"
}

Including external variables

Just like you can import (or include) tasks in a playbook. You can do the same thing with variables as well. That is, in a playbook, you can include variables defined in an external file.

To demonstrate, let’s create a file named myvars.yml that contains our port_nums list and users dictionary:

[[email protected] plays]$ cat myvars.yml 
---
port_nums: [21,22,23,25,80,443]

users:
  bob:
    username: bob
    uid: 1122
    shell: /bin/bash
  lisa:
    username: lisa
    uid: 2233
    shell: /bin/sh

Read the full story

The rest of the article is available to LHB members only. You can sign up now for FREE to read the rest of this article along with access to all members-only posts. You also get subscribed to our fortnightly Linux newsletter.

Subscribe
Already have an account? Sign in
Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to Linux Handbook.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.