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)
dnmvisser
(Dick Visser)
October 7, 2024, 11:14am
2
@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:
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
]
}
jiholland
(JĆørn Ivar)
October 7, 2024, 12:24pm
4
---
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
jiholland
(JĆørn Ivar)
October 7, 2024, 1:14pm
6
You need to use it in a playbook.
- name: Print all_vlans.
ansible.builtin.debug:
var: all_vlans
bvitnik
(Bojan Vitnik)
October 7, 2024, 9:10pm
7
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.
bcoca
(Brian Coca)
October 7, 2024, 9:27pm
8
didnāt see anyone show the āreuseā, so here it is
reuse_vlans: '{{vlans}}'
utoddl
(Todd Lewis)
October 8, 2024, 12:31am
9
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.
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
system
(system)
Closed
November 7, 2024, 12:31am
10
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.