Understanding variables
Introduction
In Ansible, variables are a way to manage differences between systems. Being able to abstract these details away and replace them with variables makes your playbooks more maintainable, reusable, and flexible. In this article, we will explore variables in depth.
What are Variables?
Variables in Ansible are very similar to variables in any programming language. They store values, and these values can be referenced throughout your playbooks. Variables can be defined in many places and have specific precedence.
Where to Define Variables
Variables can be defined in several places:
- Inventory: Variables can be defined for each host in the inventory file.
- Playbooks: Variables can be defined in playbooks.
- Loaded from Files: Variables can be loaded from .yml or .json files for use in a playbook.
- Facts: Variables can be retrieved from target systems.
- Registered Variables: Variables can be registered from the results of a task.
Variable Syntax
In Ansible, you reference a variable by wrapping the variable name with double curly braces, {{ }}
.
---
- hosts: localhost
vars:
my_variable: 'Hello, World!'
tasks:
- name: Print variable
debug:
msg: '{{ my_variable }}'
In this example, my_variable
is the name of the variable and Hello, World!
is the value. The debug
module is used to print the variable's value.
Variable Types
Ansible supports several types of variables, including:
- Strings: These are simple text variables.
- Booleans: These represent true/false values.
- Lists: These are also known as arrays, and they are ordered lists of elements.
- Dictionaries: These are also known as hashes or arrays of arrays. They are unordered collections of key-value pairs.
Facts
Facts in Ansible are a way of getting environmental information from your target systems. They are variables that Ansible gathers when it executes a playbook.
---
- hosts: localhost
tasks:
- name: Display gathered facts
debug:
var: ansible_facts
Ansible gathers a comprehensive set of facts about each host. These can be used in your playbooks.
Variable Precedence
In Ansible, if you define a variable with the same name in multiple places, the last one wins. This is known as variable precedence. Here's the order of precedence from least to greatest:
- Role defaults
- Inventory file or script group vars
- Inventory group_vars/all
- Playbook group_vars/all
- Inventory group_vars/*
- Playbook group_vars/*
- Inventory file or script host vars
- Inventory host_vars/*
- Host facts / cached set_facts
- Play vars
- Play vars_prompt
- Play vars_files
- Role vars (defined in role/vars/main.yml)
- Block vars (only for tasks in block)
- Task vars (only for the task)
- Role (and include_role) vars
- Include vars
- Include_tasks / import_tasks vars
- Set_facts / registered vars
- Extra vars (always win precedence)
Conclusion
Variables are a key part of Ansible and understanding how to use them will help you create more efficient, effective playbooks. Practice using variables in different ways to solidify your understanding.
Note: This article assumes that the reader has a basic understanding of YAML syntax which is used in Ansible playbooks. If you are not familiar with YAML, you might need to brush up your YAML syntax knowledge.