1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Clean up nxos_snmp_contact & nxos_snmp_location

This commit is contained in:
Nathaniel Case 2017-09-01 07:12:49 -04:00
parent 737c27121b
commit 9e4cdd2fce
2 changed files with 30 additions and 62 deletions

View file

@ -66,7 +66,7 @@ commands:
import re
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
@ -77,7 +77,7 @@ def execute_show_command(command, module):
'output': 'text',
}
return run_commands(module, [command])
return run_commands(module, command)
def flatten_list(command_lists):
@ -92,17 +92,12 @@ def flatten_list(command_lists):
def get_snmp_contact(module):
contact = {}
contact_regex = '.*snmp-server\scontact\s(?P<contact>\S+).*'
command = 'show run snmp'
contact_regex = r'^\s*snmp-server\scontact\s(?P<contact>.+)$'
body = execute_show_command(command, module)[0]
try:
match_contact = re.match(contact_regex, body, re.DOTALL)
group_contact = match_contact.groupdict()
contact['contact'] = group_contact["contact"]
except AttributeError:
contact = {}
body = execute_show_command('show run snmp', module)[0]
match_contact = re.search(contact_regex, body, re.M)
if match_contact:
contact['contact'] = match_contact.group("contact")
return contact
@ -110,25 +105,21 @@ def get_snmp_contact(module):
def main():
argument_spec = dict(
contact=dict(required=True, type='str'),
state=dict(choices=['absent', 'present'],
default='present')
state=dict(choices=['absent', 'present'], default='present'),
)
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}
contact = module.params['contact']
state = module.params['state']
existing = get_snmp_contact(module)
proposed = dict(contact=contact)
commands = []
if state == 'absent':
@ -140,11 +131,12 @@ def main():
cmds = flatten_list(commands)
if cmds:
results['changed'] = True
if not module.check_mode:
load_config(module, cmds)
if 'configure' in cmds:
cmds.pop(0)
results['changed'] = True
results['commands'] = cmds
module.exit_json(**results)
@ -152,4 +144,3 @@ def main():
if __name__ == '__main__':
main()

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'}
@ -33,6 +32,8 @@ description:
author:
- Jason Edelman (@jedelman8)
- Gabriele Gerbino (@GGabriele)
notes:
- Tested against NXOSv 7.3.(0)D1(1) on VIRL
options:
location:
description:
@ -60,7 +61,7 @@ EXAMPLES = '''
RETURN = '''
commands:
description: command sent to the device
description: commands sent to the device
returned: always
type: list
sample: ["snmp-server location New_Test"]
@ -69,36 +70,18 @@ commands:
import re
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
def execute_show_command(command, module, command_type='cli_show'):
if 'show run' not in command:
cmds = [{
'command': command,
'output': 'json',
}]
else:
cmds = [{
'command': command,
'output': 'text',
}]
def execute_show_command(command, module):
command = {
'command': command,
'output': 'text',
}
return run_commands(module, cmds)
def apply_key_map(key_map, table):
new_dict = {}
for key, value in table.items():
new_key = key_map.get(key)
if new_key:
if value:
new_dict[new_key] = str(value)
else:
new_dict[new_key] = value
return new_dict
return run_commands(module, command)
def flatten_list(command_lists):
@ -113,16 +96,12 @@ def flatten_list(command_lists):
def get_snmp_location(module):
location = {}
location_regex = '.*snmp-server\slocation\s(?P<location>\S+).*'
command = 'show run snmp'
location_regex = r'^\s*snmp-server\slocation\s(?P<location>.+)$'
body = execute_show_command(command, module, command_type='cli_show_ascii')
try:
match_location = re.match(location_regex, body[0], re.DOTALL)
group_location = match_location.groupdict()
location['location'] = group_location["location"]
except (AttributeError, TypeError):
location = {}
body = execute_show_command('show run snmp', module)[0]
match_location = re.search(location_regex, body, re.M)
if match_location:
location['location'] = match_location.group("location")
return location
@ -130,7 +109,7 @@ def get_snmp_location(module):
def main():
argument_spec = dict(
location=dict(required=True, type='str'),
state=dict(choices=['absent', 'present'], default='present')
state=dict(choices=['absent', 'present'], default='present'),
)
argument_spec.update(nxos_argument_spec)
@ -139,15 +118,13 @@ def main():
warnings = list()
check_args(module, warnings)
results = {'commands': [], 'changed': False, 'warnings': warnings}
results = {'changed': False, 'commands': [], 'warnings': warnings}
location = module.params['location']
state = module.params['state']
existing = get_snmp_location(module)
commands = []
proposed = dict(location=location)
end_state = existing
if state == 'absent':
if existing and existing['location'] == location:
@ -158,16 +135,16 @@ def main():
cmds = flatten_list(commands)
if cmds:
results['changed'] = True
if not module.check_mode:
load_config(module, cmds)
if 'configure' in cmds:
cmds.pop(0)
results['commands'] = cmds
results['changed'] = True
module.exit_json(**results)
if __name__ == "__main__":
if __name__ == '__main__':
main()