diff --git a/tasks/vlans.yml b/tasks/vlans.yml
index 2f481e1..c8c77d9 100644
--- a/tasks/vlans.yml
+++ b/tasks/vlans.yml
@@ -1,39 +1,24 @@
---
+# example:
+# opn_vlans:
+# - tag: 110
+# uuid: ... # uuid definition is optional but recommended
+# vlan_parent_interface: lagg1
+# settings:
+# - key: descr
+# value: 110_USR
+# - tag: 220
+# vlan_parent_interface: lagg2
+# settings:
+# - key: descr
+# value: 220_SRV
+#
+# if uuid is not defined, the tag is used to generate a uuid
-- name: vlans - create vlans
- delegate_to: localhost
- community.general.xml:
- path: "{{ local_config_path }}"
- xpath: "/opnsense/vlans/vlan[tag/text()='{{ item.0.tag }}']/{{ item.1.key }}"
- value: "{{ item.1.value }}"
- pretty_print: true
- notify: reconfigure vlans
- with_subelements:
+- name: vlans - process vlans
+ ansible.builtin.include_tasks: vlansdo.yml
+ with_items:
- "{{ opn_vlans }}"
- - settings
-
-- name: vlans - setting if
- delegate_to: localhost
- community.general.xml:
- path: "{{ local_config_path }}"
- xpath: "/opnsense/vlans/vlan[tag/text()='{{ item.0.tag }}']/if"
- value: "{{ item.0.vlan_parent_interface | default(opn_interfaces_vlan_parent_interface) }}"
- pretty_print: true
- notify: reconfigure vlans
- with_subelements:
- - "{{ opn_vlans }}"
- - settings
-
-- name: vlans - setting vlanif
- delegate_to: localhost
- community.general.xml:
- path: "{{ local_config_path }}"
- xpath: "/opnsense/vlans/vlan[tag/text()='{{ item.0.tag }}']/vlanif"
- value: "{{ item.0.vlan_parent_interface | default(opn_interfaces_vlan_parent_interface) }}_vlan{{ item.0.tag }}"
- pretty_print: true
- notify: reconfigure vlans
- with_subelements:
- - "{{ opn_vlans }}"
- - settings
-
-...
+ loop_control:
+ label: "{{ _vlan.tag }}"
+ loop_var: _vlan
diff --git a/tasks/vlansdo.yml b/tasks/vlansdo.yml
new file mode 100644
index 0000000..5212970
--- /dev/null
+++ b/tasks/vlansdo.yml
@@ -0,0 +1,49 @@
+---
+
+- name: vlans - create vlans
+ delegate_to: localhost
+ community.general.xml:
+ path: "{{ local_config_path }}"
+ xpath: "/opnsense/vlans/vlan[tag/text()='{{ _vlan.tag }}']/{{ _vlansettings.key }}"
+ value: "{{ _vlansettings.value }}"
+ pretty_print: true
+ notify: reconfigure vlans
+ with_items:
+ - "{{ _vlan.settings }}"
+ loop_control:
+ label: "{{ _vlan.tag }} {{ _vlansettings.key }}"
+ loop_var: _vlansettings
+
+- name: vlans - setting if
+ delegate_to: localhost
+ community.general.xml:
+ path: "{{ local_config_path }}"
+ xpath: "/opnsense/vlans/vlan[tag/text()='{{ _vlan.tag }}']/if"
+ value: "{{ _vlan.vlan_parent_interface | default(opn_interfaces_vlan_parent_interface) }}"
+ pretty_print: true
+ notify: reconfigure vlans
+
+- name: vlans - setting vlanif
+ delegate_to: localhost
+ community.general.xml:
+ path: "{{ local_config_path }}"
+ xpath: "/opnsense/vlans/vlan[tag/text()='{{ _vlan.tag }}']/vlanif"
+ value: "{{ _vlan.vlan_parent_interface | default(opn_interfaces_vlan_parent_interface) }}_vlan{{ _vlan.tag }}"
+ pretty_print: true
+ notify: reconfigure vlans
+
+- name: vlans - define uuid
+ ansible.builtin.set_fact:
+ _uuid: "{{ _vlan.uuid | default(_vlan.tag | to_uuid) }}"
+
+- name: vlans - set uuid
+ delegate_to: localhost
+ community.general.xml:
+ path: "{{ local_config_path }}"
+ xpath: "/opnsense/vlans/vlan[tag/text()='{{ _vlan.tag }}']"
+ attribute: uuid
+ value: "{{ _uuid }}"
+ pretty_print: true
+ notify: reconfigure vlans
+
+...
diff --git a/test/test.yml b/test/test.yml
index b8d5e26..969c5e3 100644
--- a/test/test.yml
+++ b/test/test.yml
@@ -38,6 +38,9 @@
- name: nut restart
debug:
msg: fake handler - nut restart
+ - name: reconfigure vlans
+ debug:
+ msg: fake handler - reconfigure vlans
tasks:
- name: include default vars
ansible.builtin.include_vars:
@@ -51,6 +54,7 @@
- general
- filter
- alias
+ - vlans
- gateways
- wireguard
- ipsec
diff --git a/test/vlans-test-legacy-expect.xml b/test/vlans-test-legacy-expect.xml
new file mode 100644
index 0000000..d035a9e
--- /dev/null
+++ b/test/vlans-test-legacy-expect.xml
@@ -0,0 +1,19 @@
+
+
+
+
+
+
+ 110
+ 110_USR
+ lagg1
+ lagg1_vlan110
+
+
+ 220
+ 220_SRV
+ lagg2
+ lagg2_vlan220
+
+
+
diff --git a/test/vlans-test-legacy.yml b/test/vlans-test-legacy.yml
new file mode 100644
index 0000000..8255cb8
--- /dev/null
+++ b/test/vlans-test-legacy.yml
@@ -0,0 +1,13 @@
+---
+
+opn_vlans:
+ - tag: 110
+ vlan_parent_interface: lagg1
+ settings:
+ - key: descr
+ value: 110_USR
+ - tag: 220
+ vlan_parent_interface: lagg2
+ settings:
+ - key: descr
+ value: 220_SRV
diff --git a/test/vlans-test-uuid-expect.xml b/test/vlans-test-uuid-expect.xml
new file mode 100644
index 0000000..bcb9711
--- /dev/null
+++ b/test/vlans-test-uuid-expect.xml
@@ -0,0 +1,19 @@
+
+
+
+
+
+
+ 110
+ 110_USR
+ lagg1
+ lagg1_vlan110
+
+
+ 220
+ 220_SRV
+ lagg2
+ lagg2_vlan220
+
+
+
diff --git a/test/vlans-test-uuid.yml b/test/vlans-test-uuid.yml
new file mode 100644
index 0000000..65db06d
--- /dev/null
+++ b/test/vlans-test-uuid.yml
@@ -0,0 +1,15 @@
+---
+
+opn_vlans:
+ - tag: 110
+ vlan_parent_interface: lagg1
+ uuid: 60383065-4adf-5652-ace0-936604cd8376
+ settings:
+ - key: descr
+ value: 110_USR
+ - tag: 220
+ vlan_parent_interface: lagg2
+ uuid: 5847f2ba-18e3-5405-97aa-991425034f8a
+ settings:
+ - key: descr
+ value: 220_SRV