Introduction
Ansible is a powerful automation tool that allows you to streamline your IT infrastructure management. One of the key features of Ansible is its ability to handle variables, which enables you to make your playbooks more dynamic and adaptable. In this tutorial, we'll explore how to pass variables to Ansible playbooks, from the basics to more advanced techniques, to help you optimize your automation workflows.
Introduction to Ansible Variables
Ansible is a powerful IT automation tool that allows you to manage your infrastructure and applications with ease. One of the key features of Ansible is its ability to work with variables, which are used to store and manipulate data within your playbooks.
Variables in Ansible can be defined at various levels, including:
Host-level Variables
These are variables that are specific to a particular host or group of hosts. They can be defined in the inventory file or in separate variable files.
## inventory.yml
all:
hosts:
webserver1:
ansible_host: 192.168.1.100
app_version: 2.0
webserver2:
ansible_host: 192.168.1.101
app_version: 2.1
Group-level Variables
These are variables that are applied to a group of hosts. They can be defined in the inventory file or in separate variable files.
## group_vars/webservers.yml
app_name: MyApp
db_host: 10.0.0.50
Play-level Variables
These are variables that are specific to a particular play within a playbook. They can be defined within the play itself.
- hosts: webservers
vars:
app_port: 8080
log_level: info
Task-level Variables
These are variables that are specific to a particular task within a play. They can be defined within the task itself.
- name: Install package
apt:
name: "{{ package_name }}"
state: present
vars:
package_name: nginx
Understanding how to work with these different types of variables is crucial for effectively managing your Ansible playbooks and automating your infrastructure.
Passing Variables to Ansible Playbooks
Now that you have a basic understanding of Ansible variables, let's explore how to pass variables to your Ansible playbooks. There are several ways to do this:
Command-line Arguments
You can pass variables to your playbook using the -e or --extra-vars option when running the ansible-playbook command.
ansible-playbook site.yml -e "app_version=2.2 db_host=10.0.0.60"
Variable Files
You can define variables in separate YAML files and pass them to your playbook using the --extra-vars option.
ansible-playbook site.yml --extra-vars "@vars.yml"
## vars.yml
app_name: MyApp
app_version: 2.2
db_host: 10.0.0.60
Inventory Variables
As mentioned in the previous section, you can define variables in your inventory file and they will be available to your playbook.
## inventory.yml
all:
hosts:
webserver1:
ansible_host: 192.168.1.100
app_version: 2.0
webserver2:
ansible_host: 192.168.1.101
app_version: 2.1
Playbook Variables
You can also define variables within your playbook using the vars keyword.
- hosts: webservers
vars:
app_name: MyApp
app_version: 2.2
db_host: 10.0.0.60
tasks:
- name: Print variables
debug:
msg: "App name: {{ app_name }}, App version: {{ app_version }}, DB host: {{ db_host }}"
By understanding these different ways of passing variables to your Ansible playbooks, you can make your automation more flexible and adaptable to different environments and requirements.
Advanced Variable Handling in Ansible
While the basic methods of passing variables to Ansible playbooks are straightforward, Ansible also provides more advanced features for working with variables. Let's explore some of these:
Variable Precedence
Ansible has a well-defined order of precedence for variables, which is important to understand when working with multiple sources of variables. The order, from highest to lowest, is:
- Command-line parameters (e.g.,
-e "var=value") - Task parameters (e.g.,
varswithin a task) - Include parameters (e.g.,
varswithin an included file) - Block parameters (e.g.,
varswithin a block) - Play parameters (e.g.,
varswithin a play) - Role default variables (e.g.,
defaults/main.yml) - Inventory variables (e.g., in
inventory.yml) - Host facts (e.g., gathered by the
setupmodule) - Role and include variables (e.g.,
vars/main.yml) - Set facts (e.g., using the
set_factmodule)
Understanding this precedence order can help you resolve conflicts and ensure that your variables are being used as intended.
Variable Filters and Tests
Ansible provides a rich set of Jinja2 filters and tests that you can use to manipulate and validate your variables. Some examples include:
defaultfilter:{{ my_var | default('default_value') }}to_jsonfilter:{{ my_dict | to_json }}is_definedtest:{% if my_var is defined %}...{% endif %}is_numbertest:{% if my_var is number %}...{% endif %}
These can be very useful for handling complex variable scenarios.
Vault-encrypted Variables
Ansible's Vault feature allows you to encrypt sensitive variables, such as passwords or API keys, so that they can be safely stored and used in your playbooks. This is particularly important for maintaining security in your infrastructure automation.
## vars.yml
db_password: !vault |
$ANSIBLE_VAULT;1.1;AES256
32313633326533343161663036623463
3163303236303836343239653533326134
3134613161623464
By understanding these advanced variable handling techniques, you can take your Ansible automation to the next level, making it more robust, secure, and adaptable to your organization's needs.
Summary
In this Ansible tutorial, you've learned how to pass variables to your playbooks, from simple variable assignment to more complex variable handling techniques. By mastering variable management, you can create flexible and dynamic Ansible playbooks that adapt to your changing infrastructure needs. Apply these strategies to enhance your Ansible-powered automation and take your IT operations to the next level.


