I want to set vlans for mikrotik switch with ansible, but for this I need to concatenate variables bases on condition (tag existence)
Now my task:
core-access=eth1,eth2,eth3
core-access=eth11,eth12,eth13
wifi=eth4,eth5
core=eth6
#Tasks:
- name: Create/update vlan with core-access and wifi tag
community.routeros.command:
commands:
- /interface bridge vlan add tagged={{ core-access }},{{ wifi }} \
with_dict: "{{ global_vlan_list }}"
loop_control:
loop_var: item
when:
- host_vlans is not defined
- item.value.type is defined
- '"core-access" in item.value.type'
- '"access" not in item.value.type'
- '"wifi" in item.value.type'
- name: Create/update vlan with access and wifi tag
community.routeros.command:
commands:
- /interface bridge vlan add tagged={{ access }},{{ wifi }} \
with_dict: "{{ global_vlan_list }}"
loop_control:
loop_var: item
when:
- host_vlans is not defined
- item.value.type is defined
- '"core-access" not in item.value.type'
- '"access" in item.value.type'
- '"wifi" in item.value.type'
- name: Create/update vlan with access-core, access and wifi tag
community.routeros.command:
commands:
- /interface bridge vlan add tagged={{ core-acces }},{{access}},{{ wifi }} \
with_dict: "{{ global_vlan_list }}"
loop_control:
loop_var: item
when:
- host_vlans is not defined
- item.value.type is defined
- '"core-access" in item.value.type'
- '"access" in item.value.type'
- '"wifi" in item.value.type'
How can I solve this with one task. Now I use 6, but if I use 4-5 tags, the code become unusable.
I try to find a solution to use if in ansible, but without success. I want to simplify this solution. Maybe I'm coloser to solution with next step, but don't work: - name: Create/update vlan with access tag debug: msg: test {{ port_core if '"access" in item.value.type' else "" }},{{ port_wifi if '"wifi" in item.value.type' else "" }}
Sorry, I forget to define exactly the global_vlan_list variable.
I got the answer: https://hup.hu/node/182039#comment-2931460