Ansible Variable

What is a Variable in Ansible?

In Ansible, a variable is a name (placeholder) used to store a value that can be reused across playbooks, roles, inventories, and tasks.

Variables help make automation:

Instead of hard-coding values, you define variables and reuse them wherever needed.


Simple Definition

Ansible Variable = Placeholder for data

Example

app_port: 8080
app_port: 8080

You can use this variable anywhere in your playbook as:

{{ app_port }}
{{ app_port }}

Why Use Variables in Ansible?


Types of Ansible Variables (With Examples)

Variable Type Example Purpose
Play vars vars: Used only inside a playbook
Inventory vars host var Per host values
Group vars group_vars/web.yml Per group values
Host vars host_vars/server.yml Per host values
Role defaults defaults/main.yml Lowest priority
Role vars vars/main.yml High priority
Facts ansible_os_family System information
Extra vars -e "a=1" Highest priority
Register register: result Capture task output
Set facts set_fact: Runtime variables
vars_prompt User input Ask values at runtime
vars_files vars_files: Load variables from file
Environment vars environment: Set environment variables
Loop vars item Loop iteration
Magic vars hostvars Built-in variables

Example Playbook With Explanation

Playbook Code

---
- hosts: web
  vars:
    myname: "shan"
    myage: 26

  vars_files:
    - ak.yaml   # External variable file

  tasks:
    - name: Display my name and age
      debug:
        msg: "My name is {{ myname }} and my age is {{ myage }}"

    - name: Display OS distribution
      debug:
        msg: "The OS distribution is {{ ansible_distribution }}"

    - name: Display values from external file
      debug:
        msg: "Son of {{ fname }} & {{ mname }}"

    - name: Display host-specific variable
      debug:
        msg: "The server is located in {{ location }}"
---
- hosts: web
  vars:
    myname: "shan"
    myage: 26

  vars_files:
    - ak.yaml   # External variable file

  tasks:
    - name: Display my name and age
      debug:
        msg: "My name is {{ myname }} and my age is {{ myage }}"

    - name: Display OS distribution
      debug:
        msg: "The OS distribution is {{ ansible_distribution }}"

    - name: Display values from external file
      debug:
        msg: "Son of {{ fname }} & {{ mname }}"

    - name: Display host-specific variable
      debug:
        msg: "The server is located in {{ location }}"

Step-by-Step Explanation

1️⃣ Hosts Section

- hosts: web
- hosts: web

2️⃣ Playbook Variables (vars)

vars:
  myname: "shan"
  myage: 26
vars:
  myname: "shan"
  myage: 26

Usage:

{{ myname }}
{{ myage }}
{{ myname }}
{{ myage }}

3️⃣ External Variable File (vars_files)

vars_files:
  - ak.yaml
vars_files:
  - ak.yaml

Example ak.yaml:

fname: Kumar
mname: Lakshmi
fname: Kumar
mname: Lakshmi

These variables can be used directly in tasks:

{{ fname }}
{{ mname }}
{{ fname }}
{{ mname }}

4️⃣ Using Variables in Tasks

msg: "My name is {{ myname }} and my age is {{ myage }}"
msg: "My name is {{ myname }} and my age is {{ myage }}"

5️⃣ Using Facts (Built-in Variables)

{{ ansible_distribution }}
{{ ansible_distribution }}

6️⃣ Host-Specific Variables (Host Vars)

File:

host_vars/node1.yaml
host_vars/node1.yaml

Content:

location: Chennai
location: Chennai

Usage in playbook:

{{ location }}
{{ location }}