|
|
|
|
@ -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',
|
|
|
|
|
}]
|
|
|
|
|
|
|
|
|
|
return run_commands(module, cmds)
|
|
|
|
|
|
|
|
|
|
def execute_show_command(command, module):
|
|
|
|
|
command = {
|
|
|
|
|
'command': command,
|
|
|
|
|
'output': 'text',
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
|