[WIP] Fix nxos_banner (#28607)
* Fix `'state': 'absent'` * Replace idempotence tests with references * Fix issues with nxos_banner
This commit is contained in:
parent
0a8ec4ddef
commit
862cde5e82
4 changed files with 16 additions and 46 deletions
|
@ -92,21 +92,15 @@ from ansible.module_utils.nxos import load_config, run_commands
|
|||
from ansible.module_utils.nxos import nxos_argument_spec, check_args
|
||||
|
||||
|
||||
def map_obj_to_commands(updates, module):
|
||||
def map_obj_to_commands(want, have, module):
|
||||
commands = list()
|
||||
want, have = updates
|
||||
state = module.params['state']
|
||||
|
||||
if state == 'absent' or (state == 'absent' and
|
||||
'text' in have.keys() and have['text']):
|
||||
if state == 'absent' and have.get('text'):
|
||||
commands.append('no banner %s' % module.params['banner'])
|
||||
|
||||
elif state == 'present':
|
||||
if want['text'] and (want['text'] != have.get('text')):
|
||||
banner_cmd = 'banner %s' % module.params['banner']
|
||||
banner_cmd += ' @\n'
|
||||
banner_cmd += want['text'].strip()
|
||||
banner_cmd += '\n@'
|
||||
elif state == 'present' and want.get('text') != have.get('text'):
|
||||
banner_cmd = 'banner %s @\n%s\n@' % (module.params['banner'], want['text'].strip())
|
||||
commands.append(banner_cmd)
|
||||
|
||||
return commands
|
||||
|
@ -114,6 +108,9 @@ def map_obj_to_commands(updates, module):
|
|||
|
||||
def map_config_to_obj(module):
|
||||
output = run_commands(module, ['show banner %s' % module.params['banner']])[0]
|
||||
if isinstance(output, dict):
|
||||
output = list(output.values())[0]
|
||||
|
||||
obj = {'banner': module.params['banner'], 'state': 'absent'}
|
||||
if output:
|
||||
obj['text'] = output
|
||||
|
@ -159,7 +156,7 @@ def main():
|
|||
want = map_params_to_obj(module)
|
||||
have = map_config_to_obj(module)
|
||||
|
||||
commands = map_obj_to_commands((want, have), module)
|
||||
commands = map_obj_to_commands(want, have, module)
|
||||
result['commands'] = commands
|
||||
|
||||
if commands:
|
||||
|
@ -169,5 +166,6 @@ def main():
|
|||
|
||||
module.exit_json(**result)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
provider: "{{ connection }}"
|
||||
|
||||
- name: Set exec
|
||||
nxos_banner:
|
||||
nxos_banner: &exec
|
||||
banner: exec
|
||||
text: |
|
||||
this is my exec banner
|
||||
|
@ -16,23 +16,13 @@
|
|||
provider: "{{ connection }}"
|
||||
register: result
|
||||
|
||||
- debug:
|
||||
msg: "{{ result }}"
|
||||
|
||||
- assert:
|
||||
that:
|
||||
- "result.changed == true"
|
||||
- "'banner exec @\nthis is my exec banner\nthat has a multiline\nstring\n@' in result.commands"
|
||||
|
||||
- name: Set exec again (idempotent)
|
||||
nxos_banner:
|
||||
banner: exec
|
||||
text: |
|
||||
this is my exec banner
|
||||
that has a multiline
|
||||
string
|
||||
state: present
|
||||
provider: "{{ connection }}"
|
||||
nxos_banner: *exec
|
||||
register: result
|
||||
|
||||
- assert:
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
provider: "{{ connection }}"
|
||||
|
||||
- name: Set motd
|
||||
nxos_banner:
|
||||
nxos_banner: &motd
|
||||
banner: motd
|
||||
text: |
|
||||
this is my motd banner
|
||||
|
@ -16,23 +16,13 @@
|
|||
provider: "{{ connection }}"
|
||||
register: result
|
||||
|
||||
- debug:
|
||||
msg: "{{ result }}"
|
||||
|
||||
- assert:
|
||||
that:
|
||||
- "result.changed == true"
|
||||
- "'banner motd @\nthis is my motd banner\nthat has a multiline\nstring\n@' in result.commands"
|
||||
|
||||
- name: Set motd again (idempotent)
|
||||
nxos_banner:
|
||||
banner: motd
|
||||
text: |
|
||||
this is my motd banner
|
||||
that has a multiline
|
||||
string
|
||||
state: present
|
||||
provider: "{{ connection }}"
|
||||
nxos_banner: *motd
|
||||
register: result
|
||||
|
||||
- assert:
|
||||
|
@ -40,7 +30,6 @@
|
|||
- "result.changed == false"
|
||||
- "result.commands | length == 0"
|
||||
|
||||
|
||||
# FIXME add in tests for everything defined in docs
|
||||
# FIXME Test state:absent + test:
|
||||
# FIXME Without powers ensure "privileged mode required"
|
||||
|
|
|
@ -9,25 +9,19 @@
|
|||
provider: "{{ connection }}"
|
||||
|
||||
- name: remove exec
|
||||
nxos_banner:
|
||||
nxos_banner: &rm-exec
|
||||
banner: exec
|
||||
state: absent
|
||||
provider: "{{ connection }}"
|
||||
register: result
|
||||
|
||||
- debug:
|
||||
msg: "{{ result }}"
|
||||
|
||||
- assert:
|
||||
that:
|
||||
- "result.changed == true"
|
||||
- "'no banner exec' in result.commands"
|
||||
|
||||
- name: remove exec (idempotent)
|
||||
nxos_banner:
|
||||
banner: exec
|
||||
state: absent
|
||||
provider: "{{ connection }}"
|
||||
nxos_banner: *rm-exec
|
||||
register: result
|
||||
|
||||
- assert:
|
||||
|
@ -35,7 +29,6 @@
|
|||
- "result.changed == false"
|
||||
- "result.commands | length == 0"
|
||||
|
||||
|
||||
# FIXME add in tests for everything defined in docs
|
||||
# FIXME Test state:absent + test:
|
||||
# FIXME Without powers ensure "privileged mode required"
|
||||
|
|
Loading…
Reference in a new issue