From 3dd5b0d34317aa8d409017bbf053e3f3601598c2 Mon Sep 17 00:00:00 2001 From: Alexei Znamensky <103110+russoz@users.noreply.github.com> Date: Wed, 8 Dec 2021 09:54:54 +1300 Subject: [PATCH] interfaces_file - fixed dup options bug (#3862) * interfaces_file - fixed dup options bug * added changelog fragment --- .../3862-interfaces-file-fix-dup-option.yaml | 2 ++ plugins/modules/system/interfaces_file.py | 2 +- .../interfaces_file/files/interfaces_ff_3841 | 7 +++++ .../targets/interfaces_file/tasks/main.yml | 30 +++++++++++++++++++ 4 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 changelogs/fragments/3862-interfaces-file-fix-dup-option.yaml create mode 100644 tests/integration/targets/interfaces_file/files/interfaces_ff_3841 diff --git a/changelogs/fragments/3862-interfaces-file-fix-dup-option.yaml b/changelogs/fragments/3862-interfaces-file-fix-dup-option.yaml new file mode 100644 index 0000000000..54cf17bf7d --- /dev/null +++ b/changelogs/fragments/3862-interfaces-file-fix-dup-option.yaml @@ -0,0 +1,2 @@ +bugfixes: + - interfaces_file - fixed the check for existing option in interface (https://github.com/ansible-collections/community.general/issues/3841). diff --git a/plugins/modules/system/interfaces_file.py b/plugins/modules/system/interfaces_file.py index 7666ba1cbc..91cf74b426 100644 --- a/plugins/modules/system/interfaces_file.py +++ b/plugins/modules/system/interfaces_file.py @@ -253,7 +253,7 @@ def set_interface_option(module, lines, iface, option, raw_value, state, address last_line_dict = iface_lines[-1] return add_option_after_line(option, value, iface, lines, last_line_dict, iface_options, address_family) - if option in ["pre-up", "up", "down", "post-up"] and all(ito for ito in target_options if ito['value'] != value): + if option in ["pre-up", "up", "down", "post-up"] and all(ito['value'] != value for ito in target_options): return add_option_after_line(option, value, iface, lines, target_options[-1], iface_options, address_family) # if more than one option found edit the last one diff --git a/tests/integration/targets/interfaces_file/files/interfaces_ff_3841 b/tests/integration/targets/interfaces_file/files/interfaces_ff_3841 new file mode 100644 index 0000000000..06f78dd74f --- /dev/null +++ b/tests/integration/targets/interfaces_file/files/interfaces_ff_3841 @@ -0,0 +1,7 @@ +iface eth0 inet static + address 1.2.3.4 + netmask 255.255.255.0 + gateway 1.2.3.1 + up route add -net 1.2.3.4 netmask 255.255.255.0 gw 1.2.3.1 eth0 + up ip addr add 4.3.2.1/32 dev eth0 + down ip addr add 4.3.2.1/32 dev eth0 diff --git a/tests/integration/targets/interfaces_file/tasks/main.yml b/tests/integration/targets/interfaces_file/tasks/main.yml index 2a39076e53..5dbd983379 100644 --- a/tests/integration/targets/interfaces_file/tasks/main.yml +++ b/tests/integration/targets/interfaces_file/tasks/main.yml @@ -2,6 +2,7 @@ - name: set_fact: interfaces_testfile: '{{ remote_tmp_dir }}/interfaces' + interfaces_testfile_3841: '{{ remote_tmp_dir }}/interfaces_3841' - name: Copy interfaces file copy: @@ -31,3 +32,32 @@ - assert: that: - ifile_2 is not changed + +- name: 3841 - copy interfaces file + copy: + src: 'files/interfaces_ff_3841' + dest: '{{ interfaces_testfile_3841 }}' + +- name: 3841 - floating_ip_interface_up_ip 2a01:a:b:c::1/64 dev eth0 + interfaces_file: + option: up + iface: eth0 + dest: "{{ interfaces_testfile_3841 }}" + value: 'ip addr add 2a01:a:b:c::1/64 dev eth0' + state: present + register: ifile_3841_a + +- name: 3841 - floating_ip_interface_up_ip 2a01:a:b:c::1/64 dev eth0 (again) + interfaces_file: + option: up + iface: eth0 + dest: "{{ interfaces_testfile_3841 }}" + value: 'ip addr add 2a01:a:b:c::1/64 dev eth0' + state: present + register: ifile_3841_b + +- name: 3841 - check assertions + assert: + that: + - ifile_3841_a is changed + - ifile_3841_b is not changed