This is a quick demo to show you how to use nested variables, then override them to make them useful in your playbooks.
Subscribe To Me On YouTube: https://bit.ly/lon_sub
The first thing we do is create a new role to put our ansible code:
mkdir roles
cd roles
ansible-galaxy init nested_vars
Lets add the variables we need for this example:
vi nested_vars/defaults/main.yml
---
location: bristol
offices:
london:
users:
name: Dave
bristol:
users:
name: Jill
In this case I'm using debug to display the variables so I can show when they've changed.
vi nested_vars/tasks/main.yml
---
- name: London workers
debug:
msg: "{{ offices.london.users.name }}"
- name: Bristol workers
debug:
msg: "{{ offices.bristol.users.name }}"
- name: dynamic {{ location }} workers
debug:
msg: "{{ offices[location].users.name }}"
Come back to the root ansible directory. You should just see the roles directory we created earlier:
cd ..
ls -al
drwxrwxr-x. 3 ec2-user ec2-user 25 Feb 13 08:11 roles
Now let's add the deployment yaml file so we can run the role:
vi deploy.yml
---
- name: nested variables
hosts: localhost
connection: local
gather_facts: no
tasks:
- name: include roles
include_role:
name: nested_vars
With this in place, we can run our role.
A stright run of the role gives us this output:
[ec2-user@ip-172-31-16-55 nested_variables]$ ansible-playbook deploy.yml
does not match 'all'
PLAY [nested variables] ***********************************************************************************
TASK [include roles] **************************************************************************************
TASK [nested_vars : London workers] ***********************************************************************
ok: [localhost] => {
"msg": "Dave"
}
TASK [nested_vars : Bristol workers] **********************************************************************
ok: [localhost] => {
"msg": "Jill"
}
TASK [nested_vars : dynamic bristol workers] **************************************************************
ok: [localhost] => {
"msg": "Jill"
}
PLAY RECAP ************************************************************************************************
localhost : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
now, we have the ability to change the "location" as we need within out plybook or supply the variable as an extra_vars.
Due to the default we've set, when we run the role, the location will be Bristol by default. But we can use extra_vars to override this variable like this:
Here is the command and the output:
note: The location output has switched to London.
[ec2-user@ip-172-31-16-55 nested_variables]$ ansible-playbook deploy.yml -e 'location=london'
does not match 'all'
PLAY [nested variables] ***********************************************************************************
TASK [include roles] **************************************************************************************
TASK [nested_vars : London workers] ***********************************************************************
ok: [localhost] => {
"msg": "Dave"
}
TASK [nested_vars : Bristol workers] **********************************************************************
ok: [localhost] => {
"msg": "Jill"
}
TASK [nested_vars : dynamic london workers] ***************************************************************
ok: [localhost] => {
"msg": "Dave"
}
PLAY RECAP ************************************************************************************************
localhost : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
If you'd like a specific example let me know and I'll see what I can do.
Thanks:
- Dennis