From 7aae8d5386e0ddb944f171b1847e3e16e981d635 Mon Sep 17 00:00:00 2001 From: Alexei Znamensky <103110+russoz@users.noreply.github.com> Date: Sun, 19 Sep 2021 23:44:37 +1200 Subject: [PATCH] Interfaces_file - improvements (#3328) * pythonific!! no camel cases, bitte * simplified iface attributes parsing * some improvements, passing tests * simplified set_interface_option() * further simplifications * remove unreachable stmt * pythonified a file open * added changelog fragment * adjustment per PR * PR: fixed the auto- case * PR: added testcase and chglog frag for the misleading change report * extra line removed * integration is not destructive --- .../3328-interfaces_file-improvements.yaml | 4 + plugins/modules/system/interfaces_file.py | 161 ++++++++---------- .../targets/interfaces_file/aliases | 1 + .../interfaces_file/files/interfaces_ff | 7 + .../targets/interfaces_file/tasks/main.yml | 33 ++++ .../interfaces_file/test_interfaces_file.py | 22 +-- 6 files changed, 123 insertions(+), 105 deletions(-) create mode 100644 changelogs/fragments/3328-interfaces_file-improvements.yaml create mode 100644 tests/integration/targets/interfaces_file/aliases create mode 100644 tests/integration/targets/interfaces_file/files/interfaces_ff create mode 100644 tests/integration/targets/interfaces_file/tasks/main.yml diff --git a/changelogs/fragments/3328-interfaces_file-improvements.yaml b/changelogs/fragments/3328-interfaces_file-improvements.yaml new file mode 100644 index 0000000000..10734af603 --- /dev/null +++ b/changelogs/fragments/3328-interfaces_file-improvements.yaml @@ -0,0 +1,4 @@ +bugfixes: + - interfaces_file - no longer reporting change when none happened (https://github.com/ansible-collections/community.general/pull/3328). +minor_changes: + - interfaces_file - minor refactor (https://github.com/ansible-collections/community.general/pull/3328). diff --git a/plugins/modules/system/interfaces_file.py b/plugins/modules/system/interfaces_file.py index c22c0ce29e..7666ba1cbc 100644 --- a/plugins/modules/system/interfaces_file.py +++ b/plugins/modules/system/interfaces_file.py @@ -148,57 +148,48 @@ from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.common.text.converters import to_bytes -def lineDict(line): +def line_dict(line): return {'line': line, 'line_type': 'unknown'} -def optionDict(line, iface, option, value, address_family): +def make_option_dict(line, iface, option, value, address_family): return {'line': line, 'iface': iface, 'option': option, 'value': value, 'line_type': 'option', 'address_family': address_family} -def getValueFromLine(s): - spaceRe = re.compile(r'\s+') - for m in spaceRe.finditer(s): - pass - valueEnd = m.start() - option = s.split()[0] - optionStart = s.find(option) - optionLen = len(option) - valueStart = re.search(r'\s', s[optionLen + optionStart:]).end() + optionLen + optionStart - return s[valueStart:valueEnd] +def get_option_value(line): + patt = re.compile(r'^\s+(?P