Skip to content

Commit

Permalink
interfaces_file: filter by address_familiy when updating method (#7612)
Browse files Browse the repository at this point in the history
* When updating method, check address_family if provided.

* Also test modifying 'method' without address_family filter.
  • Loading branch information
felixfontein authored Nov 26, 2023
1 parent f496256 commit 5adb7ab
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/7612-interface_file-method.yml
Original file line number Diff line number Diff line change
@@ -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)."
2 changes: 2 additions & 0 deletions plugins/modules/interfaces_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
58 changes: 58 additions & 0 deletions tests/integration/targets/interfaces_file/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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

0 comments on commit 5adb7ab

Please sign in to comment.