Bootstrap FreeKB - Ansible - Getting Started with Nested Variables
Ansible - Getting Started with Nested Variables

Updated:   |  Ansible articles

Let's say you are creating variables using the vars pluginNested variables can be created like this.

---
- hosts: all
  vars:
    food:
      fruit: apple
      veggy: onion
  tasks:
  - ansible.builtin.debug:
      var: food
...

 

Or using the set_fact module.

---
- hosts: all
  tasks:
  - ansible.builtin.set_fact:
      food:
        fruit: apple
        veggy: onion
  - ansible.builtin.debug:
      var: food
...        

 

The debug module can be used to print the value of the "food" key which should return something like this.

ok: [server1.example.com] => {
    "food": {
      "fruit": "apple",
      "veggy": "onion"
    }
}

 

The debug module can be used to print the value of the "fruit" variable.

- name: output the nested 'food.fruit' variable
  debug:
    var: food.fruit

 

Or like this, using Jinja2 syntax, delimiting each key with a period.

- name: output the nested 'food.fruit' variable
  debug:
    msg: "{{ food.fruit }}"

 

Or like this, using brackets.

- name: output the nested 'food.fruit' variable
  debug:
    msg: "{{ food['fruit'] }}"

 

Something like this should be returned.

ok: [server1.example.com] => {
    "food.fruit": "Apple"
}

 

Period Delimiter

Let's say you have a key that contains a period (foo.bar in this example).

foo.bar:
  var1: "Hello World"

 

If a key contains a period, you'll have to use brackets, like this.

- name: output the nested 'foo.bar.var1' variable
  debug:
    msg: "{{ foo.bar['var1'] }}"

 

Magic Variables

Let's say you one of your keys matches a magic variable. In this example, the "server1" key would probably match the inventory_hostname_short magic variable.

server1:
  bar: "Hello World"

 

In this scenario, using vars may get the job done.

- name: output the nested 'hostname.bar' variable
  debug:
    var: vars[inventory_hostname_short].bar

 

Or, use the vars lookup plugin to reference the inventory_hostname_short variable.

- name: output the nested 'hostname.bar' variable
  debug:
    msg: "{{ lookup('vars', inventory_hostname_short)['bar'] }}"

 

In this example, the "server1" key would probably match the inventory_hostname_short magic variable, and is below the foo key.

foo:
  server1:
    bar: "Hello World"

 

In this scenario, when referencing the magic variable with brackets, do not place single quotes around the magic variable.

- name: output the nested 'foo.server1.bar' variable
  debug:
    msg: "{{ foo[inventory_hostname_short]['bar'] }}"

 

--extra-vars

Let's say you set the env variable to contain a value of dev using the -e or --extra-vars command line option.

ansible-playbook foo.yml --extra-vars "env=dev"

 

In this example, the env variable contains a value of dev, which matches the "dev" key.

dev:
  foo:
    bar: "Hello World"

 

In this scenario, use the vars lookup plugin to reference the env variable (dev key).

- name: output the nested 'env.foo.bar' variable
  debug:
    msg: "{{ lookup('vars', env)['foo']['bar'] }}"



Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 0126c5 in the box below so that we can be sure you are a human.