How to use Ansible Variable, Overriding Ansible variables

by Anish


Posted on Tuesday July 3rd


Ansible Variable, Overriding Ansible variables, Group Vars/ Host Variables

Ansible supports property hierarchy, the ansible variable can be overwritten if the following condition matches

Ansible Order of Execution is

  • If variables present in roles/roletest/tasks/main.yml exists,then tasks listed therein will be added to the play
  • If variables present in roles/roletest/handlers/main.yml exists,then handlers listed therein will be added to the play
  • If variables present in roles/roletest/vars/main.yml exists,then variables listed therein will be added to the play
  • If variables present in roles/roletest/defaults/main.yml exists,then variables listed therein will be added to the play
  • If variables present in roles/roletest/meta/main.yml exists,any role dependencies listed therein will be added to the list of roles


Lets Begin the Test

In the first create the directory structure used in this example

mkdir -p roles/roletest/tasks/
mkdir -p roles/roletest/vars/
mkdir -p roles/roletest/handlers/
mkdir -p roles/roletest/defaults/
mkdir -p group_vars/

To begin this test,define an ansible variable first in the roletest/defaults/main.yml and in the roletest/vars/main.yml

Content of these files are

[[email protected] roles]$ cat roletest/defaults/main.yml 
first: coming default main
[[email protected] roles]$ cat roletest/vars/main.yml 
first: 'coming from vars main'
[[email protected] roles]$ 

The tasks/main.yml file, a very basic definition with debug tasks to check from where the ansible variable first is coming

[[email protected] roles]$ cat roletest/tasks/main.yml 
---
  - debug:
      msg: "{{first}}"

The content of the roletest.yml Playbook run file

[[email protected] opt]$ cat roletest.yml 
- hosts: localhost
  roles:
     - { role: roletest }

Running the playbook, I

In the first example the ansible variable first is loaded from the roletest/vars/main.yml file, since the tasks/main.yml doesn't contain this variable defination

[[email protected] opt]$ ansible-playbook roletest.yml 
TASK [roletest : debug] *****************************************************************************************
ok: [localhost] => {
    "msg": "coming from vars main"
}

Now remove this variable first from the roletest/vars/main.yml file and re-run the playbook

This time variable is picked from roletest/defualt/main.yml


TASK [roletest : debug] *****************************************************************************************
ok: [localhost] => {
    "msg": "coming default main"
}

Overriding Ansible variable Property

With this structure using group variable the property can be overridden

create variable first in the group_vars/all.ymland re-run the Playbook (Group Variable)

[[email protected] opt]$ cat group_vars/all.yml 
first: "Inside groupvar"

On the Playbook run this property is overridden by group variable

TASK [roletest : debug] *****************************************************************************************
ok: [localhost] => {
    "msg": "coming from groupvar"
}

Override group var through Host Variable

Update the Inventory file and define host variable

[local]
localhost first="Coming from Inventroy"

Run the Ansible Playbook you will be notice inventory property is given with max priority from group variable

TASK [roletest : debug] *****************************************************************************************
ok: [localhost] => {
    "msg": "Coming from the Inventory"
}

Passing Variable in Ansible Playbook Run

In this example we will be passing the first variable in the Ansible main playbook run file roletest.yml

Note We have still not defined any variable defination in the tasks/main.yml file

[[email protected] opt]$ cat roletest.yml 
- hosts: localhost
  vars:
     - first: "Coming from Main run"
  roles:
     - { role: roletest }

After running the Playbook, this ansible variable first has picked up the Max priority


TASK [roletest : debug] *****************************************************************************************
ok: [localhost] => {
    "msg": "Coming from Main run"
}

Final test

Test ansible variable first placed in tasks/main.yml will have the MAX preferences

Now this ansible variable first is defined in HOST_VARIABLE , GROUP_VARS, defaults and vars

Modifying the file tasks/main.yml and including the variable file myvar.yml

[[email protected] opt]$ cat roles/roletest/tasks/main.yml 
---
  - include_vars: myvar.yml
  - debug:
      msg: "{{first}}"

Place this file myvar.yml under roletest/vars/ and run the playbook

The sample Content of the file

[[email protected] opt]$ cat roles/roletest/vars/myvar.yml 
first: 'THIS ALWAYS GIVE FIRST PRIORITY'

The Ansible Playbook run output

TASK [roletest : debug] *****************************************************************************************
ok: [localhost] => {
    "msg": "THIS ALWAYS GIVE FIRST PRIORITY"
}


Thanku for reading !!! Give a Share for Support

Asking for donation sound bad to me, so i'm raising fund from The Modern Cryptography CookBook for Just $9. Leanpub books Discount coupon first 100 reader. No hurry read the sample chapters here then decide.

Alternatively to support you can buy My all four Cryptography book Just $10.99


Referefce
  • The Modern Cryptography Book.
  • Go lang Cryptography for developers
  • Python Cryptography
  • Cryptography for JavaScript Developer


python Cryptography Topics
Topics
For Coffee/ Beer/ Amazon Bill and further development of the project Support by Purchasing, The Modern Cryptography CookBook for Just $9 Coupon Price

Cryptography for Python Developers

Cryptography for JavaScript Developers

Go lang ryptography for Developers

Here