Cherry pick 2.4 eos banner fixes (#26904)

* Fix multiple EOS EAPI code and test issues (#26651)

(cherry picked from commit b81209c187)

* Fix eos_banner basic-motd eapi asserts (#23398)

The commands in EAPI does not contain the plain command sent to the
device as a one liner, but it is split in cmd/input keys.
(cherry picked from commit ce9826d76e)

* Add CHANGELOG entry
pull/26935/head
Ricardo Carrillo Cruz 7 years ago committed by GitHub
parent 85e9a07117
commit 32b4342209

@ -46,6 +46,7 @@ Ansible Changes By Release
* fix for win_chocolatey where it did not fail if the version set did not exist * fix for win_chocolatey where it did not fail if the version set did not exist
* fix for win_regedit always changing a reg key if the dword values set is a hex * fix for win_regedit always changing a reg key if the dword values set is a hex
* fix for wait_for on non-Linux systems with newer versions of psutil * fix for wait_for on non-Linux systems with newer versions of psutil
* fix eos_banner code and test issues
## 2.3.1 "Ramble On" - 2017-06-01 ## 2.3.1 "Ramble On" - 2017-06-01

@ -90,27 +90,37 @@ session_name:
from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.eos import load_config, run_commands 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.eos import eos_argument_spec, check_args
from ansible.module_utils._text import to_text
def map_obj_to_commands(updates, module): def map_obj_to_commands(updates, module):
commands = list() commands = list()
want, have = updates want, have = updates
state = module.params['state'] state = module.params['state']
if state == 'absent' and 'text' in have.keys() and have['text']: if state == 'absent' and have.get('text'):
if isinstance(have['text'], str):
commands.append('no banner %s' % module.params['banner']) 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': elif state == 'present':
if want['text'] and (want['text'] != have.get('text')): if isinstance(have['text'], str):
if module.params['transport'] == 'cli': if want['text'] != have['text']:
commands.append('banner %s' % module.params['banner']) commands.append('banner %s' % module.params['banner'])
commands.extend(want['text'].strip().split('\n')) commands.extend(want['text'].strip().split('\n'))
commands.append('EOF') 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 # For EAPI we need to construct a dict with cmd/input
# key/values for the banner # key/values for the banner
commands.append({'cmd': 'banner %s' % module.params['banner'], commands.append({'cmd': 'banner %s' % module.params['banner'],
'input': want['text'].strip('\n')}) 'input': want['text'].strip('\n')})
return commands return commands
def map_config_to_obj(module): def map_config_to_obj(module):

@ -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: eos_banner:
banner: login banner: login
state: absent text: |
Junk login banner
over multiple lines
state: present
authorize: yes authorize: yes
provider: "{{ cli }}" 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: eos_banner:
banner: login banner: login
text: | text: |
this is my login banner Junk login banner
that has a multiline over multiple lines
string
state: present state: present
authorize: yes authorize: yes
provider: "{{ cli }}" provider: "{{ cli }}"
register: result register: result
- debug: - assert:
msg: "{{ result }}" 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: - assert:
that: that:
- "result.changed == true" - "result.changed == true"
- "'this is my login banner' in result.commands" - "'no banner login' in result.commands" # does this break due to "contains?"
- "'that has a multiline' in result.commands"
# Ensure sessions contains epoc. Will fail after 18th May 2033 # Ensure sessions contains epoc. Will fail after 18th May 2033
- "'ansible_1' in result.session_name" - "'ansible_1' in result.session_name"
- name: Set login again (idempotent) - name: remove login (idempotent)
eos_banner: eos_banner:
banner: login banner: login
text: | state: absent
this is my login banner
that has a multiline
string
state: present
authorize: yes authorize: yes
provider: "{{ cli }}" provider: "{{ cli }}"
register: result register: result

@ -1,13 +1,11 @@
--- ---
- name: Remove previous motd banner
- name: setup - remove motd eos_config:
eos_banner: lines: no banner motd
banner: motd
state: absent
authorize: yes authorize: yes
provider: "{{ cli }}" provider: "{{ cli }}"
- name: Set motd - name: Create motd
eos_banner: eos_banner:
banner: motd banner: motd
text: | text: |
@ -19,9 +17,6 @@
provider: "{{ cli }}" provider: "{{ cli }}"
register: result register: result
- debug:
msg: "{{ result }}"
- assert: - assert:
that: that:
- "result.changed == true" - "result.changed == true"
@ -30,7 +25,7 @@
# Ensure sessions contains epoc. Will fail after 18th May 2033 # Ensure sessions contains epoc. Will fail after 18th May 2033
- "'ansible_1' in result.session_name" - "'ansible_1' in result.session_name"
- name: Set motd again (idempotent) - name: Create motd again (idempotent)
eos_banner: eos_banner:
banner: motd banner: motd
text: | text: |
@ -49,6 +44,43 @@
# Ensure sessions contains epoc. Will fail after 18th May 2033 # Ensure sessions contains epoc. Will fail after 18th May 2033
- "result.session_name is not defined" - "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 add in tests for everything defined in docs
# FIXME Test state:absent + test: # FIXME Test state:absent + test:

@ -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"

@ -1,9 +1,8 @@
--- ---
- name: setup - remove login - name: Remove previous login banner
eos_banner: eos_config:
banner: login lines: no banner login
state: absent
authorize: yes authorize: yes
provider: "{{ eapi }}" provider: "{{ eapi }}"
@ -19,9 +18,6 @@
provider: "{{ eapi }}" provider: "{{ eapi }}"
register: result register: result
- debug:
msg: "{{ result }}"
- assert: - assert:
that: that:
- "result.changed == true" - "result.changed == true"
@ -49,6 +45,43 @@
# Ensure sessions contains epoc. Will fail after 18th May 2033 # Ensure sessions contains epoc. Will fail after 18th May 2033
- "result.session_name is not defined" - "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 add in tests for everything defined in docs
# FIXME Test state:absent + test: # FIXME Test state:absent + test:

@ -1,9 +1,8 @@
--- ---
- name: setup - remove motd - name: Remove previous motd banner
eos_banner: eos_config:
banner: motd lines: no banner motd
state: absent
authorize: yes authorize: yes
provider: "{{ eapi }}" provider: "{{ eapi }}"
@ -19,14 +18,11 @@
provider: "{{ eapi }}" provider: "{{ eapi }}"
register: result register: result
- debug:
msg: "{{ result }}"
- assert: - assert:
that: that:
- "result.changed == true" - "result.changed == true"
- "'this is my motd banner' in result.commands" - "result.commands.0.cmd == 'banner motd'"
- "'that has a multiline' in result.commands" - "result.commands.0.input == 'this is my motd banner\nthat has a multiline\nstring'"
# Ensure sessions contains epoc. Will fail after 18th May 2033 # Ensure sessions contains epoc. Will fail after 18th May 2033
- "'ansible_1' in result.session_name" - "'ansible_1' in result.session_name"
@ -49,6 +45,43 @@
# Ensure sessions contains epoc. Will fail after 18th May 2033 # Ensure sessions contains epoc. Will fail after 18th May 2033
- "result.session_name is not defined" - "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 add in tests for everything defined in docs
# FIXME Test state:absent + test: # FIXME Test state:absent + test:
# FIXME Without powers ensure "privileged mode required" # FIXME Without powers ensure "privileged mode required"

@ -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"

@ -53,12 +53,6 @@ class TestEosBannerModule(TestEosModule):
commands = ['banner login', 'test', 'banner', 'string', 'EOF'] commands = ['banner login', 'test', 'banner', 'string', 'EOF']
self.execute_module(changed=True, commands=commands) 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): def test_eos_banner_remove_with_cli_transport(self):
set_module_args(dict(banner='login', state='absent', transport='cli')) set_module_args(dict(banner='login', state='absent', transport='cli'))
commands = ['no banner login'] commands = ['no banner login']

Loading…
Cancel
Save