Skip to content

Instantly share code, notes, and snippets.

@surajsaini95
Last active April 10, 2020 06:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save surajsaini95/fa7b748ac60143fb5d53b5c7a213907d to your computer and use it in GitHub Desktop.
Save surajsaini95/fa7b748ac60143fb5d53b5c7a213907d to your computer and use it in GitHub Desktop.
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

system1
counter
my_hobby

Invalid variables

system 1
cou-nter
my hobby

Frequently used vars

  • Simple vars
    str=I am a string
    
  • List vars
    devops:
      - LFS
      - Bash
      - Python
      - Ansible
      - Jenkins
      - Docker
    
  • Dictionary vars
        devops:
          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 
    
    Example
      "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
  vars:
    str: I am playbook var
  tasks:
  - name: print str variable
    debug:
      msg: "{{ str }}"

Vars in inventory

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

[aws]
3.21.165.170 user=Sam

[aws:vars]
str=I am inventory var
---
- name: This playbook uses inventory variable
  hosts: aws
  gather_facts: false
  tasks:
  - name: print str variable
    debug:
      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
  vars_files:
    - external_vars.yml
  
  tasks:
  - name: prints str
    debug:
      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)
include_vars
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