Skip to main content

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:

  1. Inventory: Variables can be defined for each host in the inventory file.
  2. Playbooks: Variables can be defined in playbooks.
  3. Loaded from Files: Variables can be loaded from .yml or .json files for use in a playbook.
  4. Facts: Variables can be retrieved from target systems.
  5. 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:

  1. Role defaults
  2. Inventory file or script group vars
  3. Inventory group_vars/all
  4. Playbook group_vars/all
  5. Inventory group_vars/*
  6. Playbook group_vars/*
  7. Inventory file or script host vars
  8. Inventory host_vars/*
  9. Host facts / cached set_facts
  10. Play vars
  11. Play vars_prompt
  12. Play vars_files
  13. Role vars (defined in role/vars/main.yml)
  14. Block vars (only for tasks in block)
  15. Task vars (only for the task)
  16. Role (and include_role) vars
  17. Include vars
  18. Include_tasks / import_tasks vars
  19. Set_facts / registered vars
  20. 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.