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:
- Ansible RHCE #1: Introduction and installation
- Ansible RHCE #1: Running ad-hoc commands
- Ansible RHCE #1: Understanding playbooks
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