1. Overview
Ansible is a widely used tool for configuration management. One key characteristic of Ansible is the way it works with variables. Understanding Ansible variables enables effective configuration management.
In this tutorial, we’ll see how to get the list of all Ansible variables for a host or group.
To follow along with this tutorial, we need a control node with Ansible. Furthermore, the client nodes should also be set up to use Ansible to talk to the control node.
2. The debug Module
Ansible uses the debug module for showing variables or messages on stdout when a playbook runs. Several parameters are used with this module:
- msg: string-type parameter that prints a custom message
- var: variable to be debugged with msg and var being mutually exclusive
- verbosity: level at which the debug operation runs (e.g., for value of 3, debug runs with -vvv)
For this tutorial, we’ll use the debug module to list Ansible variables in many cases.
3. Ansible Variables
Variables in Ansible and other programming languages are rather similar. A variable can come from a playbook file or be passed at runtime. Further, we can use the register keyword to save the return value or values from a task run.
In addition, we can use conditional statements with variables in Ansible. This provides a way to execute tasks based on specific conditions. Notably, these variables can be referenced in playbooks.
In general, we can set variables in several ways for use in Ansible tasks. Let’s explore these ways and see how to list variables in each case.
4. Listing Variables in a File
Ansible provides flexibility in defining variables. For example, we can store them in different files. This enables us to organize and manage variables effectively. Thus, we can put them in separate environments with specific requirements.
4.1. Inventory File
The inventory file lists the hosts and groups we manage. Thus, it also provides an easy way to set variables that apply to specific hosts or groups.
Let’s use vim to add some variable definitions to our inventory file:
$ vim inventory.ini
[webservers]
client1 ansible_host=192.168.221.171 ansible_user=vagrant
client2 ansible_host=192.168.221.172 ansible_user=vagrant2
[webservers:vars]
ntp_server=ntp.example.com
proxy=proxy.example.com
In this inventory file, we’ve got some variables with values unique to each host:
- ansible_host
- ansible_vagrant
Similarly, there are two variables common to both hosts:
To list variables from the inventory file for host client2, we can use the ansible-inventory command:
$ ansible-inventory -i inventory.ini --host client2
{
"ansible_host": "192.168.221.172",
"ansible_user": "vagrant2",
"ntp_server": "ntp.example.com",
"proxy": "proxy.example.com"
}
With option -i, we specify our inventory file. Similarly, the –host option sets the target host. As a result, the variables unique to host client2 are shown on stdout.
4.2. Playbook
The vars section of a playbook can also be used to list variables. It’s also an easy way to set variables for each run of that playbook.
To list variables defined within a playbook, we can again use the debug module.
Let’s create a playbook, playbook_vars.yml:
$ cat playbook_vars.yml
---
- hosts: client1
vars:
var1:
- test1
- test2
tasks:
- debug:
var: var1
In this playbook, we define a list of variables under the var1 entry. Next, we use the debug module to print these variables.
Let’s run the above playbook with the ansible-playbook command:
$ ansible-playbook -i inventory.ini playbook_vars.yml
PLAY [client1] *************
TASK [Gathering Facts] ***
ok: [client1]
TASK [debug] **********
ok: [client1] => {
"var1": [
"test1",
"test2"
]
}
PLAY...
...
As a result, thevar1 and var2 variables are both listed in the output.
4.3. Reusable Variable File
An Ansible project can be more organized by adding variables to an external file. We can then include that file in our playbooks.
Let’s look at how to get a list of variables from these files.
First, we create a file to hold all our variables in a list format:
$ cat var_file.yml
---
my_vars:
- test1: 22
- test2: 23
- test3: 24
Next, we create a playbook that will list the variables under the my_vars list:
$ cat var_file_play.yml
---
- name: Printing Variable
hosts: client1
tasks:
- include_vars: var_file.yml
- debug:
var: my_vars
The approach used here is similar to the one we used with the playbook case above. However, here, we’ve used the include_vars statement to load our variable file.
Let’s now run the playbook:
$ ansible-playbook -i inventory.ini var_file_play.yml
PLAY [Printing Variable] **********************************************************
TASK [Gathering Facts] *********************************************************
ok: [client1]
TASK [include_vars] ************************************************************
ok: [client1]
TASK [debug] *******************************************************************
ok: [client1] => {
"my_vars": [
{
"test1": 22
},
{
"test2": 23
},
{
"test3": 24
}
]
}
PLAY RECAP *********************************************************************
...
Consequently, we can see variables from the file listed in the output.
5. Listing Runtime Variables
Ansible Playbook uses the –extra-vars option to receive variable definitions on the command line at run time. We can list these variables by using the debug module.
Let’s take another playbook example, rt_var.yml:
$ cat rt_var.yml
---
- hosts: client1
tasks:
- name: List a variable
debug:
var: my_var
Here, we’ve defined the my_var variable, the value for which will be defined at runtime.
Let’s run the playbook and supply a value for the variable my_var at runtime:
$ ansible-playbook -i inventory.ini rt_var.yml --extra-vars my_var=foo
PLAY [client1] *****************************************************************
TASK [Gathering Facts] *********************************************************
ok: [client1]
TASK [List a variable] *********************************************************
ok: [client1] => {
"my_var": "foo"
}
PLAY..
...
Thus, we can see the my_var variable listed in the output.
6. Listing Registered Variables
Ansible provides a way to redirect task results into new variables for reuse. Let’s register a variable and then show it on stdout.
First, we create a new playbook, reg_var.yml:
$ cat reg_var.yml
---
- name: Using Register variable
hosts: client1
tasks:
- name: Store a variable
command: "date +%H"
register: current_hour
- name: Print a variable
debug:
var: current_hour.stdout
In this playbook, we store the output from the date command in the current_hour variable. Then, we use the debug module to list the registered variables.
Let’s now run this playbook:
$ ansible-playbook -i inventory.ini reg_var.yml
PLAY [Using Register variable] *************************************************
TASK [Gathering Facts] *********************************************************
ok: [client1]
TASK [Store a variable] ********************************************************
changed: [client1]
TASK [Print a variable] ********************************************************
ok: [client1] => {
"current_hour.stdout": "07"
}
PLAY RECAP *
...
From the output, we can see that our current_hour variable contains the current time value in hours.
7. Listing Group Variables
Lastly, we can create a host group for hosts that share a common variable. Then, we apply the variable value to the whole group at a time.
Notably, we can define group variables at the group level in the inventory file or playbook. As a best practice, we should keep the file holding the variables in a separate directory.
Let’s create a directory, group_vars, to hold files with variables for our webservers group:
$ mkdir group_vars/
Next, we create a file in the above directory and assign our variables to it:
$ cat group_vars/webservers
---
my_vars:
- OS: Ubuntu
- uid: "445"
- Home: /home/ruhul
- gid: 69809
- shell: /bin/bash
To make use of our group variables, we create a playbook, grp_var.yml:
$ cat grp_var.yml
- hosts: client1
tasks:
- name: List a variable
debug:
var: my_vars
Let’s run the playbook and see the output:
$ ansible-playbook -i inventory.ini grp_var.yml
PLAY [client1] **************
TASK [List a variable] ******
ok: [client1] => {
"my_vars": [
{
"OS": "Ubuntu"
},
{
"uid": "445"
},
{
"Home": "/home/ruhul"
},
{
"gid": 69809
},
{
"shell": "/bin/bash"
}
]
}
PLAY RECAP...
...
In this way, the output shows all group variables.
8. Conclusion
In this article, we’ve explored various methods to list Ansible variables.
First, we used the inventory file, playbook, and reusable variable files. Next, we listed the users’ variables during runtime. Then, we employed registered variables. Finally, we saw how to list variables defined for a group of hosts.