From 4a0c875447feb6f7e822a57f13c4bfb545042d18 Mon Sep 17 00:00:00 2001 From: Albert Siersema Date: Thu, 23 Jan 2020 16:49:15 +0100 Subject: [PATCH] Working Cisco IOS macro editing (fixes #55211) (#55220) * Working Cisco IOS macro editing (fixes #55211) * Working Cisco IOS macro editing [fixed pylints] (fixes #55211) * Added integration tests for edit_macro * Added conditional integration tests for ios_config macro * Cosmetic fix: when near block for added conditional integration tests for ios_config macro * Move teardown to always for block --- lib/ansible/plugins/cliconf/ios.py | 27 ++++++-- .../ios_config/templates/basic/macro.j2 | 8 +++ .../targets/ios_config/tests/cli/macro.yaml | 66 +++++++++++++++++++ 3 files changed, 94 insertions(+), 7 deletions(-) create mode 100644 test/integration/targets/ios_config/templates/basic/macro.j2 create mode 100644 test/integration/targets/ios_config/tests/cli/macro.yaml diff --git a/lib/ansible/plugins/cliconf/ios.py b/lib/ansible/plugins/cliconf/ios.py index 6b6242c54c2..c057a8386ce 100644 --- a/lib/ansible/plugins/cliconf/ios.py +++ b/lib/ansible/plugins/cliconf/ios.py @@ -161,22 +161,35 @@ class Cliconf(CliconfBase): return resp def edit_macro(self, candidate=None, commit=True, replace=None, comment=None): + """ + ios_config: + lines: "{{ macro_lines }}" + parents: "macro name {{ macro_name }}" + after: '@' + match: line + replace: block + """ resp = {} operations = self.get_device_operations() - self.check_edit_config_capabiltiy(operations, candidate, commit, replace, comment) + self.check_edit_config_capability(operations, candidate, commit, replace, comment) results = [] requests = [] if commit: commands = '' + self.send_command('config terminal') + time.sleep(0.1) + # first item: macro command + commands += (candidate.pop(0) + '\n') + multiline_delimiter = candidate.pop(-1) for line in candidate: - if line != 'None': - commands += (' ' + line + '\n') - self.send_command('config terminal', sendonly=True) - obj = {'command': commands, 'sendonly': True} - results.append(self.send_command(**obj)) - requests.append(commands) + commands += (' ' + line + '\n') + commands += (multiline_delimiter + '\n') + obj = {'command': commands, 'sendonly': True} + results.append(self.send_command(**obj)) + requests.append(commands) + time.sleep(0.1) self.send_command('end', sendonly=True) time.sleep(0.1) results.append(self.send_command('\n')) diff --git a/test/integration/targets/ios_config/templates/basic/macro.j2 b/test/integration/targets/ios_config/templates/basic/macro.j2 new file mode 100644 index 00000000000..44a7a9bdbd5 --- /dev/null +++ b/test/integration/targets/ios_config/templates/basic/macro.j2 @@ -0,0 +1,8 @@ + A12345678 123456789 123456789 123456789 123456789 123456789 123456789 + B12345678 123456789 123456789 123456789 123456789 123456789 123456789 + C12345678 123456789 123456789 123456789 123456789 123456789 123456789 + D12345678 123456789 123456789 123456789 123456789 123456789 123456789 + E12345678 123456789 123456789 123456789 123456789 123456789 123456789 + F12345678 123456789 123456789 123456789 123456789 123456789 123456789 + G12345678 123456789 123456789 123456789 123456789 123456789 123456789 + H12345678 123456789 123456789 123456789 123456789 123456789 123456789 diff --git a/test/integration/targets/ios_config/tests/cli/macro.yaml b/test/integration/targets/ios_config/tests/cli/macro.yaml new file mode 100644 index 00000000000..b526f47dc5c --- /dev/null +++ b/test/integration/targets/ios_config/tests/cli/macro.yaml @@ -0,0 +1,66 @@ +--- + +- debug: msg="START cli/cli_macro.yaml on connection={{ ansible_connection }}" + +- name: "Check for macro support" + ios_command: + commands: + - "show parser macro brief" + register: supports_macro + ignore_errors: yes + +- name: "ios_config macro integration tests" + when: supports_macro is succeeded + block: + + - name: "Define macro name" + set_fact: + macro_name: 'MACRO_ANSIBLE_TEST' + + - name: "setup - remove configuration" + ios_config: + lines: + - 'no macro name {{ macro_name }}' + - 'do show clock' + match: none + + - name: "configure macro" + ios_config: + parents: "macro name {{ macro_name }}" + # before: [ 'no macro name ...'] + multiline_delimiter: '@' + after: '@' + match: line + replace: block + lines: "{{ lookup('template', 'basic/macro.j2') }}" + register: result + + - name: "Check if macro has been added" + assert: + that: + - "result.changed == true" + + - name: "configure macro again - idempotent check" + ios_config: + parents: "macro name {{ macro_name }}" + multiline_delimiter: '@' + after: '@' + match: line + replace: block + lines: "{{ lookup('template', 'basic/macro.j2') }}" + register: result + + - name: "macro already/correctly configured ?" + assert: + that: + - "result.changed == false" + + always: + - name: "teardown" + ios_config: + lines: + - "no macro name {{ macro_name }}" + - 'do show clock' + match: none + +- debug: msg="END cli/cli_macro.yaml on connection={{ ansible_connection }}"