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

[ansible@controller roles]$ cat roletest/defaults/main.yml 
first: coming default main
[ansible@controller roles]$ cat roletest/vars/main.yml 
first: 'coming from vars main'
[ansible@controller roles]$ 

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

[ansible@controller roles]$ cat roletest/tasks/main.yml 
---
  - debug:
      msg: "{{first}}"

The content of the roletest.yml Playbook run file

[ansible@controller 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

[ansible@controller 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)

[ansible@controller 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

[ansible@controller 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

[ansible@controller 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

[ansible@controller 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


Your Support Matters!

Instead of directly asking for donations, I'm thrilled to offer you all nine of my books for just $9 on leanpub By grabbing this bundle you not only help cover my coffee, beer, and Amazon bills but also play a crucial role in advancing and refining this project. Your contribution is indispensable, and I'm genuinely grateful for your involvement in this journey!

Any private key value that you enter or we generate is not stored on this site, this tool is provided via an HTTPS URL to ensure that private keys cannot be stolen, for extra security run this software on your network, no cloud dependency




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

Kubernetes for DevOps

Hello Dockerfile

Cryptography for Python Developers

Cryptography for JavaScript Developers

Go lang ryptography for Developers

Here