diff --git a/changelogs/fragments/fix_config_module.yaml b/changelogs/fragments/fix_config_module.yaml new file mode 100644 index 000000000..d7c1c79db --- /dev/null +++ b/changelogs/fragments/fix_config_module.yaml @@ -0,0 +1,3 @@ +--- +minor_changes: + - "nxos_config - Relax restrictions on I(src) parameter so it can be used more like I(lines). (https://github.com/ansible-collections/cisco.nxos/issues/89)." diff --git a/plugins/modules/nxos_config.py b/plugins/modules/nxos_config.py index 8cccae68f..132a62d28 100644 --- a/plugins/modules/nxos_config.py +++ b/plugins/modules/nxos_config.py @@ -415,9 +415,9 @@ def main(): mutually_exclusive = [("lines", "src", "replace_src"), ("parents", "src")] required_if = [ - ("match", "strict", ["lines"]), - ("match", "exact", ["lines"]), - ("replace", "block", ["lines"]), + ("match", "strict", ["lines", "src"], True), + ("match", "exact", ["lines", "src"], True), + ("replace", "block", ["lines", "src"], True), ("replace", "config", ["replace_src"]), ("diff_against", "intended", ["intended_config"]), ] diff --git a/tests/integration/targets/nxos_config/templates/basic/acl_config.j2 b/tests/integration/targets/nxos_config/templates/basic/acl_config.j2 new file mode 100644 index 000000000..ec03c24a2 --- /dev/null +++ b/tests/integration/targets/nxos_config/templates/basic/acl_config.j2 @@ -0,0 +1,5 @@ +ip access-list test + 10 permit ip 192.0.2.1/32 any log + 20 permit ip 192.0.2.2/32 any log + 30 permit ip 192.0.2.3/32 any log + 40 permit ip 192.0.2.4/32 any log diff --git a/tests/integration/targets/nxos_config/tests/cli/replace_block.yaml b/tests/integration/targets/nxos_config/tests/cli/replace_block.yaml new file mode 100644 index 000000000..deb79b940 --- /dev/null +++ b/tests/integration/targets/nxos_config/tests/cli/replace_block.yaml @@ -0,0 +1,35 @@ +--- +- ansible.builtin.debug: msg="START cli/replace_block.yaml on connection={{ ansible_connection }}" + +- name: "setup" + cisco.nxos.nxos_config: + lines: + - "no ip access-list test" + ignore_errors: true + +- name: "Populate nxos acls configuration with replace block and lines options" + register: result1 + cisco.nxos.nxos_config: + lines: "{{ lookup('template', 'basic/acl_config.j2') }}" + replace: block + +- ansible.builtin.assert: + that: + - result1.changed == true + +- name: "setup" + cisco.nxos.nxos_config: + lines: + - "no ip access-list test" + ignore_errors: true + +- name: "Populate acl configuration with replace block and src options" + register: result2 + cisco.nxos.nxos_config: + src: basic/acl_config.j2 + replace: block + +- ansible.builtin.assert: + that: + - result2.changed == true + - result1.commands == result2.commands diff --git a/tests/unit/modules/network/nxos/test_nxos_config.py b/tests/unit/modules/network/nxos/test_nxos_config.py index 83e58722a..ce5c987c7 100644 --- a/tests/unit/modules/network/nxos/test_nxos_config.py +++ b/tests/unit/modules/network/nxos/test_nxos_config.py @@ -181,6 +181,47 @@ def test_nxos_config_parents(self): self.assertEqual(config, result["commands"], result["commands"]) + def test_nxos_replace_block_src(self): + src = load_fixture("nxos_config", "candidate.cfg") + args = dict(src=src, replace="block") + self.conn.get_diff = MagicMock( + return_value=self.cliconf_obj.get_diff(src, self.running_config), + ) + set_module_args(args) + + result = self.execute_module(changed=True) + config = [ + "hostname switch01", + "interface Ethernet1", + "description test interface", + "no shutdown", + "ip routing", + ] + + self.assertEqual(sorted(config), sorted(result["commands"]), result["commands"]) + + def test_nxos_replace_block_lines(self): + lines = ["ip address 1.2.3.4/5", "no shutdown"] + parents = ["interface Ethernet10"] + args = dict(lines=lines, parents=parents, replace="block") + self.conn.get_diff = MagicMock( + return_value=self.cliconf_obj.get_diff( + "\n".join(parents + lines), + self.running_config, + path=parents, + ), + ) + set_module_args(args) + + result = self.execute_module(changed=True) + config = [ + "interface Ethernet10", + "ip address 1.2.3.4/5", + "no shutdown", + ] + + self.assertEqual(config, result["commands"], result["commands"]) + def test_nxos_config_src_and_lines_fails(self): args = dict(src="foo", lines="foo") set_module_args(args) @@ -201,7 +242,7 @@ def test_nxos_config_match_strict_requires_lines(self): set_module_args(args) result = self.execute_module(failed=True) - def test_nxos_config_replace_block_requires_lines(self): + def test_nxos_config_replace_block_requires_lines_or_src(self): args = dict(replace="block") set_module_args(args) result = self.execute_module(failed=True)