Among the different types of variables, Ansible provides default variables that hold a special place. They provide a way to define the fallback values in our playbook configurations.
In this tutorial, we will delve into Ansible’s default variables and learn what they are and how to use them in a playbook.
What Are the Ansible Default Variables?
In Ansible, the default variables, as the name suggests, are variables with default values. These variables act as a fallback mechanism. If a specific value for a variable is not provided elsewhere in a playbook or inventory, Ansible uses the value from the default variable.
The primary use of default variables is to avoid errors that are caused by missing variable definitions and to provide a base configuration that we can override when necessary.
Ansible Default Variables Definition
By default, the default variables in Ansible are defined in the “defaults” directory of the role. Inside this directory, you will often find a file named “main.yml” where you can list all your default variables.
An example directory structure is as follows:
|-- defaults/
| |-- main.yml
|-- tasks/
| |-- main.yml
The previous illustration shows a basic directory and file structure for a basic Ansible role.
Defining the Ansible Default Variables
To define the default variables, edit the defaults/main.yml file and define the variables like any other Ansible variable.
For example, consider a role that allows us to setup a basic web server. We might have the default values configured such as the port, root directory, etc. as follows:
webserver_port: 8080
webserver_name: "MyWebServer"
document_root: "/var/www/html"
In this case, we define three default variables to store the web server port, web server name, and the document root directory.
Luckily, since we defined them as default variables, we can easily override them in playbooks, host variables, group variables, etc. However, if we do not set a different value for them, Ansible uses the default values that are previously defined instead of throwing an error.
Overriding the Ansible Default Variables
As you might have guessed by now, the major strength and benefit of Ansible’s default variables is that we can override them when necessary.
For example, if we are running a playbook and we wish to use a different value for the webserver_port variable, we can define it in the playbook as an inventory as follows:
- hosts: webserver_hosts
roles:
- my_role
vars:
webserver_port: 9001
In this playbook, we quickly set a different value for the webserver_port from 8080 to 9001.
Ansible Variable Precedence
Before choosing which variable type you should use in your configurations, it is best to understand the Ansible variable precedence.
The key is default variables which have the lowest priority in the hierarchy. If a variable is defined anywhere such as in a playbook, hostvars, groupvars, etc., those values will take precedence over those that are specified in the “defaults” directory.
Think of Ansible variable precedence as described in the following:
- Variables passed via the command line (-e or –extra-vars) – Extreme priority
- host_vars and group_vars – Highest Priority
- Variables defined in a playbook – Higher priority
- Variables in roles/<role_name>/vars/main.yml – High priority
- Variables in roles/<role_name>/defaults/main.yml (Default variables) – Lowest Priority
Using the Ansible Default Variables
Let us now cover some basic examples of defining and using the Ansible default variables.
We can start by defining a default variable in the defaults/main.yml file as follows:
To use the default variable in a task, we can define it as follows:
mysql_db:
name: "{{ database_name }}"
state: present
Since we do not provide a different value for the database_name variable, Ansible creates a database with the “standard_db” name as defined in the defaults/main.yml file.
To override the value in a given task, we do the following:
- hosts: db_servers
vars:
database_name: "prod"
roles:
- db_role
Ansible creates a prod database instead of using the default value in this case.
Conclusion
This tutorial taught us the ins and outs of Ansible default variables. We learned what they are, what role they place, where to define them, how to use them, and how to override them when necessary.
As you can see in this post, the default variables offer a robust way to provide the base values. This can help make your roles and playbooks more reusable across environments and setups.