Skip to content

Instantly share code, notes, and snippets.

@dmccuk
Last active February 16, 2024 09:06
Show Gist options
  • Save dmccuk/b86a5398aa2771c113e4ad2e8ef5228a to your computer and use it in GitHub Desktop.
Save dmccuk/b86a5398aa2771c113e4ad2e8ef5228a to your computer and use it in GitHub Desktop.

Ansible: Nested and Dynamic Variables

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

Create a role

The first thing we do is create a new role to put our ansible code:

mkdir roles
cd roles
ansible-galaxy init nested_vars

Create the variable file

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

Create the tasks

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 }}"

Create the deploy file

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.

Running the 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.

Set the variable using 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment