From 73a38f863ad3727a363bd85082adac1709a8dbee Mon Sep 17 00:00:00 2001 From: Orion Poplawski Date: Fri, 4 Dec 2026 08:42:38 -0700 Subject: [PATCH 1/3] [pfsense_aggregate] Fix for combined initial creation of VLANs and interfaces --- plugins/module_utils/interface.py | 11 +++++- .../plugins/modules/test_pfsense_aggregate.py | 37 +++++++++++++++++++ .../plugins/modules/test_pfsense_interface.py | 15 +++++++- 3 files changed, 60 insertions(+), 3 deletions(-) diff --git a/plugins/module_utils/interface.py b/plugins/module_utils/interface.py index 73eafa5a..16f1c1a3 100644 --- a/plugins/module_utils/interface.py +++ b/plugins/module_utils/interface.py @@ -436,7 +436,7 @@ def _run_post(self): def _get_interface_list(self): # The configuration is read through config_get_path() when pfSense has it: pfSense Plus can serve its configuration # from a controller (Netgate Nexus), and then leaves $config unset. - return self.pfsense.php( + interfaces = self.pfsense.php( "require_once('/etc/inc/interfaces.inc');" "$cfg = function_exists('config_get_path') ? config_get_path('', []) : $config;" "$portlist = get_interface_list();" @@ -495,7 +495,14 @@ def _get_interface_list(self): "foreach ($ipsec_descrs as $ifname => $ifdescr) $portlist[$ifname] = array('descr' => $ifdescr);" "" "echo json_encode($portlist, JSON_PRETTY_PRINT);" - ) + ) or {} + + # Aggregate VLAN additions are staged in XML and applied before interface setup. + for vlan in self.pfsense.get_elements("vlans/vlan"): + vlanif = vlan.findtext("vlanif") + if vlanif: + interfaces.setdefault(vlanif, self.pfsense.element_to_dict(vlan)) + return interfaces def _get_media_mode(self, interface): """Find all possible media options for the interface""" diff --git a/tests/unit/plugins/modules/test_pfsense_aggregate.py b/tests/unit/plugins/modules/test_pfsense_aggregate.py index f1e04ba6..2153b48f 100644 --- a/tests/unit/plugins/modules/test_pfsense_aggregate.py +++ b/tests/unit/plugins/modules/test_pfsense_aggregate.py @@ -294,6 +294,43 @@ def test_aggregate_interface_gateway(self): self.assert_xml_elt_dict("gateways", {"name": "UPSTREAM_GW"}, {"interface": "wan", "gateway": "192.0.2.1"}) self.assertEqual(self.xml_result.findtext("interfaces/wan/gateway"), "UPSTREAM_GW") + def test_aggregate_assigns_new_vlan_to_interface(self): + self.load_fixtures() + + def php_response(command): + if "get_interface_list()" in command: + # The new VLAN exists only in the in-memory XML until the aggregate is saved. + return {"vmx0": {}} + return ["autoselect"] + + self.php.side_effect = php_response + args = dict( + aggregated_vlans=[dict(vlan_id=101, interface="vmx0", descr="voice")], + aggregated_interfaces=[dict(descr="VOICE", interface="vmx0.101", enable=True)], + ) + with set_module_args(dict(args, _ansible_check_mode=True)): + preview = self.changed(True) + self.assertFalse(self.load_xml_result()) + self.phpshell.assert_not_called() + + with set_module_args(args): + result = self.changed(True) + self.assertEqual(result["result_vlans"], preview["result_vlans"]) + self.assertEqual(result["result_interfaces"], preview["result_interfaces"]) + self.assert_find_vlan("vmx0", "101") + interface = self.find_xml_tag(self.xml_result.find("interfaces"), {"descr": "VOICE", "if": "vmx0.101"}) + self.assertIsNotNone(interface) + self.phpshell.assert_called_once() + php = self.phpshell.call_args[0][0] + self.assertLess(php.index("interface_vlan_configure("), php.index(f"interface_configure('{interface.tag}', true)")) + + self.phpshell.reset_mock() + with set_module_args(args): + result = self.changed(False) + self.assertEqual(result["result_vlans"], []) + self.assertEqual(result["result_interfaces"], []) + self.phpshell.assert_not_called() + def test_aggregate_aliases_purge(self): """test creation of a some aliases with purge""" args = dict( diff --git a/tests/unit/plugins/modules/test_pfsense_interface.py b/tests/unit/plugins/modules/test_pfsense_interface.py index ac5556a7..faee0176 100644 --- a/tests/unit/plugins/modules/test_pfsense_interface.py +++ b/tests/unit/plugins/modules/test_pfsense_interface.py @@ -139,6 +139,16 @@ def test_interface_create_by_descr(self): command = "create interface 'VOICE', port='vmx0.100'" self.do_module_test(interface, command=command) + def test_interface_create_on_configured_vlan_with_no_php_inventory(self): + def php_response(command): + if "get_interface_list()" in command: + return None + return ["autoselect"] + + self.php.side_effect = php_response + interface = dict(descr="VOICE", interface="vmx0.100") + self.do_module_test(interface, command="create interface 'VOICE', port='vmx0.100'") + def test_interface_create_static(self): """test creation of a new interface with a static ip""" interface = dict(descr="VOICE", interface="vmx0.100", ipv4_type="static", ipv4_address="10.20.30.40", ipv4_prefixlen=24) @@ -464,7 +474,10 @@ def test_interface_error_if(self): interface = dict( descr="wlan_1100", interface="vmx1.1200", enable=True, ipv4_type="static", ipv4_address="172.16.151.210", ipv4_prefixlen=24, ipv4_gateway="voice_gw" ) - msg = "vmx1.1200 can't be assigned. Interface may only be one the following: ['vmx0', 'vmx1', 'vmx2', 'vmx3', 'vmx0.100', 'vmx1.1100']" + msg = ( + "vmx1.1200 can't be assigned. Interface may only be one the following: " + "['vmx0', 'vmx1', 'vmx2', 'vmx3', 'vmx0.100', 'vmx1.1100', 'vmx1.100', 'vmx2.1100']" + ) self.do_module_test(interface, failed=True, msg=msg) def test_interface_error_eq(self): From 9ddb4434732248553fbdf8d3cf6fa91aedae716d Mon Sep 17 00:00:00 2001 From: Orion Poplawski Date: Fri, 4 Dec 2026 08:47:24 -0700 Subject: [PATCH 2/3] ruff --- plugins/module_utils/interface.py | 123 +++++++++++++++--------------- 1 file changed, 63 insertions(+), 60 deletions(-) diff --git a/plugins/module_utils/interface.py b/plugins/module_utils/interface.py index 16f1c1a3..7bf06276 100644 --- a/plugins/module_utils/interface.py +++ b/plugins/module_utils/interface.py @@ -436,66 +436,69 @@ def _run_post(self): def _get_interface_list(self): # The configuration is read through config_get_path() when pfSense has it: pfSense Plus can serve its configuration # from a controller (Netgate Nexus), and then leaves $config unset. - interfaces = self.pfsense.php( - "require_once('/etc/inc/interfaces.inc');" - "$cfg = function_exists('config_get_path') ? config_get_path('', []) : $config;" - "$portlist = get_interface_list();" - "" - "/* add wireless clone interfaces */" - "if (is_array($cfg['wireless']) && is_array($cfg['wireless']['clone']) && count($cfg['wireless']['clone']))" - " foreach ($cfg['wireless']['clone'] as $clone) $portlist[$clone['cloneif']] = $clone;" - "" - "/* add VLAN interfaces */" - "if (is_array($cfg['vlans']) && is_array($cfg['vlans']['vlan']) && count($cfg['vlans']['vlan']))" - " foreach ($cfg['vlans']['vlan'] as $vlan) $portlist[$vlan['vlanif']] = $vlan;" - "" - "/* add Bridge interfaces */" - "if (is_array($cfg['bridges']) && is_array($cfg['bridges']['bridged']) && count($cfg['bridges']['bridged']))" - " foreach ($cfg['bridges']['bridged'] as $bridge) $portlist[$bridge['bridgeif']] = $bridge;" - "" - "/* add GIF interfaces */" - "if (is_array($cfg['gifs']) && is_array($cfg['gifs']['gif']) && count($cfg['gifs']['gif']))" - " foreach ($cfg['gifs']['gif'] as $gif) $portlist[$gif['gifif']] = $gif;" - "" - "/* add GRE interfaces */" - "if (is_array($cfg['gres']) && is_array($cfg['gres']['gre']) && count($cfg['gres']['gre']))" - " foreach ($cfg['gres']['gre'] as $gre) $portlist[$gre['greif']] = $gre;" - "" - "/* add LAGG interfaces */" - "if (is_array($cfg['laggs']) && is_array($cfg['laggs']['lagg']) && count($cfg['laggs']['lagg']))" - " foreach ($cfg['laggs']['lagg'] as $lagg) {" - " $portlist[$lagg['laggif']] = $lagg;" - " /* LAGG members cannot be assigned */" - " $lagifs = explode(',', $lagg['members']);" - " foreach ($lagifs as $lagif)" - " if (isset($portlist[$lagif])) unset($portlist[$lagif]);" - " }" - "" - "/* add QinQ interfaces */" - "if (is_array($cfg['qinqs']) && is_array($cfg['qinqs']['qinqentry']) && count($cfg['qinqs']['qinqentry']))" - " foreach ($cfg['qinqs']['qinqentry'] as $qinq) {" - " $portlist[\"{$qinq['vlanif']}\"] = $qinq;" - " /* QinQ members */" - " $qinqifs = explode(' ', $qinq['members']);" - " foreach ($qinqifs as $qinqif) $portlist[\"{$qinq['vlanif']}.{$qinqif}\"] = $qinqif;" - " }" - "" - "/* add PPP interfaces */" - "if (is_array($cfg['ppps']) && is_array($cfg['ppps']['ppp']) && count($cfg['ppps']['ppp']))" - " foreach ($cfg['ppps']['ppp'] as $pppid => $ppp) $portlist[$ppp['if']] = $ppp;" - "" - "if (is_array($cfg['openvpn'])) {" - " if (is_array($cfg['openvpn']['openvpn-server']))" - " foreach ($cfg['openvpn']['openvpn-server'] as $s) $portlist[\"ovpns{$s['vpnid']}\"] = $s;" - " if (is_array($cfg['openvpn']['openvpn-client']))" - " foreach ($cfg['openvpn']['openvpn-client'] as $c) $portlist[\"ovpnc{$c['vpnid']}\"] = $c;" - "}" - "" - "$ipsec_descrs = interface_ipsec_vti_list_all();" - "foreach ($ipsec_descrs as $ifname => $ifdescr) $portlist[$ifname] = array('descr' => $ifdescr);" - "" - "echo json_encode($portlist, JSON_PRETTY_PRINT);" - ) or {} + interfaces = ( + self.pfsense.php( + "require_once('/etc/inc/interfaces.inc');" + "$cfg = function_exists('config_get_path') ? config_get_path('', []) : $config;" + "$portlist = get_interface_list();" + "" + "/* add wireless clone interfaces */" + "if (is_array($cfg['wireless']) && is_array($cfg['wireless']['clone']) && count($cfg['wireless']['clone']))" + " foreach ($cfg['wireless']['clone'] as $clone) $portlist[$clone['cloneif']] = $clone;" + "" + "/* add VLAN interfaces */" + "if (is_array($cfg['vlans']) && is_array($cfg['vlans']['vlan']) && count($cfg['vlans']['vlan']))" + " foreach ($cfg['vlans']['vlan'] as $vlan) $portlist[$vlan['vlanif']] = $vlan;" + "" + "/* add Bridge interfaces */" + "if (is_array($cfg['bridges']) && is_array($cfg['bridges']['bridged']) && count($cfg['bridges']['bridged']))" + " foreach ($cfg['bridges']['bridged'] as $bridge) $portlist[$bridge['bridgeif']] = $bridge;" + "" + "/* add GIF interfaces */" + "if (is_array($cfg['gifs']) && is_array($cfg['gifs']['gif']) && count($cfg['gifs']['gif']))" + " foreach ($cfg['gifs']['gif'] as $gif) $portlist[$gif['gifif']] = $gif;" + "" + "/* add GRE interfaces */" + "if (is_array($cfg['gres']) && is_array($cfg['gres']['gre']) && count($cfg['gres']['gre']))" + " foreach ($cfg['gres']['gre'] as $gre) $portlist[$gre['greif']] = $gre;" + "" + "/* add LAGG interfaces */" + "if (is_array($cfg['laggs']) && is_array($cfg['laggs']['lagg']) && count($cfg['laggs']['lagg']))" + " foreach ($cfg['laggs']['lagg'] as $lagg) {" + " $portlist[$lagg['laggif']] = $lagg;" + " /* LAGG members cannot be assigned */" + " $lagifs = explode(',', $lagg['members']);" + " foreach ($lagifs as $lagif)" + " if (isset($portlist[$lagif])) unset($portlist[$lagif]);" + " }" + "" + "/* add QinQ interfaces */" + "if (is_array($cfg['qinqs']) && is_array($cfg['qinqs']['qinqentry']) && count($cfg['qinqs']['qinqentry']))" + " foreach ($cfg['qinqs']['qinqentry'] as $qinq) {" + " $portlist[\"{$qinq['vlanif']}\"] = $qinq;" + " /* QinQ members */" + " $qinqifs = explode(' ', $qinq['members']);" + " foreach ($qinqifs as $qinqif) $portlist[\"{$qinq['vlanif']}.{$qinqif}\"] = $qinqif;" + " }" + "" + "/* add PPP interfaces */" + "if (is_array($cfg['ppps']) && is_array($cfg['ppps']['ppp']) && count($cfg['ppps']['ppp']))" + " foreach ($cfg['ppps']['ppp'] as $pppid => $ppp) $portlist[$ppp['if']] = $ppp;" + "" + "if (is_array($cfg['openvpn'])) {" + " if (is_array($cfg['openvpn']['openvpn-server']))" + " foreach ($cfg['openvpn']['openvpn-server'] as $s) $portlist[\"ovpns{$s['vpnid']}\"] = $s;" + " if (is_array($cfg['openvpn']['openvpn-client']))" + " foreach ($cfg['openvpn']['openvpn-client'] as $c) $portlist[\"ovpnc{$c['vpnid']}\"] = $c;" + "}" + "" + "$ipsec_descrs = interface_ipsec_vti_list_all();" + "foreach ($ipsec_descrs as $ifname => $ifdescr) $portlist[$ifname] = array('descr' => $ifdescr);" + "" + "echo json_encode($portlist, JSON_PRETTY_PRINT);" + ) + or {} + ) # Aggregate VLAN additions are staged in XML and applied before interface setup. for vlan in self.pfsense.get_elements("vlans/vlan"): From 5a1d23d9b91d5be9ab4945eedd9d52b558c0ffb1 Mon Sep 17 00:00:00 2001 From: Orion Poplawski Date: Fri, 4 Dec 2026 08:48:24 -0700 Subject: [PATCH 3/3] [interface] Speedup aggregated interface runs --- plugins/module_utils/interface.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/plugins/module_utils/interface.py b/plugins/module_utils/interface.py index 7bf06276..5e042911 100644 --- a/plugins/module_utils/interface.py +++ b/plugins/module_utils/interface.py @@ -67,6 +67,7 @@ def __init__(self, module, pfsense=None): self.obj = dict() self.root_elt = self.pfsense.interfaces + self.interface_list = None self.setup_interface_cmds = "" self.setup_interface_pre_cmds = "" self.gateways_changed = False @@ -180,7 +181,10 @@ def _validate_params(self): if params.get("mss") is not None and (params["mss"] < 576 or params["mss"] > 65535): self.module.fail_json(msg="mss must be between 576 and 65535.") - interfaces = self._get_interface_list() + # The aggregate stages all VLANs before validating interfaces. + if self.interface_list is None: + self.interface_list = self._get_interface_list() + interfaces = self.interface_list if params.get("interface") is not None: if params["interface"] not in interfaces.keys(): self.module.fail_json( @@ -202,10 +206,12 @@ def _validate_params(self): if params["mtu"] < min_mtu or params["mtu"] > max_mtu: self.module.fail_json(msg="mtu must be between {0} and {1}.".format(min_mtu, max_mtu)) - media_modes = set(self._get_media_mode(params["interface"])) - media_modes.add("autoselect") - if params.get("speed_duplex") and params["speed_duplex"] not in media_modes: - self.module.fail_json(msg="For this interface, media mode may only be one the following: {0}".format(media_modes)) + speed_duplex = params.get("speed_duplex") + if speed_duplex and speed_duplex != "autoselect": + media_modes = set(self._get_media_mode(params["interface"])) + if speed_duplex not in media_modes: + media_modes.add("autoselect") + self.module.fail_json(msg="For this interface, media mode may only be one the following: {0}".format(media_modes)) if params["ipv4_type"] == "static": if params.get("ipv4_address") and not self.pfsense.is_ipv4_address(params["ipv4_address"]):