diff options
5 files changed, 87 insertions, 9 deletions
diff --git a/changelogs/fragments/91-new-interface.yaml b/changelogs/fragments/91-new-interface.yaml new file mode 100644 index 0000000..b8bba25 --- /dev/null +++ b/changelogs/fragments/91-new-interface.yaml @@ -0,0 +1,3 @@ +--- +bugfixes: + - Enable configuring an interface which is not present in the running config. diff --git a/plugins/module_utils/network/vyos/config/interfaces/interfaces.py b/plugins/module_utils/network/vyos/config/interfaces/interfaces.py index 86008e8..484e600 100644 --- a/plugins/module_utils/network/vyos/config/interfaces/interfaces.py +++ b/plugins/module_utils/network/vyos/config/interfaces/interfaces.py @@ -161,10 +161,10 @@ class Interfaces(ConfigBase): else: for item in want: name = item["name"] + enable_state = item["enabled"] obj_in_have = search_obj_in_list(name, have) - if not obj_in_have: - obj_in_have = {"name": name} + obj_in_have = {"name": name, "enabled": enable_state} if self.state in ("merged", "rendered"): commands.extend(self._state_merged(item, obj_in_have)) @@ -207,6 +207,11 @@ class Interfaces(ConfigBase): for intf in want: intf_in_have = search_obj_in_list(intf["name"], have) + if not intf_in_have: + intf_in_have = { + "name": intf["name"], + "enabled": intf["enabled"], + } commands.extend(self._state_replaced(intf, intf_in_have)) return commands @@ -271,7 +276,6 @@ class Interfaces(ConfigBase): want_copy = deepcopy(remove_empties(want)) have_copy = deepcopy(have) - want_vifs = want_copy.pop("vifs", []) have_vifs = have_copy.pop("vifs", []) @@ -321,7 +325,6 @@ class Interfaces(ConfigBase): vif=want_vif["vlan_id"], ) ) - return commands def _compute_commands( diff --git a/tests/integration/targets/vyos_interfaces/vars/main.yaml b/tests/integration/targets/vyos_interfaces/vars/main.yaml index 84a8bf0..83d0e59 100644 --- a/tests/integration/targets/vyos_interfaces/vars/main.yaml +++ b/tests/integration/targets/vyos_interfaces/vars/main.yaml @@ -157,16 +157,13 @@ rendered: commands: - set interfaces ethernet eth0 duplex 'auto' - set interfaces ethernet eth0 speed 'auto' - - delete interfaces ethernet eth0 disable - set interfaces ethernet eth1 duplex 'auto' - - delete interfaces ethernet eth1 disable - set interfaces ethernet eth1 speed 'auto' - set interfaces ethernet eth1 description 'Configured by Ansible - Interface 1' - set interfaces ethernet eth1 mtu '1500' - set interfaces ethernet eth1 vif 100 description 'Eth1 - VIF 100' - set interfaces ethernet eth1 vif 100 mtu '400' - set interfaces ethernet eth1 vif 101 description 'Eth1 - VIF 101' - - set interfaces ethernet eth2 disable - set interfaces ethernet eth2 description 'Configured by Ansible - Interface 2 (ADMIN DOWN)' - set interfaces ethernet eth2 mtu '600' diff --git a/tests/unit/modules/network/vyos/fixtures/vyos_interfaces_config.cfg b/tests/unit/modules/network/vyos/fixtures/vyos_interfaces_config.cfg index 0876916..90f120c 100644 --- a/tests/unit/modules/network/vyos/fixtures/vyos_interfaces_config.cfg +++ b/tests/unit/modules/network/vyos/fixtures/vyos_interfaces_config.cfg @@ -3,4 +3,5 @@ set interfaces ethernet eth0 hw-id '08:00:27:7c:85:05' set interfaces ethernet eth1 description 'test-interface' set interfaces ethernet eth2 hw-id '08:00:27:04:85:99' set interfaces ethernet eth3 hw-id '08:00:27:1c:82:d1' +set interfaces ethernet eth3 description 'Ethernet 3' set interfaces loopback 'lo' diff --git a/tests/unit/modules/network/vyos/test_vyos_interfaces.py b/tests/unit/modules/network/vyos/test_vyos_interfaces.py index 9b832a0..40770df 100644 --- a/tests/unit/modules/network/vyos/test_vyos_interfaces.py +++ b/tests/unit/modules/network/vyos/test_vyos_interfaces.py @@ -93,8 +93,82 @@ class TestVyosFirewallInterfacesModule(TestVyosModule): commands = [ "set interfaces bonding bond1 description 'Bond - 1'", - "delete interfaces bonding bond1 disable", "set interfaces openvpn vtun1 description 'vtun - 1'", - "delete interfaces openvpn vtun1 disable", + ] + self.execute_module(changed=True, commands=commands) + + def test_vyos_interfaces_merged_newinterface(self): + set_module_args( + dict( + config=[ + dict( + name="eth4", + description="Ethernet 4", + enabled=True, + speed="auto", + duplex="auto", + ), + dict(name="eth1", description="Configured by Ansible"), + ], + state="merged", + ) + ) + + commands = [ + "set interfaces ethernet eth1 description 'Configured by Ansible'", + "set interfaces ethernet eth4 description 'Ethernet 4'", + "set interfaces ethernet eth4 duplex 'auto'", + "set interfaces ethernet eth4 speed 'auto'", + ] + self.execute_module(changed=True, commands=commands) + + def test_vyos_interfaces_replaced_newinterface(self): + set_module_args( + dict( + config=[ + dict( + name="eth4", + description="Ethernet 4", + enabled=True, + speed="auto", + duplex="auto", + ), + dict(name="eth1", description="Configured by Ansible"), + ], + state="replaced", + ) + ) + + commands = [ + "set interfaces ethernet eth1 description 'Configured by Ansible'", + "set interfaces ethernet eth4 description 'Ethernet 4'", + "set interfaces ethernet eth4 duplex 'auto'", + "set interfaces ethernet eth4 speed 'auto'", + ] + self.execute_module(changed=True, commands=commands) + + def test_vyos_interfaces_overridden_newinterface(self): + set_module_args( + dict( + config=[ + dict( + name="eth4", + description="Ethernet 4", + enabled=True, + speed="auto", + duplex="auto", + ), + dict(name="eth1", description="Configured by Ansible"), + ], + state="overridden", + ) + ) + + commands = [ + "set interfaces ethernet eth1 description 'Configured by Ansible'", + "set interfaces ethernet eth4 description 'Ethernet 4'", + "set interfaces ethernet eth4 duplex 'auto'", + "set interfaces ethernet eth4 speed 'auto'", + "delete interfaces ethernet eth3 description", ] self.execute_module(changed=True, commands=commands) |