Variables in host_vars possible?

Hi all, we use lists for vlans in our host_vars files.
Is it possible to reuse variables definded further up in the file?
For example like this:

vlans:
  - 34
  - 356
  - 22

reuse_vlans: vlans

add_vlans:
  - vlans
  - 666

I have trouble to get this working. Maybe you can help me?

Thanks, starflighter

Ansible-Version: ansible 2.10.6 (we are stuck at python version 3.6.15)

https://docs.ansible.com/ansible/latest/collections/ansible/builtin/union_filter.html

@dnmvisser Do you have an example how to use the filter in a host_vars file?
That would be great :slight_smile:
I canā€™t make it work. The filter is just printed out as string.
content of host_vars file:

vlans:
  - 34
  - 356
  - 22

reuse_vlans: vlans

add_vlans: "{{ vlan | union('666') }}"

Output of ansible-inventory --host xxx

{
    "add_vlans": "{{ vlan | union('666') }}",
    "ansible_host": "xxx",,
    "reuse_vlans": "vlans",
    "vlans": [
        34,
        356,
        22
    ]
}

---
vlans:
  - 34
  - 356
  - 22
add_vlans:
  - 666
all_vlans: "{{ vlans | union(add_vlans) }}"
1 Like

Hi @jiholland,
I copied your example to host_vars/r1-xxx.
And the result from ansible-inventory --host r1-xxx is:

{
    "add_vlans": [
        666
    ],
    "all_vlans": "{{ vlans | union(add_vlans) }}",
    "ansible_host": "xxx",
    "vlans": [
        34,
        356,
        22
    ]
}

So the filter is not executed, do you have any Idea?

Thanks, starflighter

You need to use it in a playbook.

- name: Print all_vlans.
  ansible.builtin.debug:
    var: all_vlans

Just a note that lists can be concatenated in Jinja scripts using + operator so this is also valid:

---
vlans:
  - 34
  - 356
  - 22
add_vlans:
  - 666
all_vlans: "{{ vlans + add_vlans }}"

Unlike with union, list element ordering is preserved in this case.

didnā€™t see anyone show the ā€˜reuseā€™, so here it is

reuse_vlans: '{{vlans}}'

You can also do it at the YAML level ā€” no Ansible involved!

---
vlans: &vlans
  - 34
  - 356
  - 22

reuse_vlans: *vlans

add_vlans:
  - *vlans
  - 666

Bonus: It uses the same syntax as C pointers, because thatā€™s never lead to mistakes. :slight_smile:

You can read about Anchors and Aliases in the YAML 1.2 spec, or google it for comprehensible explanations.

The spec says it, but it doesnā€™t actually tell you anything unless you already know what it means. Classic spec! Donā€™t get me wrong: itā€™s a good spec, but itā€™s not a tutorial.

Note: ā€œadd_vlansā€ above results in a two-element list. The first element is the three-element vlans list, and the second element is ā€œ666ā€. Hereā€™s the playbook with a little cheating.

# starflighter-01.yml
- name: Variables in host_vars possible
  hosts: localhost
  gather_facts: false
  vars:
    vlans: &vlans
      - 34
      - 356
      - 22

    reuse_vlans: *vlans

    add_vlans:
      - *vlans
      - 666
  tasks:
    - name: Show vlans
      ansible.builtin.debug:
        msg:
          - '{{ vlans }}'
          - '{{ reuse_vlans }}'
          - '{{ add_vlans | flatten }}'  # <-- I cheated!

which produces

TASK [Show vlans] ****
ok: [localhost] => 
  msg:
  - - 34
    - 356
    - 22
  - - 34
    - 356
    - 22
  - - 34
    - 356
    - 22
    - 666
1 Like