|
|
|
@ -67,59 +67,31 @@ EXAMPLES = '''
|
|
|
|
|
community: TESTING7
|
|
|
|
|
group: network-operator
|
|
|
|
|
state: present
|
|
|
|
|
host: "{{ inventory_hostname }}"
|
|
|
|
|
username: "{{ un }}"
|
|
|
|
|
password: "{{ pwd }}"
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
RETURN = '''
|
|
|
|
|
proposed:
|
|
|
|
|
description: k/v pairs of parameters passed into module
|
|
|
|
|
returned: always
|
|
|
|
|
type: dict
|
|
|
|
|
sample: {"group": "network-operator"}
|
|
|
|
|
existing:
|
|
|
|
|
description: k/v pairs of existing snmp community
|
|
|
|
|
returned: always
|
|
|
|
|
type: dict
|
|
|
|
|
sample: {}
|
|
|
|
|
end_state:
|
|
|
|
|
description: k/v pairs of snmp community after module execution
|
|
|
|
|
returned: always
|
|
|
|
|
type: dict
|
|
|
|
|
sample: {"acl": "None", "group": "network-operator"}
|
|
|
|
|
updates:
|
|
|
|
|
commands:
|
|
|
|
|
description: commands sent to the device
|
|
|
|
|
returned: always
|
|
|
|
|
type: list
|
|
|
|
|
sample: ["snmp-server community TESTING7 group network-operator"]
|
|
|
|
|
changed:
|
|
|
|
|
description: check to see if a change was made on the device
|
|
|
|
|
returned: always
|
|
|
|
|
type: boolean
|
|
|
|
|
sample: true
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import re
|
|
|
|
|
import re
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
def execute_show_command(command, module):
|
|
|
|
|
command = {
|
|
|
|
|
'command': command,
|
|
|
|
|
'output': 'json',
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return body
|
|
|
|
|
return run_commands(module, [command])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def apply_key_map(key_map, table):
|
|
|
|
@ -127,7 +99,6 @@ def apply_key_map(key_map, table):
|
|
|
|
|
for key, value in table.items():
|
|
|
|
|
new_key = key_map.get(key)
|
|
|
|
|
if new_key:
|
|
|
|
|
value = table.get(key)
|
|
|
|
|
if value:
|
|
|
|
|
new_dict[new_key] = str(value)
|
|
|
|
|
else:
|
|
|
|
@ -178,7 +149,7 @@ def get_snmp_community(module, find_filter=None):
|
|
|
|
|
community = apply_key_map(community_map, each)
|
|
|
|
|
key = each['community_name']
|
|
|
|
|
community_dict[key] = community
|
|
|
|
|
except (KeyError, AttributeError):
|
|
|
|
|
except (KeyError, AttributeError, TypeError):
|
|
|
|
|
return community_dict
|
|
|
|
|
|
|
|
|
|
if find_filter:
|
|
|
|
@ -222,13 +193,13 @@ def main():
|
|
|
|
|
argument_spec.update(nxos_argument_spec)
|
|
|
|
|
|
|
|
|
|
module = AnsibleModule(argument_spec=argument_spec,
|
|
|
|
|
required_one_of=[['access', 'group']],
|
|
|
|
|
mutually_exclusive=[['access', 'group']],
|
|
|
|
|
supports_check_mode=True)
|
|
|
|
|
required_one_of=[['access', 'group']],
|
|
|
|
|
mutually_exclusive=[['access', 'group']],
|
|
|
|
|
supports_check_mode=True)
|
|
|
|
|
|
|
|
|
|
warnings = list()
|
|
|
|
|
check_args(module, warnings)
|
|
|
|
|
|
|
|
|
|
results = {'changed': False, 'commands': [], 'warnings': warnings}
|
|
|
|
|
|
|
|
|
|
access = module.params['access']
|
|
|
|
|
group = module.params['group']
|
|
|
|
@ -246,46 +217,31 @@ def main():
|
|
|
|
|
configured_groups = get_snmp_groups(module)
|
|
|
|
|
|
|
|
|
|
if group not in configured_groups:
|
|
|
|
|
module.fail_json(msg="group not on switch."
|
|
|
|
|
"please add before moving forward")
|
|
|
|
|
module.fail_json(msg="Group not on switch. Please add before moving forward")
|
|
|
|
|
|
|
|
|
|
existing = get_snmp_community(module, community)
|
|
|
|
|
args = dict(group=group, acl=acl)
|
|
|
|
|
proposed = dict((k, v) for k, v in args.items() if v is not None)
|
|
|
|
|
delta = dict(set(proposed.items()).difference(existing.items()))
|
|
|
|
|
|
|
|
|
|
changed = False
|
|
|
|
|
end_state = existing
|
|
|
|
|
commands = []
|
|
|
|
|
|
|
|
|
|
if state == 'absent':
|
|
|
|
|
if existing:
|
|
|
|
|
command = "no snmp-server community {0}".format(community)
|
|
|
|
|
commands.append(command)
|
|
|
|
|
cmds = flatten_list(commands)
|
|
|
|
|
elif state == 'present':
|
|
|
|
|
if delta:
|
|
|
|
|
command = config_snmp_community(dict(delta), community)
|
|
|
|
|
commands.append(command)
|
|
|
|
|
cmds = flatten_list(commands)
|
|
|
|
|
|
|
|
|
|
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_community(module, community)
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|