From 5adb7ab948d79fba6f7c7a335b6fce3865813270 Mon Sep 17 00:00:00 2001 From: Felix Fontein Date: Sun, 26 Nov 2023 19:34:13 +0100 Subject: [PATCH] interfaces_file: filter by address_familiy when updating method (#7612) * When updating method, check address_family if provided. * Also test modifying 'method' without address_family filter. --- .../fragments/7612-interface_file-method.yml | 2 + plugins/modules/interfaces_file.py | 2 + .../targets/interfaces_file/tasks/main.yml | 58 +++++++++++++++++++ 3 files changed, 62 insertions(+) create mode 100644 changelogs/fragments/7612-interface_file-method.yml diff --git a/changelogs/fragments/7612-interface_file-method.yml b/changelogs/fragments/7612-interface_file-method.yml new file mode 100644 index 0000000000..38fcb71503 --- /dev/null +++ b/changelogs/fragments/7612-interface_file-method.yml @@ -0,0 +1,2 @@ +bugfixes: + - "interface_files - also consider ``address_family`` when changing ``option=method`` (https://github.com/ansible-collections/community.general/issues/7610, https://github.com/ansible-collections/community.general/pull/7612)." diff --git a/plugins/modules/interfaces_file.py b/plugins/modules/interfaces_file.py index 423f7efe81..98103082ec 100644 --- a/plugins/modules/interfaces_file.py +++ b/plugins/modules/interfaces_file.py @@ -340,6 +340,8 @@ def addOptionAfterLine(option, value, iface, lines, last_line_dict, iface_option changed = False for ln in lines: if ln.get('line_type', '') == 'iface' and ln.get('iface', '') == iface and value != ln.get('params', {}).get('method', ''): + if address_family is not None and ln.get('address_family') != address_family: + continue changed = True ln['line'] = re.sub(ln.get('params', {}).get('method', '') + '$', value, ln.get('line')) ln['params']['method'] = value diff --git a/tests/integration/targets/interfaces_file/tasks/main.yml b/tests/integration/targets/interfaces_file/tasks/main.yml index 918a323319..18af12f5a2 100644 --- a/tests/integration/targets/interfaces_file/tasks/main.yml +++ b/tests/integration/targets/interfaces_file/tasks/main.yml @@ -7,6 +7,7 @@ set_fact: interfaces_testfile: '{{ remote_tmp_dir }}/interfaces' interfaces_testfile_3841: '{{ remote_tmp_dir }}/interfaces_3841' + interfaces_testfile_7610: '{{ remote_tmp_dir }}/interfaces_7610' - name: Copy interfaces file copy: @@ -65,3 +66,60 @@ that: - ifile_3841_a is changed - ifile_3841_b is not changed + +- name: 7610 - create file + copy: + dest: '{{ interfaces_testfile_7610 }}' + content: | + iface ens3 inet dhcp + iface ens3 inet6 auto + +- name: 7610 - modify file + interfaces_file: + dest: '{{ interfaces_testfile_7610 }}' + iface: ens3 + address_family: "inet6" + option: "{{ item.option }}" + value: "{{ item.value }}" + loop: + - option: "method" + value: "static" + - option: "address" + value: "1:2::3/48" + +- name: 7610 - read file + slurp: + src: '{{ interfaces_testfile_7610 }}' + register: content_7610 + +- name: 7610 - check assertions + assert: + that: + - content_7610.content | b64decode == expected_content + vars: + expected_content: | + iface ens3 inet dhcp + iface ens3 inet6 static + address 1:2::3/48 + +- name: 7610 - modify file again + interfaces_file: + dest: '{{ interfaces_testfile_7610 }}' + iface: ens3 + option: method + value: foobar + +- name: 7610 - read file + slurp: + src: '{{ interfaces_testfile_7610 }}' + register: content_7610 + +- name: 7610 - check assertions + assert: + that: + - content_7610.content | b64decode == expected_content + vars: + expected_content: | + iface ens3 inet foobar + iface ens3 inet6 foobar + address 1:2::3/48