[WIP] Fix nxos_banner (#28607)

* Fix `'state': 'absent'`

* Replace idempotence tests with references

* Fix issues with nxos_banner
This commit is contained in:
Nathaniel Case 2017-08-28 17:09:54 -04:00 committed by GitHub
parent 0a8ec4ddef
commit 862cde5e82
4 changed files with 16 additions and 46 deletions

View file

@ -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 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() commands = list()
want, have = updates
state = module.params['state'] state = module.params['state']
if state == 'absent' or (state == 'absent' and if state == 'absent' and have.get('text'):
'text' in have.keys() and have['text']):
commands.append('no banner %s' % module.params['banner']) commands.append('no banner %s' % module.params['banner'])
elif state == 'present': elif state == 'present' and want.get('text') != have.get('text'):
if want['text'] and (want['text'] != have.get('text')): banner_cmd = 'banner %s @\n%s\n@' % (module.params['banner'], want['text'].strip())
banner_cmd = 'banner %s' % module.params['banner']
banner_cmd += ' @\n'
banner_cmd += want['text'].strip()
banner_cmd += '\n@'
commands.append(banner_cmd) commands.append(banner_cmd)
return commands return commands
@ -114,6 +108,9 @@ def map_obj_to_commands(updates, module):
def map_config_to_obj(module): def map_config_to_obj(module):
output = run_commands(module, ['show banner %s' % module.params['banner']])[0] 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'} obj = {'banner': module.params['banner'], 'state': 'absent'}
if output: if output:
obj['text'] = output obj['text'] = output
@ -159,7 +156,7 @@ def main():
want = map_params_to_obj(module) want = map_params_to_obj(module)
have = map_config_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 result['commands'] = commands
if commands: if commands:
@ -169,5 +166,6 @@ def main():
module.exit_json(**result) module.exit_json(**result)
if __name__ == '__main__': if __name__ == '__main__':
main() main()

View file

@ -6,7 +6,7 @@
provider: "{{ connection }}" provider: "{{ connection }}"
- name: Set exec - name: Set exec
nxos_banner: nxos_banner: &exec
banner: exec banner: exec
text: | text: |
this is my exec banner this is my exec banner
@ -16,23 +16,13 @@
provider: "{{ connection }}" provider: "{{ connection }}"
register: result register: result
- debug:
msg: "{{ result }}"
- assert: - assert:
that: that:
- "result.changed == true" - "result.changed == true"
- "'banner exec @\nthis is my exec banner\nthat has a multiline\nstring\n@' in result.commands" - "'banner exec @\nthis is my exec banner\nthat has a multiline\nstring\n@' in result.commands"
- name: Set exec again (idempotent) - name: Set exec again (idempotent)
nxos_banner: nxos_banner: *exec
banner: exec
text: |
this is my exec banner
that has a multiline
string
state: present
provider: "{{ connection }}"
register: result register: result
- assert: - assert:

View file

@ -6,7 +6,7 @@
provider: "{{ connection }}" provider: "{{ connection }}"
- name: Set motd - name: Set motd
nxos_banner: nxos_banner: &motd
banner: motd banner: motd
text: | text: |
this is my motd banner this is my motd banner
@ -16,23 +16,13 @@
provider: "{{ connection }}" provider: "{{ connection }}"
register: result register: result
- debug:
msg: "{{ result }}"
- assert: - assert:
that: that:
- "result.changed == true" - "result.changed == true"
- "'banner motd @\nthis is my motd banner\nthat has a multiline\nstring\n@' in result.commands" - "'banner motd @\nthis is my motd banner\nthat has a multiline\nstring\n@' in result.commands"
- name: Set motd again (idempotent) - name: Set motd again (idempotent)
nxos_banner: nxos_banner: *motd
banner: motd
text: |
this is my motd banner
that has a multiline
string
state: present
provider: "{{ connection }}"
register: result register: result
- assert: - assert:
@ -40,7 +30,6 @@
- "result.changed == false" - "result.changed == false"
- "result.commands | length == 0" - "result.commands | length == 0"
# 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"

View file

@ -9,25 +9,19 @@
provider: "{{ connection }}" provider: "{{ connection }}"
- name: remove exec - name: remove exec
nxos_banner: nxos_banner: &rm-exec
banner: exec banner: exec
state: absent state: absent
provider: "{{ connection }}" provider: "{{ connection }}"
register: result register: result
- debug:
msg: "{{ result }}"
- assert: - assert:
that: that:
- "result.changed == true" - "result.changed == true"
- "'no banner exec' in result.commands" - "'no banner exec' in result.commands"
- name: remove exec (idempotent) - name: remove exec (idempotent)
nxos_banner: nxos_banner: *rm-exec
banner: exec
state: absent
provider: "{{ connection }}"
register: result register: result
- assert: - assert:
@ -35,7 +29,6 @@
- "result.changed == false" - "result.changed == false"
- "result.commands | length == 0" - "result.commands | length == 0"
# 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"