diff --git a/lib/ansible/modules/network/eos/eos_banner.py b/lib/ansible/modules/network/eos/eos_banner.py index 288bc70b5ad..dc0994f37bc 100644 --- a/lib/ansible/modules/network/eos/eos_banner.py +++ b/lib/ansible/modules/network/eos/eos_banner.py @@ -90,27 +90,37 @@ session_name: from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.eos import load_config, run_commands from ansible.module_utils.eos import eos_argument_spec, check_args +from ansible.module_utils._text import to_text def map_obj_to_commands(updates, module): commands = list() want, have = updates state = module.params['state'] - if state == 'absent' and 'text' in have.keys() and have['text']: - commands.append('no banner %s' % module.params['banner']) + if state == 'absent' and have.get('text'): + if isinstance(have['text'], str): + commands.append('no banner %s' % module.params['banner']) + elif have['text'].get('loginBanner') or have['text'].get('motd'): + commands.append({'cmd': 'no banner %s' % module.params['banner']}) elif state == 'present': - if want['text'] and (want['text'] != have.get('text')): - if module.params['transport'] == 'cli': + if isinstance(have['text'], str): + if want['text'] != have['text']: commands.append('banner %s' % module.params['banner']) commands.extend(want['text'].strip().split('\n')) commands.append('EOF') - else: + else: + have_text = have['text'].get('loginBanner') or have['text'].get('motd') + if have_text: + have_text = have_text.strip() + + if to_text(want['text']) != have_text or not have_text: # For EAPI we need to construct a dict with cmd/input # key/values for the banner commands.append({'cmd': 'banner %s' % module.params['banner'], 'input': want['text'].strip('\n')}) + return commands def map_config_to_obj(module): diff --git a/test/integration/targets/eos_banner/tests/cli/basic-login.yaml b/test/integration/targets/eos_banner/tests/cli/basic-login.yaml index 670e09574b2..f8748be57bb 100644 --- a/test/integration/targets/eos_banner/tests/cli/basic-login.yaml +++ b/test/integration/targets/eos_banner/tests/cli/basic-login.yaml @@ -1,43 +1,65 @@ --- +- name: Remove previous login banner + eos_config: + lines: no banner login + authorize: yes + provider: "{{ cli }}" -- name: setup - remove login +- name: Create login banner eos_banner: banner: login - state: absent + text: | + Junk login banner + over multiple lines + state: present authorize: yes provider: "{{ cli }}" + register: result + +- assert: + that: + - "result.changed == true" + - "'banner login' in result.commands[0]" # does this break due to "contains?" + # Ensure sessions contains epoc. Will fail after 18th May 2033 + - "'ansible_1' in result.session_name" -- name: Set login +- name: Create login banner again (idempotent) eos_banner: banner: login text: | - this is my login banner - that has a multiline - string + Junk login banner + over multiple lines state: present authorize: yes provider: "{{ cli }}" register: result -- debug: - msg: "{{ result }}" +- assert: + that: + - "result.changed == false" + - "result.commands | length == 0" + # Ensure sessions contains epoc. Will fail after 18th May 2033 + - "result.session_name is not defined" + +- name: remove login + eos_banner: + banner: login + state: absent + authorize: yes + provider: "{{ cli }}" + register: result - assert: that: - "result.changed == true" - - "'this is my login banner' in result.commands" - - "'that has a multiline' in result.commands" + - "'no banner login' in result.commands" # does this break due to "contains?" # Ensure sessions contains epoc. Will fail after 18th May 2033 - "'ansible_1' in result.session_name" -- name: Set login again (idempotent) +- name: remove login (idempotent) eos_banner: banner: login - text: | - this is my login banner - that has a multiline - string - state: present + state: absent authorize: yes provider: "{{ cli }}" register: result diff --git a/test/integration/targets/eos_banner/tests/cli/basic-motd.yaml b/test/integration/targets/eos_banner/tests/cli/basic-motd.yaml index c79bc089887..859d293d074 100644 --- a/test/integration/targets/eos_banner/tests/cli/basic-motd.yaml +++ b/test/integration/targets/eos_banner/tests/cli/basic-motd.yaml @@ -1,13 +1,11 @@ --- - -- name: setup - remove motd - eos_banner: - banner: motd - state: absent +- name: Remove previous motd banner + eos_config: + lines: no banner motd authorize: yes provider: "{{ cli }}" -- name: Set motd +- name: Create motd eos_banner: banner: motd text: | @@ -19,9 +17,6 @@ provider: "{{ cli }}" register: result -- debug: - msg: "{{ result }}" - - assert: that: - "result.changed == true" @@ -30,7 +25,7 @@ # Ensure sessions contains epoc. Will fail after 18th May 2033 - "'ansible_1' in result.session_name" -- name: Set motd again (idempotent) +- name: Create motd again (idempotent) eos_banner: banner: motd text: | @@ -49,6 +44,43 @@ # Ensure sessions contains epoc. Will fail after 18th May 2033 - "result.session_name is not defined" +- name: Remove motd + eos_banner: + banner: motd + text: | + this is my motd banner + that has a multiline + string + state: absent + authorize: yes + provider: "{{ cli }}" + register: result + +- assert: + that: + - "result.changed == true" + - "'no banner motd' in result.commands" + # Ensure sessions contains epoc. Will fail after 18th May 2033 + - "'ansible_1' in result.session_name" + +- name: Remove motd again (idempotent) + eos_banner: + banner: motd + text: | + this is my motd banner + that has a multiline + string + state: absent + authorize: yes + provider: "{{ cli }}" + register: result + +- assert: + that: + - "result.changed == false" + - "result.commands | length == 0" + # Ensure sessions contains epoc. Will fail after 18th May 2033 + - "result.session_name is not defined" # FIXME add in tests for everything defined in docs # FIXME Test state:absent + test: diff --git a/test/integration/targets/eos_banner/tests/cli/basic-no-login.yaml b/test/integration/targets/eos_banner/tests/cli/basic-no-login.yaml deleted file mode 100644 index c34afb7d9d5..00000000000 --- a/test/integration/targets/eos_banner/tests/cli/basic-no-login.yaml +++ /dev/null @@ -1,48 +0,0 @@ ---- -- name: Setup - eos_banner: - banner: login - text: | - Junk login banner - over multiple lines - state: present - authorize: yes - provider: "{{ cli }}" - -- name: remove login - eos_banner: - banner: login - state: absent - authorize: yes - provider: "{{ cli }}" - register: result - -- debug: - msg: "{{ result }}" - -- assert: - that: - - "result.changed == true" - - "'no banner login' in result.commands" # does this break due to "contains?" - # Ensure sessions contains epoc. Will fail after 18th May 2033 - - "'ansible_1' in result.session_name" - -- name: remove login (idempotent) - eos_banner: - banner: login - state: absent - authorize: yes - provider: "{{ cli }}" - register: result - -- assert: - that: - - "result.changed == false" - - "result.commands | length == 0" - # Ensure sessions contains epoc. Will fail after 18th May 2033 - - "result.session_name is not defined" - - -# FIXME add in tests for everything defined in docs -# FIXME Test state:absent + test: -# FIXME Without powers ensure "privileged mode required" diff --git a/test/integration/targets/eos_banner/tests/eapi/basic-login.yaml b/test/integration/targets/eos_banner/tests/eapi/basic-login.yaml index a7b64d47719..e72fd5c52ae 100644 --- a/test/integration/targets/eos_banner/tests/eapi/basic-login.yaml +++ b/test/integration/targets/eos_banner/tests/eapi/basic-login.yaml @@ -1,9 +1,8 @@ --- -- name: setup - remove login - eos_banner: - banner: login - state: absent +- name: Remove previous login banner + eos_config: + lines: no banner login authorize: yes provider: "{{ eapi }}" @@ -19,9 +18,6 @@ provider: "{{ eapi }}" register: result -- debug: - msg: "{{ result }}" - - assert: that: - "result.changed == true" @@ -49,6 +45,43 @@ # Ensure sessions contains epoc. Will fail after 18th May 2033 - "result.session_name is not defined" +- name: Remove login + eos_banner: + banner: login + text: | + this is my login banner + that has a multiline + string + state: absent + authorize: yes + provider: "{{ eapi }}" + register: result + +- assert: + that: + - "result.changed == true" + - "result.commands.0.cmd == 'no banner login'" + # Ensure sessions contains epoc. Will fail after 18th May 2033 + - "'ansible_1' in result.session_name" + +- name: Remove login again (idempotent) + eos_banner: + banner: login + text: | + this is my login banner + that has a multiline + string + state: absent + authorize: yes + provider: "{{ eapi }}" + register: result + +- assert: + that: + - "result.changed == false" + - "result.commands | length == 0" + # Ensure sessions contains epoc. Will fail after 18th May 2033 + - "result.session_name is not defined" # FIXME add in tests for everything defined in docs # FIXME Test state:absent + test: diff --git a/test/integration/targets/eos_banner/tests/eapi/basic-motd.yaml b/test/integration/targets/eos_banner/tests/eapi/basic-motd.yaml index 08ae54b7cce..7146d37a231 100644 --- a/test/integration/targets/eos_banner/tests/eapi/basic-motd.yaml +++ b/test/integration/targets/eos_banner/tests/eapi/basic-motd.yaml @@ -1,9 +1,8 @@ --- -- name: setup - remove motd - eos_banner: - banner: motd - state: absent +- name: Remove previous motd banner + eos_config: + lines: no banner motd authorize: yes provider: "{{ eapi }}" @@ -19,9 +18,6 @@ provider: "{{ eapi }}" register: result -- debug: - msg: "{{ result }}" - - assert: that: - "result.changed == true" @@ -49,6 +45,43 @@ # Ensure sessions contains epoc. Will fail after 18th May 2033 - "result.session_name is not defined" +- name: Remove motd + eos_banner: + banner: motd + text: | + this is my motd banner + that has a multiline + string + state: absent + authorize: yes + provider: "{{ eapi }}" + register: result + +- assert: + that: + - "result.changed == true" + - "result.commands.0.cmd == 'no banner motd'" + # Ensure sessions contains epoc. Will fail after 18th May 2033 + - "'ansible_1' in result.session_name" + +- name: Remove motd again (idempotent) + eos_banner: + banner: motd + text: | + this is my motd banner + that has a multiline + string + state: absent + authorize: yes + provider: "{{ eapi }}" + register: result + +- assert: + that: + - "result.changed == false" + - "result.commands | length == 0" + # Ensure sessions contains epoc. Will fail after 18th May 2033 + - "result.session_name is not defined" # FIXME add in tests for everything defined in docs # FIXME Test state:absent + test: # FIXME Without powers ensure "privileged mode required" diff --git a/test/integration/targets/eos_banner/tests/eapi/basic-no-login.yaml b/test/integration/targets/eos_banner/tests/eapi/basic-no-login.yaml deleted file mode 100644 index b9b9bf55ea7..00000000000 --- a/test/integration/targets/eos_banner/tests/eapi/basic-no-login.yaml +++ /dev/null @@ -1,46 +0,0 @@ ---- -- name: Setup - eos_banner: - banner: login - text: Junk login banner single line - state: present - authorize: yes - provider: "{{ eapi }}" - -- name: remove login - eos_banner: - banner: login - state: absent - authorize: yes - provider: "{{ eapi }}" - register: result - -- debug: - msg: "{{ result }}" - -- assert: - that: - - "result.changed == true" - - "'no banner login' in result.commands" # does this break due to "contains?" - # Ensure sessions contains epoc. Will fail after 18th May 2033 - - "'ansible_1' in result.session_name" - -- name: remove login (idempotent) - eos_banner: - banner: login - state: absent - authorize: yes - provider: "{{ eapi }}" - register: result - -- assert: - that: - - "result.changed == false" - - "result.commands | length == 0" - # Ensure sessions contains epoc. Will fail after 18th May 2033 - - "result.session_name is not defined" - - -# FIXME add in tests for everything defined in docs -# FIXME Test state:absent + test: -# FIXME Without powers ensure "privileged mode required" diff --git a/test/units/modules/network/eos/test_eos_banner.py b/test/units/modules/network/eos/test_eos_banner.py index 7ea4f3a9071..dc1e3215de2 100644 --- a/test/units/modules/network/eos/test_eos_banner.py +++ b/test/units/modules/network/eos/test_eos_banner.py @@ -53,12 +53,6 @@ class TestEosBannerModule(TestEosModule): commands = ['banner login', 'test', 'banner', 'string', 'EOF'] self.execute_module(changed=True, commands=commands) - def test_eos_banner_create_with_eapi_transport(self): - set_module_args(dict(banner='login', text='test\nbanner\nstring', - transport='eapi')) - commands = [{'cmd': 'banner login', 'input': 'test\nbanner\nstring'}] - self.execute_module(changed=True, commands=commands, transport='eapi') - def test_eos_banner_remove_with_cli_transport(self): set_module_args(dict(banner='login', state='absent', transport='cli')) commands = ['no banner login']