# Variables in host\_vars possible?

**URL:** <https://forum.ansible.com/t/variables-in-host-vars-possible/9740>\
**Category:** Get Help\
**Tags:** playbook\
**Created:** [October 7, 2024, 10:29am UTC](https://forum.ansible.com/t/variables-in-host-vars-possible/9740 "2024-10-07T10:29:30Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![starflighter](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/starflighter/32/4992_2.png) [@starflighter](https://forum.ansible.com/u/starflighter)\
**Post date:** [October 7, 2024, 10:29am UTC](https://forum.ansible.com/t/variables-in-host-vars-possible/9740/1 "2024-10-07T10:29:30Z")

</div>

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:

```auto
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)

---

<div class="post-metadata">

**Author:** ![dnmvisser](https://avatars.discourse-cdn.com/v4/letter/d/8e8cbc/32.png) [@dnmvisser](https://forum.ansible.com/u/dnmvisser)\
**Post date:** [October 7, 2024, 11:14am UTC](https://forum.ansible.com/t/variables-in-host-vars-possible/9740/2 "2024-10-07T11:14:25Z")

</div>

[https://docs.ansible.com/ansible/latest/collections/ansible/builtin/union\_filter.html](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/union_filter.html)

---

<div class="post-metadata">

**Author:** ![starflighter](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/starflighter/32/4992_2.png) [@starflighter](https://forum.ansible.com/u/starflighter)\
**Post date:** [October 7, 2024, 12:14pm UTC](https://forum.ansible.com/t/variables-in-host-vars-possible/9740/3 "2024-10-07T12:14:56Z")

</div>

@dnmvisser Do you have an example how to use the filter in a host\_vars file?  
That would be great 🙂  
I can’t make it work. The filter is just printed out as string.  
content of host\_vars file:

```auto
vlans:
  - 34
  - 356
  - 22

reuse_vlans: vlans

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

```

Output of ansible-inventory --host xxx

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

```

---

<div class="post-metadata">

**Author:** ![jiholland](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jiholland/32/11273_2.png) [@jiholland](https://forum.ansible.com/u/jiholland)\
**Post date:** [October 7, 2024, 12:24pm UTC](https://forum.ansible.com/t/variables-in-host-vars-possible/9740/4 "2024-10-07T12:24:57Z")

</div>

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

```

---

<div class="post-metadata">

**Author:** ![starflighter](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/starflighter/32/4992_2.png) [@starflighter](https://forum.ansible.com/u/starflighter)\
**Post date:** [October 7, 2024, 12:53pm UTC](https://forum.ansible.com/t/variables-in-host-vars-possible/9740/5 "2024-10-07T12:53:46Z")

</div>

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

```auto
{
    "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

---

<div class="post-metadata">

**Author:** ![jiholland](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jiholland/32/11273_2.png) [@jiholland](https://forum.ansible.com/u/jiholland)\
**Post date:** [October 7, 2024, 1:14pm UTC](https://forum.ansible.com/t/variables-in-host-vars-possible/9740/6 "2024-10-07T13:14:46Z")

</div>

You need to use it in a playbook.

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

```

---

<div class="post-metadata">

**Author:** ![bvitnik](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bvitnik/32/4132_2.png) [@bvitnik](https://forum.ansible.com/u/bvitnik)\
**Post date:** [October 7, 2024, 9:10pm UTC](https://forum.ansible.com/t/variables-in-host-vars-possible/9740/7 "2024-10-07T21:10:23Z")

</div>

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

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

```

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

---

<div class="post-metadata">

**Author:** ![bcoca](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bcoca/32/9269_2.png) [@bcoca](https://forum.ansible.com/u/bcoca)\
**Post date:** [October 7, 2024, 9:27pm UTC](https://forum.ansible.com/t/variables-in-host-vars-possible/9740/8 "2024-10-07T21:27:30Z")

</div>

didn’t see anyone show the ‘reuse’, so here it is

```auto
reuse_vlans: '{{vlans}}'

```

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [October 8, 2024, 12:31am UTC](https://forum.ansible.com/t/variables-in-host-vars-possible/9740/9 "2024-10-08T00:31:34Z")

</div>

You can also do it at the YAML level — no Ansible involved!

```yaml
---
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. 🙂

You can read about [Anchors and Aliases](https://yaml.org/spec/1.2.2/#3222-anchors-and-aliases) in the YAML 1.2 spec, or [google it](https://www.google.com/search?q=yaml+anchors+aliases) 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.

```yaml
# 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

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

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [November 7, 2024, 12:31am UTC](https://forum.ansible.com/t/variables-in-host-vars-possible/9740/10 "2024-11-07T00:31:55Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
