Ansible Variables: Complete Guide to vars, facts & Precedence

By Luca Berton · Published 2024-01-01 · Category: installation

Complete guide to Ansible variables. Define vars, use facts, understand precedence, register output, and manage variables across inventories and roles.

Ansible variables are a core feature that allows users to make playbooks dynamic and reusable. By defining variables, you can simplify configurations, adapt tasks to different environments, and manage large-scale automation workflows effectively. This article explores Ansible variables, their types, and best practices for using them.

What Are Ansible Variables?

Ansible variables are key-value pairs that store data used during playbook execution. They make playbooks flexible by allowing you to dynamically configure tasks based on the target system or environment.

Key Features:Dynamic Customization: Modify task behavior based on variable values. • Centralized Management: Define variables in a single place for easier updates. • Reusability: Share variables across multiple playbooks.

Types of Ansible Variables

Ansible supports a variety of variable types to suit different use cases:

1. Playbook Variables Variables defined directly within a playbook.

Example:

2. Host Variables Variables specific to individual hosts, defined in the inventory file or host-specific files.

Example in Inventory:

Example in host_vars/host1.yml:

3. Group Variables Variables applied to groups of hosts, defined in the inventory file or group-specific files.

Example in group_vars/webservers.yml:

4. Facts Automatically gathered variables about the target system.

Example:

5. Command-Line Variables Variables passed at runtime using the --extra-vars option.

Example:

6. Default Variables Variables with default values defined in roles or playbooks.

Example:

Precedence of Variables

When multiple variables are defined, Ansible resolves conflicts using a precedence order. The order (from lowest to highest) includes: Defaults in roles. Inventory Group Variables. Inventory Host Variables. Playbook Variables. Extra Variables (always highest precedence).

Using Variables in Playbooks

Variables can be referenced using {{ variable_name }} syntax.

Example with Loops:

Example with Conditionals:

Example with Templates: Use variables in Jinja2 templates for dynamic configuration files. nginx.conf.j2:

Playbook:

Best Practices for Using Variables Use Meaningful Names: Avoid ambiguous variable names to ensure clarity. Organize Variables: Store variables in group_vars/ and host_vars/ directories for better structure. Secure Sensitive Data: Use Ansible Vault to encrypt variables like passwords and API keys. Define Defaults: Provide default values to prevent errors in playbook execution. Test Variable Resolution: Use ansible-playbook --check to validate variable substitution.

Conclusion

Ansible variables are a fundamental tool for making playbooks dynamic, reusable, and efficient. By leveraging different types of variables and following best practices, you can simplify complex automation workflows and manage diverse environments effectively.

Learn More About Ansible Variables

Define Variables

In playbook

In inventory

On command line

With set_fact (runtime)

Variable Types

Access Variables

Variable Precedence (Low → High) Role defaults (defaults/main.yml) Inventory group_vars Inventory host_vars Playbook group_vars Playbook host_vars Play vars Role vars (vars/main.yml) Block/task vars set_fact / registered vars Extra vars (-e) — always wins

Register Variables

Default Values

Variable in Conditionals

Encrypted Variables (Vault)

Special Variables

| Variable | Description | |----------|-------------| | inventory_hostname | Current host name | | ansible_host | Connection address | | groups | All inventory groups | | hostvars | All host variables | | play_hosts | Hosts in current play | | ansible_facts | Gathered facts | | role_path | Current role path |

FAQ

Why does my variable show as undefined?

Check: 1) Spelling, 2) Scope (defined in wrong play/role), 3) Precedence (overridden by higher priority). Use -e to override everything.

How do I use variables across plays?

Use set_fact with cacheable: true, or access via hostvars[hostname].variable.

Can I use variables in variable names?

Yes, with vars lookup: {{ hostvars[server_name][var_name] }} or {{ lookup('vars', dynamic_name) }}

Define Variables

Variable Sources

Register Variables

set_fact

Variable Precedence (Low → High)

Data Types

Access Nested Variables

Default Values

Prompt for Variables

Vault Encrypted Variables

FAQ

How to check if a variable is defined?

Can I use variables in variable names?

group_vars vs role defaults?

group_vars for environment-specific values (prod vs staging). Role defaults for sane default values that users can override.

Related ArticlesAnsible Template GuideAnsible Vault GuideAnsible when Conditional GuideAnsible Inventory GuideAnsible Loops Guide

Category: installation

Browse all Ansible tutorials · AnsiblePilot Home