Ansible Role Hierarchy Explained - anishnath/ansible GitHub Wiki
Ref : Ansible Roles
Ansible Role Hierarchy Explained
Ansible supports property hierarchy, the ansible variable can be overwritten if the following condition mataches
Ansible Order of Execution is
- If roles/roletest/tasks/main.yml exists, tasks listed therein will be added to the play
- If roles/roletest/handlers/main.yml exists, handlers listed therein will be added to the play
- If roles/roletest/vars/main.yml exists, variables listed therein will be added to the play
- If roles/roletest/defaults/main.yml exists, variables listed therein will be added to the play
- If roles/roletest/meta/main.yml exists, any role dependencies listed therein will be added to the list of roles
Lets Begin the Test
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, first we define an ansible variable first in the roletest/defaults/main.yml and in the **roletest/vars/main.yml **
[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 debugging tasks the ansible variable first from where it's picking
[ansible@controller roles]$ cat roletest/tasks/main.yml
---
- debug:
msg: "{{first}}"
The sample roletest test Playbook run file
[ansible@controller opt]$ cat roletest.yml
- hosts: localhost
roles:
- { role: roletest }
Running the playbook, In the first example the first variable is loaded from the roletest/vars/main.yml file, since the tasks/main.yml doesn't contain any first variable
[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 Property
create variable first in the **group_vars/all.yml ** and 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"
}
Placing This property in the Inventory Hosts (Host Variables)
[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"
}
overriding all property
In this example we will be passing the first variable in the Ansible main playbook run file roletest.yml
[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"
}
At the begining we have told variable placed in tasks/main.yml will have the MAX preferences, Now this ansible variable first is defined as 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'
TASK [roletest : debug] *****************************************************************************************
ok: [localhost] => {
"msg": "THIS ALWAYS GIVE FIRST PRIORITY"
}
Thanks for reading, Consider Paypal