Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
this blog provides a brief guide on using variables in ansible

Ansible Variables

While automation makes it easier to do things which are repeatable but all systems are not exactly alike as some may require configuration that is slightly different from others.

For example, you might need to find out the IP address of a system and use it as a configuration value on another system.

Ansible uses variables to help deal with differences between systems.

Naming variables correctly

Variable names should be letters, numbers, and underscores. Variables should always start with a letter.

Valid variables


Invalid variables

system 1
my hobby

Frequently used vars

  • Simple vars
    str=I am a string
  • List vars
      - LFS
      - Bash
      - Python
      - Ansible
      - Jenkins
      - Docker
  • Dictionary vars
          LFS: Linux File System
          Bash: USed for scripting
          Python: Used for scripting
          Ansible: used for automation
  • Special vars These are builtin variables which cannot be set by the user and which Ansible will always override. Use setup module to them all by gathering system facts.
     $ ansible localhost -m setup 
      "ansible_distribution": "Ubuntu", 
         "ansible_distribution_file_parsed": true, 
         "ansible_distribution_file_path": "/etc/os-release", 
         "ansible_distribution_file_variety": "Debian", 
         "ansible_distribution_major_version": "18", 
         "ansible_distribution_release": "bionic", 
         "ansible_distribution_version": "18.04", 

Where to define variables

Ansible allows to define variables at various places and they all have precedence associated with them.

Vars in playbook

Variables can be accessed with {{ }} to use their assigned values.

- name: This playbook uses variable
  hosts: aws
  gather_facts: false
    str: I am playbook var
  - name: print str variable
      msg: "{{ str }}"

Vars in inventory

Variables defined in inventory can be accessed in playbook in similar fashion as above.

[aws] user=Sam

str=I am inventory var
- name: This playbook uses inventory variable
  hosts: aws
  gather_facts: false
  - name: print str variable
      msg: "{{ str }}"

Vars in Roles

There is a dedicated directory as vars for defining variables in role which can be used effectively. Also there is a directory as defaults which allow you to set default variables for included or dependent roles

Vars in files

Variables can also be included in the playbook which are not the part of the role. You can do this by using an external variables file, or files, just like this:

- name: This playbook external variables from different file.
  hosts: localhost
  gather_facts: false
    - external_vars.yml
  - name: prints str
      msg: "{{ str }}"

# external_vars.yml
str: I am external var

Vars through command line

It is possible to set variables at the command line using the --extra-vars (or -e) argument.

ansible-playbook playbook-demo.yml -e "str='I am command line var'"

Precedence of vars

As per the Ansible documentation the vars precedence will be :

# low to high

command line values (eg “-u user”)
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/*
playbook 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)
set_facts / registered vars
role (and include_role) params
include params
extra vars (always win precedence)

Note : If multiple variables of the same name are defined in different places, they get overwritten in a certain order.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment