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

Fix nxos_snmp_contact, others (#28000)

* Fix nxos_snmp_contact

Fixes #27714

* Fix nxos_snmp_community

Fixes #27711

* Fix nxos_install_os

Fixes #27604

* Update nxos_ntp_auth

Fixes #27521

* Fix nxos_igmp_snooping

Fixes #27501
This commit is contained in:
Nathaniel Case 2017-08-11 10:12:43 -04:00 committed by GitHub
parent 1de91a9aa0
commit 0464a21a85
5 changed files with 83 additions and 254 deletions

View file

@ -78,9 +78,6 @@ EXAMPLES = '''
# ensure igmp snooping params supported in this module are in there default state
- nxos_igmp_snooping:
state: default
host: inventory_hostname }}
username: un }}
password: pwd }}
# ensure following igmp snooping params are in the desired state
- nxos_igmp_snooping:
@ -90,32 +87,10 @@ EXAMPLES = '''
optimize_mcast_flood: false
report_supp: true
v3_report_supp: true
host: "{{ inventory_hostname }}"
username: "{{ un }}"
password: "{{ pwd }}"
'''
RETURN = '''
proposed:
description: k/v pairs of parameters passed into module
returned: always
type: dict
sample: {"group_timeout": "50", "link_local_grp_supp": true,
"report_supp": false, "snooping": false, "v3_report_supp": false}
existing:
description:
- k/v pairs of existing configuration
returned: always
type: dict
sample: {"group_timeout": "never", "link_local_grp_supp": false,
"report_supp": true, "snooping": true, "v3_report_supp": true}
end_state:
description: k/v pairs of configuration after module execution
returned: always
type: dict
sample: {"group_timeout": "50", "link_local_grp_supp": true,
"report_supp": false, "snooping": false, "v3_report_supp": false}
updates:
commands:
description: command sent to the device
returned: always
type: list
@ -124,31 +99,23 @@ updates:
"no ip igmp snooping report-suppression",
"no ip igmp snooping v3-report-suppression",
"no ip igmp snooping"]
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
def execute_show_command(command, module):
command = {
'command': command,
'output': 'text',
}
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)
return body
return run_commands(module, [command])
def flatten_list(command_lists):
@ -184,8 +151,7 @@ def get_snooping(config):
def get_igmp_snooping(module):
command = 'show run all | include igmp.snooping'
existing = {}
body = execute_show_command(
command, module, command_type='cli_show_ascii')[0]
body = execute_show_command(command, module)[0]
if body:
split_body = body.splitlines()
@ -272,12 +238,11 @@ def main():
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}
snooping = module.params['snooping']
link_local_grp_supp = module.params['link_local_grp_supp']
@ -295,7 +260,6 @@ def main():
existing = get_igmp_snooping(module)
end_state = existing
changed = False
commands = []
if state == 'present':
@ -317,23 +281,13 @@ def main():
commands.append(command)
cmds = flatten_list(commands)
results = {}
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_igmp_snooping(module)
if 'configure' in cmds:
cmds.pop(0)
results['proposed'] = proposed
results['existing'] = existing
results['updates'] = cmds
results['changed'] = changed
results['warnings'] = warnings
results['end_state'] = end_state
if 'configure' in cmds:
cmds.pop(0)
results['commands'] = cmds
module.exit_json(**results)

View file

@ -66,10 +66,6 @@ EXAMPLES = '''
- name: Install OS
nxos_install_os:
system_image_file: nxos.7.0.3.I2.2d.bin
host: "{{ inventory_hostname }}"
username: "{{ un }}"
password: "{{ pwd }}"
transport: nxapi
rescue:
- name: Wait for device to perform checks
wait_for:
@ -77,22 +73,16 @@ EXAMPLES = '''
state: stopped
timeout: 300
delay: 60
host: "{{ inventory_hostname }}"
- name: Wait for device to come back up
wait_for:
port: 22
state: started
timeout: 300
delay: 60
host: "{{ inventory_hostname }}"
- name: Check installed OS
nxos_command:
commands:
- show version
username: "{{ un }}"
password: "{{ pwd }}"
host: "{{ inventory_hostname }}"
transport: nxapi
register: output
- assert:
that:
@ -119,19 +109,19 @@ install_state:
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
def execute_show_command(command, module, command_type='cli_show_ascii'):
cmds = [command]
if module.params['transport'] == 'cli':
body = run_commands(module, cmds)
elif module.params['transport'] == 'nxapi':
body = run_commands(module, cmds)
def execute_show_command(command, module):
command = {
'command': command,
'output': 'text',
}
return body
return run_commands(module, [command])
def get_boot_options(module):
@ -198,7 +188,7 @@ def main():
warnings = list()
check_args(module, warnings)
install_state = module.params['install_state']
system_image_file = module.params['system_image_file']
kickstart_image_file = module.params['kickstart_image_file']
@ -230,4 +220,3 @@ def main():
if __name__ == '__main__':
main()

View file

@ -82,67 +82,37 @@ EXAMPLES = '''
key_id: 32
md5string: hello
auth_type: text
host: "{{ inventory_hostname }}"
username: "{{ un }}"
password: "{{ pwd }}"
'''
RETURN = '''
proposed:
description: k/v pairs of parameters passed into module
returned: always
type: dict
sample: {"auth_type": "text", "authentication": "off",
"key_id": "32", "md5string": "helloWorld",
"trusted_key": "true"}
existing:
description:
- k/v pairs of existing ntp authentication
returned: always
type: dict
sample: {"authentication": "off", "trusted_key": "false"}
end_state:
description: k/v pairs of ntp authentication after module execution
returned: always
type: dict
sample: {"authentication": "off", "key_id": "32",
"md5string": "kapqgWjwdg", "trusted_key": "true"}
state:
description: state as sent in from the playbook
returned: always
type: string
sample: "present"
updates:
commands:
description: command sent to the device
returned: always
type: list
sample: ["ntp authentication-key 32 md5 helloWorld 0", "ntp trusted-key 32"]
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
def execute_show_command(command, module):
if 'show run' not in command:
command = {
'command': command,
'output': 'json',
}
else:
command = {
'command': command,
'output': 'text',
}
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)
return body
return run_commands(module, [command])
def flatten_list(command_lists):
@ -173,8 +143,7 @@ def get_ntp_trusted_key(module):
trusted_key_list = []
command = 'show run | inc ntp.trusted-key'
trusted_key_str = execute_show_command(
command, module, command_type='cli_show_ascii')[0]
trusted_key_str = execute_show_command(command, module)[0]
if trusted_key_str:
trusted_keys = trusted_key_str.splitlines()
@ -194,10 +163,10 @@ def get_ntp_auth_key(key_id, module):
auth_regex = (".*ntp\sauthentication-key\s(?P<key_id>\d+)\s"
"md5\s(?P<md5string>\S+).*")
body = execute_show_command(command, module, command_type='cli_show_ascii')
body = execute_show_command(command, module)[0]
try:
match_authentication = re.match(auth_regex, body[0], re.DOTALL)
match_authentication = re.match(auth_regex, body, re.DOTALL)
group_authentication = match_authentication.groupdict()
key_id = group_authentication["key_id"]
md5string = group_authentication['md5string']

View file

@ -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 = {
'command': command,
'output': 'json',
}
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)
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)

View file

@ -52,59 +52,31 @@ EXAMPLES = '''
- nxos_snmp_contact:
contact: Test
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: {"contact": "New_Test"}
existing:
description: k/v pairs of existing snmp contact
returned: always
type: dict
sample: {"contact": "Test"}
end_state:
description: k/v pairs of snmp contact after module execution
returned: always
type: dict
sample: {"contact": "New_Test"}
updates:
commands:
description: commands sent to the device
returned: always
type: list
sample: ["snmp-server contact New_Test"]
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 = {
'command': command,
'output': 'text',
}
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)
return body
return run_commands(module, [command])
def flatten_list(command_lists):
@ -122,7 +94,7 @@ def get_snmp_contact(module):
contact_regex = '.*snmp-server\scontact\s(?P<contact>\S+).*'
command = 'show run snmp'
body = execute_show_command(command, module, command_type='cli_show_ascii')[0]
body = execute_show_command(command, module)[0]
try:
match_contact = re.match(contact_regex, body, re.DOTALL)
@ -148,15 +120,14 @@ def main():
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)
changed = False
proposed = dict(contact=contact)
end_state = existing
commands = []
if state == 'absent':
@ -168,22 +139,12 @@ def main():
cmds = flatten_list(commands)
if cmds:
if module.check_mode:
module.exit_json(changed=True, commands=cmds)
else:
changed = True
if not module.check_mode:
load_config(module, cmds)
end_state = get_snmp_contact(module)
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['changed'] = True
results['commands'] = cmds
module.exit_json(**results)