community.general/lib/ansible/modules/network/nxos/nxos_snmp_location.py

174 lines
4.5 KiB
Python
Raw Normal View History

2016-09-14 15:02:02 +00:00
#!/usr/bin/python
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
#
2017-08-16 03:16:38 +00:00
ANSIBLE_METADATA = {'metadata_version': '1.1',
'status': ['preview'],
'supported_by': 'network'}
2016-12-06 10:35:05 +00:00
2016-09-14 15:02:02 +00:00
DOCUMENTATION = '''
---
module: nxos_snmp_location
extends_documentation_fragment: nxos
version_added: "2.2"
2016-09-14 15:02:02 +00:00
short_description: Manages SNMP location information.
description:
- Manages SNMP location configuration.
author:
- Jason Edelman (@jedelman8)
- Gabriele Gerbino (@GGabriele)
options:
location:
description:
- Location information.
required: true
state:
description:
- Manage the state of the resource.
required: false
default: present
choices: ['present','absent']
'''
EXAMPLES = '''
# ensure snmp location is configured
- nxos_snmp_location:
location: Test
state: present
2016-09-14 15:02:02 +00:00
# ensure snmp location is not configured
- nxos_snmp_location:
location: Test
state: absent
2016-09-14 15:02:02 +00:00
'''
RETURN = '''
commands:
description: command sent to the device
2016-09-14 15:02:02 +00:00
returned: always
2016-09-14 15:10:21 +00:00
type: list
2016-09-14 15:02:02 +00:00
sample: ["snmp-server location New_Test"]
'''
import re
from ansible.module_utils.nxos import get_config, load_config, run_commands
from ansible.module_utils.nxos import nxos_argument_spec, check_args
from ansible.module_utils.basic import AnsibleModule
2016-09-14 15:02:02 +00:00
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',
}]
return run_commands(module, cmds)
2016-09-14 15:02:02 +00:00
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
2016-09-14 15:02:02 +00:00
def flatten_list(command_lists):
flat_command_list = []
for command in command_lists:
if isinstance(command, list):
flat_command_list.extend(command)
else:
flat_command_list.append(command)
return flat_command_list
def get_snmp_location(module):
location = {}
location_regex = '.*snmp-server\slocation\s(?P<location>\S+).*'
command = 'show run snmp'
2016-09-14 15:02:02 +00:00
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 = {}
2016-09-14 15:02:02 +00:00
return location
def main():
argument_spec = dict(
location=dict(required=True, type='str'),
state=dict(choices=['absent', 'present'], default='present')
2016-09-14 15:02:02 +00:00
)
argument_spec.update(nxos_argument_spec)
module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True)
warnings = list()
check_args(module, warnings)
results = {'commands': [], 'changed': False, 'warnings': warnings}
2016-09-14 15:02:02 +00:00
location = module.params['location']
state = module.params['state']
existing = get_snmp_location(module)
commands = []
proposed = dict(location=location)
end_state = existing
2016-09-14 15:02:02 +00:00
if state == 'absent':
if existing and existing['location'] == location:
commands.append('no snmp-server location')
elif state == 'present':
if not existing or existing['location'] != location:
commands.append('snmp-server location {0}'.format(location))
cmds = flatten_list(commands)
if cmds:
if not module.check_mode:
load_config(module, cmds)
if 'configure' in cmds:
cmds.pop(0)
results['commands'] = cmds
results['changed'] = True
2016-09-14 15:02:02 +00:00
module.exit_json(**results)
if __name__ == "__main__":
main()