mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-06-27 18:50:21 -07:00
Clean up nxos_snmp_contact & nxos_snmp_location
This commit is contained in:
parent
737c27121b
commit
9e4cdd2fce
2 changed files with 30 additions and 62 deletions
|
@ -66,7 +66,7 @@ commands:
|
||||||
|
|
||||||
import re
|
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.nxos import nxos_argument_spec, check_args
|
||||||
from ansible.module_utils.basic import AnsibleModule
|
from ansible.module_utils.basic import AnsibleModule
|
||||||
|
|
||||||
|
@ -77,7 +77,7 @@ def execute_show_command(command, module):
|
||||||
'output': 'text',
|
'output': 'text',
|
||||||
}
|
}
|
||||||
|
|
||||||
return run_commands(module, [command])
|
return run_commands(module, command)
|
||||||
|
|
||||||
|
|
||||||
def flatten_list(command_lists):
|
def flatten_list(command_lists):
|
||||||
|
@ -92,17 +92,12 @@ def flatten_list(command_lists):
|
||||||
|
|
||||||
def get_snmp_contact(module):
|
def get_snmp_contact(module):
|
||||||
contact = {}
|
contact = {}
|
||||||
contact_regex = '.*snmp-server\scontact\s(?P<contact>\S+).*'
|
contact_regex = r'^\s*snmp-server\scontact\s(?P<contact>.+)$'
|
||||||
command = 'show run snmp'
|
|
||||||
|
|
||||||
body = execute_show_command(command, module)[0]
|
body = execute_show_command('show run snmp', module)[0]
|
||||||
|
match_contact = re.search(contact_regex, body, re.M)
|
||||||
try:
|
if match_contact:
|
||||||
match_contact = re.match(contact_regex, body, re.DOTALL)
|
contact['contact'] = match_contact.group("contact")
|
||||||
group_contact = match_contact.groupdict()
|
|
||||||
contact['contact'] = group_contact["contact"]
|
|
||||||
except AttributeError:
|
|
||||||
contact = {}
|
|
||||||
|
|
||||||
return contact
|
return contact
|
||||||
|
|
||||||
|
@ -110,25 +105,21 @@ def get_snmp_contact(module):
|
||||||
def main():
|
def main():
|
||||||
argument_spec = dict(
|
argument_spec = dict(
|
||||||
contact=dict(required=True, type='str'),
|
contact=dict(required=True, type='str'),
|
||||||
state=dict(choices=['absent', 'present'],
|
state=dict(choices=['absent', 'present'], default='present'),
|
||||||
default='present')
|
|
||||||
)
|
)
|
||||||
|
|
||||||
argument_spec.update(nxos_argument_spec)
|
argument_spec.update(nxos_argument_spec)
|
||||||
|
|
||||||
module = AnsibleModule(argument_spec=argument_spec,
|
module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True)
|
||||||
supports_check_mode=True)
|
|
||||||
|
|
||||||
warnings = list()
|
warnings = list()
|
||||||
check_args(module, warnings)
|
check_args(module, warnings)
|
||||||
results = {'changed': False, 'commands': [], 'warnings': warnings}
|
results = {'changed': False, 'commands': [], 'warnings': warnings}
|
||||||
|
|
||||||
|
|
||||||
contact = module.params['contact']
|
contact = module.params['contact']
|
||||||
state = module.params['state']
|
state = module.params['state']
|
||||||
|
|
||||||
existing = get_snmp_contact(module)
|
existing = get_snmp_contact(module)
|
||||||
proposed = dict(contact=contact)
|
|
||||||
commands = []
|
commands = []
|
||||||
|
|
||||||
if state == 'absent':
|
if state == 'absent':
|
||||||
|
@ -140,11 +131,12 @@ def main():
|
||||||
|
|
||||||
cmds = flatten_list(commands)
|
cmds = flatten_list(commands)
|
||||||
if cmds:
|
if cmds:
|
||||||
|
results['changed'] = True
|
||||||
if not module.check_mode:
|
if not module.check_mode:
|
||||||
load_config(module, cmds)
|
load_config(module, cmds)
|
||||||
|
|
||||||
if 'configure' in cmds:
|
if 'configure' in cmds:
|
||||||
cmds.pop(0)
|
cmds.pop(0)
|
||||||
results['changed'] = True
|
|
||||||
results['commands'] = cmds
|
results['commands'] = cmds
|
||||||
|
|
||||||
module.exit_json(**results)
|
module.exit_json(**results)
|
||||||
|
@ -152,4 +144,3 @@ def main():
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|
||||||
|
|
|
@ -16,7 +16,6 @@
|
||||||
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
||||||
#
|
#
|
||||||
|
|
||||||
|
|
||||||
ANSIBLE_METADATA = {'metadata_version': '1.1',
|
ANSIBLE_METADATA = {'metadata_version': '1.1',
|
||||||
'status': ['preview'],
|
'status': ['preview'],
|
||||||
'supported_by': 'network'}
|
'supported_by': 'network'}
|
||||||
|
@ -33,6 +32,8 @@ description:
|
||||||
author:
|
author:
|
||||||
- Jason Edelman (@jedelman8)
|
- Jason Edelman (@jedelman8)
|
||||||
- Gabriele Gerbino (@GGabriele)
|
- Gabriele Gerbino (@GGabriele)
|
||||||
|
notes:
|
||||||
|
- Tested against NXOSv 7.3.(0)D1(1) on VIRL
|
||||||
options:
|
options:
|
||||||
location:
|
location:
|
||||||
description:
|
description:
|
||||||
|
@ -60,7 +61,7 @@ EXAMPLES = '''
|
||||||
|
|
||||||
RETURN = '''
|
RETURN = '''
|
||||||
commands:
|
commands:
|
||||||
description: command sent to the device
|
description: commands sent to the device
|
||||||
returned: always
|
returned: always
|
||||||
type: list
|
type: list
|
||||||
sample: ["snmp-server location New_Test"]
|
sample: ["snmp-server location New_Test"]
|
||||||
|
@ -69,36 +70,18 @@ commands:
|
||||||
|
|
||||||
import re
|
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.nxos import nxos_argument_spec, check_args
|
||||||
from ansible.module_utils.basic import AnsibleModule
|
from ansible.module_utils.basic import AnsibleModule
|
||||||
|
|
||||||
|
|
||||||
def execute_show_command(command, module, command_type='cli_show'):
|
def execute_show_command(command, module):
|
||||||
if 'show run' not in command:
|
command = {
|
||||||
cmds = [{
|
'command': command,
|
||||||
'command': command,
|
'output': 'text',
|
||||||
'output': 'json',
|
}
|
||||||
}]
|
|
||||||
else:
|
|
||||||
cmds = [{
|
|
||||||
'command': command,
|
|
||||||
'output': 'text',
|
|
||||||
}]
|
|
||||||
|
|
||||||
return run_commands(module, cmds)
|
return run_commands(module, command)
|
||||||
|
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
|
|
||||||
def flatten_list(command_lists):
|
def flatten_list(command_lists):
|
||||||
|
@ -113,16 +96,12 @@ def flatten_list(command_lists):
|
||||||
|
|
||||||
def get_snmp_location(module):
|
def get_snmp_location(module):
|
||||||
location = {}
|
location = {}
|
||||||
location_regex = '.*snmp-server\slocation\s(?P<location>\S+).*'
|
location_regex = r'^\s*snmp-server\slocation\s(?P<location>.+)$'
|
||||||
command = 'show run snmp'
|
|
||||||
|
|
||||||
body = execute_show_command(command, module, command_type='cli_show_ascii')
|
body = execute_show_command('show run snmp', module)[0]
|
||||||
try:
|
match_location = re.search(location_regex, body, re.M)
|
||||||
match_location = re.match(location_regex, body[0], re.DOTALL)
|
if match_location:
|
||||||
group_location = match_location.groupdict()
|
location['location'] = match_location.group("location")
|
||||||
location['location'] = group_location["location"]
|
|
||||||
except (AttributeError, TypeError):
|
|
||||||
location = {}
|
|
||||||
|
|
||||||
return location
|
return location
|
||||||
|
|
||||||
|
@ -130,7 +109,7 @@ def get_snmp_location(module):
|
||||||
def main():
|
def main():
|
||||||
argument_spec = dict(
|
argument_spec = dict(
|
||||||
location=dict(required=True, type='str'),
|
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)
|
argument_spec.update(nxos_argument_spec)
|
||||||
|
@ -139,15 +118,13 @@ def main():
|
||||||
|
|
||||||
warnings = list()
|
warnings = list()
|
||||||
check_args(module, warnings)
|
check_args(module, warnings)
|
||||||
results = {'commands': [], 'changed': False, 'warnings': warnings}
|
results = {'changed': False, 'commands': [], 'warnings': warnings}
|
||||||
|
|
||||||
location = module.params['location']
|
location = module.params['location']
|
||||||
state = module.params['state']
|
state = module.params['state']
|
||||||
|
|
||||||
existing = get_snmp_location(module)
|
existing = get_snmp_location(module)
|
||||||
commands = []
|
commands = []
|
||||||
proposed = dict(location=location)
|
|
||||||
end_state = existing
|
|
||||||
|
|
||||||
if state == 'absent':
|
if state == 'absent':
|
||||||
if existing and existing['location'] == location:
|
if existing and existing['location'] == location:
|
||||||
|
@ -158,16 +135,16 @@ def main():
|
||||||
|
|
||||||
cmds = flatten_list(commands)
|
cmds = flatten_list(commands)
|
||||||
if cmds:
|
if cmds:
|
||||||
|
results['changed'] = True
|
||||||
if not module.check_mode:
|
if not module.check_mode:
|
||||||
load_config(module, cmds)
|
load_config(module, cmds)
|
||||||
|
|
||||||
if 'configure' in cmds:
|
if 'configure' in cmds:
|
||||||
cmds.pop(0)
|
cmds.pop(0)
|
||||||
results['commands'] = cmds
|
results['commands'] = cmds
|
||||||
results['changed'] = True
|
|
||||||
|
|
||||||
module.exit_json(**results)
|
module.exit_json(**results)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue