In this tutorial, I will introduce Ansible variables and explore their usage with examples.
Prerequisites
To proceed with this tutorial, you will need the following:
- An Ansible management node with Ansible installed and configured
- An Ansible target node.
- SSH key-based authentication between the management node and the target node
Check out our introductory articles in this Ansible beginner’s tutorial, where we explain how to configure the necessary prerequisites.
Use variables in inventories
You can use variables in inventory files to define properties specific to hosts or groups. This helps in making your playbooks more flexible and reusable.
Let’s create an inventory.txt file with the following content.
[web]webserver1 ansible_host=192.168.1.101
webserver2 ansible_host=192.168.1.102
[db]
dbserver1 ansible_host=192.168.1.201
[web:vars]
http_port=80
user=admin
In this example, httpport and user are group variables defined under the [web:vars] section. These variables will apply to all hosts under the [web] group.
After defining variables in your inventory file, you can use them in your Ansible playbooks.
Create a playbook.yaml file and add the following content:
---
- hosts: web
tasks:
- name: Print HTTP port
debug:
msg: "HTTP Port is {{ http_port }}"
- name: Print username
debug:
msg: "Username is {{ user }}"
In this playbook, {{ http_port }} and {{ user }} variables in the playbook will be substituted with the values defined in your inventory for the web hosts group.
Now, run the playbook using the following command.
ansible-playbook -i inventory.txt playbook.yaml
You will see the values of the {{ httpport }} and {{ user }} variables in the following screen.
Showing the value of variables defined in an inventory file
Use variables within a task in playbooks
You can define variables directly within a task. Let’s create a playbook.yaml file with the following content:
---
- hosts: db
vars:
db_port: 3306
user: "root"
tasks:
- name: Print database port
debug:
msg: "Database Port is {{ db_port }}"
- name: Print username
debug:
msg: "Username is {{ user }}"
Now, run the playbook using the following command.
ansible-playbook -i inventory.txt playbook.yaml
You will see the values of variables {{ dbport }} and {{ user }} on the following screen.
Showing the value of variables defined within a tasks
Use variables in files
You can define variables in separate YAML files and include them in the playbook using the vars_files directive.
Let’s create a playbook.yaml file and add the following content.
---
- name: Playbook with Variable Files
hosts: db
vars_files:
- vars_file.yaml
tasks:
- name: Task using variable from file
debug:
msg: "The value of variable is {{ var }}"
Next, create a varsfile.yaml in your current working directory and add the following content:
var: "value from file"
Now, run the playbook using the following command.
ansible-playbook -i inventory.txt playbook.yaml
This will fetch the value of the var variable from the varsfile.yaml file and print it on the screen.
Showing the value of variables defined in a file
Use variables with prompts
Ansible facilitates prompting for variable input during playbook execution with the help of the vars_prompt section.
Proceed by creating a playbook.yaml and include the subsequent content:
---
- name: Playbook with Prompted Variables
hosts: db
vars_prompt:
- name: user_input
prompt: "Enter a value:"
tasks:
- name: Task using prompted variable
debug:
msg: "You entered: {{ user_input }}"
Now, run the playbook using the following command.
ansible-playbook -i inventory.txt playbook.yaml
You will be prompted to enter a value, which will then appear on the screen.
Showing the value of input variables
Showing the value of input variables
Pass variables via the command line
You can pass variables via the command line using the -e option in Ansible. This lets you define or override variables when running your playbook or ad-hoc commands.
Create a playbook.yaml file and add the following content:
---
- name: Example Playbook
hosts: db
vars:
my_variable: "{{ my_var | default('default_value_if_not_passed') }}"
tasks:
- name: Display variable value
debug:
var: my_variable
This playbook contains a vars section where the variable my_variable is defined using the value passed via the command line. It uses the default filter to set a default value default_value_if_not_passed if my_var is not provided via the command line.
Now, run the playbook and pass a variable my_var with the value hello:
ansible-playbook -i inventory.txt -e "my_var=hello" playbook.yaml
You will see the value of my_variable based on the passed or default value.
Register task output as a variable
In Ansible, you can register the output of a task and save it as a variable using the register keyword. This allows you to capture the result of a task and then use that result in subsequent tasks within the playbook.
Create a playbook.yaml with the following content.
---
- name: Register Task Output Example
hosts: db
tasks:
- name: Run a command and register its output
command: free -m
register: command_output
- name: Display registered output
debug:
var: command_output
In this playbook, the command task executes a free -m command. The outcome of this command is then saved in a variable named command_output using the register keyword. Subsequently, the debug task is used to present the value stored in the command_output variable.
Proceed by running the given playbook.
ansible-playbook -i inventory.txt playbook.yaml
You will see the output of the free -m command on the following screen.
Showing the value of registered output
Use a conditional variable
In Ansible, you can use conditional statements to set variables dynamically based on certain conditions. Ansible allows for using the when statement to set conditions under which a variable should be defined or assigned a particular value.
Let’s create a playbook.yaml file with the following content.
---
- name: Conditional Variable Example
hosts: db
gather_facts: false
vars:
my_condition: true
tasks:
- name: Set variable based on condition
set_fact:
my_variable: "value_when_true"
when: my_condition == true
- name: Set variable based on alternative condition
set_fact:
my_variable: "value_when_false"
when: my_condition != true
- name: Display variable value
debug:
var: my_variable
Now, run the above playbook.
ansible-playbook -i inventory.txt playbook.yaml
This playbook sets a variable my_condition to true. The first task uses the set_fact module to set the variable my_variable to value_when_true only when my_condition is true. The second task uses set_fact to set my_variable to value_when_false when my_condition is not true. The debug task displays the value of my_variable. Depending on the value of my_condition, either value_when_true or value_when_false will be assigned to my_variable.