nxos_snmp cleanup (#28922)

* Clean up nxos_snmp_contact & nxos_snmp_location

* Bring nxos_snmp_community in line

* Bring nxos_snmp_host in line

* And I would have gotten away with it too,

if it weren't for those meddling sanity tests

* Bring nxos_snmp_traps & nxos_snmp_user in line

* Appease Shippable
This commit is contained in:
Nathaniel Case 2017-09-13 08:23:36 -07:00 committed by GitHub
commit 8c03609e54
7 changed files with 156 additions and 360 deletions

View file

@ -16,7 +16,6 @@
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
#
ANSIBLE_METADATA = {'metadata_version': '1.1',
'status': ['preview'],
'supported_by': 'network'}
@ -55,75 +54,39 @@ options:
choices: ['enabled','disabled']
'''
EXAMPLES = '''
EXAMPLES = '''
# ensure lldp trap configured
- nxos_snmp_traps:
group: lldp
state: enabled
host: "{{ inventory_hostname }}"
username: "{{ un }}"
password: "{{ pwd }}"
# ensure lldp trap is not configured
- nxos_snmp_traps:
group: lldp
state: disabled
host: "{{ inventory_hostname }}"
username: "{{ un }}"
password: "{{ pwd }}"
'''
RETURN = '''
proposed:
description: k/v pairs of parameters passed into module
returned: always
type: dict
sample: {"group": "lldp"}
existing:
description: k/v pairs of existing trap status
returned: always
type: dict
sample: {"lldp": [{"enabled": "No",
"trap": "lldpRemTablesChange"}]}
end_state:
description: k/v pairs of trap info after module execution
returned: always
type: dict
sample: {"lldp": [{"enabled": "Yes",
"trap": "lldpRemTablesChange"}]}
updates:
commands:
description: command sent to the device
returned: always
type: list
sample: "snmp-server enable traps lldp ;"
changed:
description: check to see if a change was made on the device
returned: always
type: boolean
sample: true
'''
from ansible.module_utils.nxos import get_config, load_config, run_commands
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.basic import AnsibleModule
import re
import re
def execute_show_command(command, module):
command = {
'command': command,
'output': 'json',
}
def execute_show_command(command, module, command_type='cli_show'):
if module.params['transport'] == 'cli':
if 'show run' not in command:
command += ' | json'
cmds = [command]
body = run_commands(module, cmds)
elif module.params['transport'] == 'nxapi':
cmds = [command]
body = run_commands(module, cmds)
return body
return run_commands(module, command)
def apply_key_map(key_map, table):
@ -149,10 +112,8 @@ def flatten_list(command_lists):
return flat_command_list
def get_snmp_traps(group, module):
command = 'show snmp trap'
body = execute_show_command(command, module)
body = execute_show_command('show snmp trap', module)
trap_key = {
'description': 'trap',
@ -204,14 +165,14 @@ def get_trap_commands(group, state, existing, module):
if state == 'disabled':
for feature in existing:
trap_commands = ['no snmp-server enable traps {0}'.format(feature) for
trap in existing[feature] if trap['enabled'] == 'Yes']
trap in existing[feature] if trap['enabled'] == 'Yes']
trap_commands = list(set(trap_commands))
commands.append(trap_commands)
elif state == 'enabled':
for feature in existing:
trap_commands = ['snmp-server enable traps {0}'.format(feature) for
trap in existing[feature] if trap['enabled'] == 'No']
trap in existing[feature] if trap['enabled'] == 'No']
trap_commands = list(set(trap_commands))
commands.append(trap_commands)
@ -239,50 +200,37 @@ def main():
argument_spec = dict(
state=dict(choices=['enabled', 'disabled'], default='enabled'),
group=dict(choices=['aaa', 'bridge', 'callhome', 'cfs', 'config',
'entity', 'feature-control', 'hsrp',
'license', 'link', 'lldp', 'ospf', 'pim', 'rf',
'rmon', 'snmp', 'storm-control', 'stpx',
'sysmgr', 'system', 'upgrade', 'vtp', 'all'],
required=True),
'entity', 'feature-control', 'hsrp',
'license', 'link', 'lldp', 'ospf', 'pim', 'rf',
'rmon', 'snmp', 'storm-control', 'stpx',
'sysmgr', 'system', 'upgrade', 'vtp', 'all'],
required=True),
)
argument_spec.update(nxos_argument_spec)
module = AnsibleModule(argument_spec=argument_spec,
supports_check_mode=True)
module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True)
warnings = list()
check_args(module, warnings)
results = {'changed': False, 'commands': [], 'warnings': warnings}
group = module.params['group'].lower()
state = module.params['state']
existing = get_snmp_traps(group, module)
proposed = {'group': group}
changed = False
end_state = existing
commands = get_trap_commands(group, state, existing, module)
cmds = flatten_list(commands)
if cmds:
if module.check_mode:
module.exit_json(changed=True, commands=cmds)
else:
changed = True
results['changed'] = True
if not module.check_mode:
load_config(module, cmds)
end_state = get_snmp_traps(group, module)
if 'configure' in cmds:
cmds.pop(0)
results = {}
results['proposed'] = proposed
results['existing'] = existing
results['end_state'] = end_state
results['updates'] = cmds
results['changed'] = changed
results['warnings'] = warnings
if 'configure' in cmds:
cmds.pop(0)
results['commands'] = cmds
module.exit_json(**results)