Defining Variables in Ansible
Introduction
Variables are an essential part of any configuration management system. In Ansible, variables allow you to manage dynamic values. This tutorial will guide you through the process of defining variables in Ansible, with detailed explanations and examples.
Basic Variable Definition
Variables in Ansible are defined using YAML syntax. They can be defined in various places such as inventory files, playbooks, or separate variable files. Here's a simple example of a variable definition:
In a playbook:
--- - hosts: all vars: my_variable: "Hello, Ansible!"
Using Variables in Playbooks
Once a variable is defined, it can be used in playbooks. Variables are referenced using the Jinja2 templating system, which uses double curly braces {{ }}
. Here's an example:
--- - hosts: all vars: greeting: "Hello" name: "World" tasks: - name: Print greeting debug: msg: "{{ greeting }}, {{ name }}!"
Variable Precedence
Ansible has a specific order of precedence for variables. This means that if a variable is defined in multiple places, Ansible will determine which value to use based on where it was defined. The order of precedence is as follows:
- Extra vars (always win precedence)
- Task vars (only for the current task)
- Block vars (only for tasks in the current block)
- Role and include vars (only for tasks in the current role or include)
- Play vars
- Set_facts / registered vars
- Host facts
- Playbook group_vars
- Playbook host_vars
- Inventory group_vars
- Inventory host_vars
- Inventory file or script group vars
- Inventory file or script host vars
- Role defaults
Defining Variables in Inventory Files
Variables can also be defined in inventory files. This allows you to associate variables with specific hosts or groups of hosts. Here's an example of an inventory file with variables:
[webservers] web1 ansible_host=192.168.1.10 http_port=80 web2 ansible_host=192.168.1.11 http_port=8080 [databases] db1 ansible_host=192.168.1.20 db_port=3306 db2 ansible_host=192.168.1.21 db_port=5432
Defining Variables in Separate Files
For better organization, variables can be stored in separate files. These files can then be included in playbooks. Here's an example of a variable file:
variables.yml:
--- greeting: "Hello" name: "Ansible User"
And here's how to include it in a playbook:
--- - hosts: all vars_files: - /path/to/variables.yml tasks: - name: Print greeting debug: msg: "{{ greeting }}, {{ name }}!"
Host and Group Variables
Host and group variables are defined in the host_vars
and group_vars
directories. These directories should be at the same level as your inventory file. Here's an example:
Directory structure:
inventory/ |-- hosts |-- host_vars/ | |-- web1.yml | |-- web2.yml |-- group_vars/ |-- webservers.yml |-- databases.yml
Contents of group_vars/webservers.yml
:
--- http_port: 80
Contents of host_vars/web1.yml
:
--- ansible_host: 192.168.1.10
Conclusion
Defining and using variables in Ansible is a powerful way to manage dynamic values in your playbooks. By understanding the different ways to define variables and the order of precedence, you can write more flexible and maintainable automation scripts.