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
pull/27716/merge
Nathaniel Case 7 years ago committed by GitHub
parent 1de91a9aa0
commit 0464a21a85

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

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

@ -82,67 +82,37 @@ EXAMPLES = '''
key_id: 32 key_id: 32
md5string: hello md5string: hello
auth_type: text auth_type: text
host: "{{ inventory_hostname }}"
username: "{{ un }}"
password: "{{ pwd }}"
''' '''
RETURN = ''' RETURN = '''
proposed: commands:
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:
description: command sent to the device description: command sent to the device
returned: always returned: always
type: list type: list
sample: ["ntp authentication-key 32 md5 helloWorld 0", "ntp trusted-key 32"] 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 get_config, 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
import re
def execute_show_command(command, module, command_type='cli_show'): def execute_show_command(command, module):
if module.params['transport'] == 'cli': if 'show run' not in command:
if 'show run' not in command: command = {
command += ' | json' 'command': command,
cmds = [command] 'output': 'json',
body = run_commands(module, cmds) }
elif module.params['transport'] == 'nxapi': else:
cmds = [command] command = {
body = run_commands(module, cmds) 'command': command,
'output': 'text',
}
return body return run_commands(module, [command])
def flatten_list(command_lists): def flatten_list(command_lists):
@ -173,8 +143,7 @@ def get_ntp_trusted_key(module):
trusted_key_list = [] trusted_key_list = []
command = 'show run | inc ntp.trusted-key' command = 'show run | inc ntp.trusted-key'
trusted_key_str = execute_show_command( trusted_key_str = execute_show_command(command, module)[0]
command, module, command_type='cli_show_ascii')[0]
if trusted_key_str: if trusted_key_str:
trusted_keys = trusted_key_str.splitlines() 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" auth_regex = (".*ntp\sauthentication-key\s(?P<key_id>\d+)\s"
"md5\s(?P<md5string>\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: 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() group_authentication = match_authentication.groupdict()
key_id = group_authentication["key_id"] key_id = group_authentication["key_id"]
md5string = group_authentication['md5string'] md5string = group_authentication['md5string']

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

@ -52,59 +52,31 @@ EXAMPLES = '''
- nxos_snmp_contact: - nxos_snmp_contact:
contact: Test contact: Test
state: present state: present
host: "{{ inventory_hostname }}"
username: "{{ un }}"
password: "{{ pwd }}"
''' '''
RETURN = ''' RETURN = '''
proposed: commands:
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:
description: commands sent to the device description: commands sent to the device
returned: always returned: always
type: list type: list
sample: ["snmp-server contact New_Test"] 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 get_config, 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
import re def execute_show_command(command, module):
import re 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): def flatten_list(command_lists):
@ -122,7 +94,7 @@ def get_snmp_contact(module):
contact_regex = '.*snmp-server\scontact\s(?P<contact>\S+).*' contact_regex = '.*snmp-server\scontact\s(?P<contact>\S+).*'
command = 'show run snmp' command = 'show run snmp'
body = execute_show_command(command, module, command_type='cli_show_ascii')[0] body = execute_show_command(command, module)[0]
try: try:
match_contact = re.match(contact_regex, body, re.DOTALL) match_contact = re.match(contact_regex, body, re.DOTALL)
@ -148,15 +120,14 @@ def main():
warnings = list() warnings = list()
check_args(module, warnings) check_args(module, 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)
changed = False
proposed = dict(contact=contact) proposed = dict(contact=contact)
end_state = existing
commands = [] commands = []
if state == 'absent': if state == 'absent':
@ -168,22 +139,12 @@ def main():
cmds = flatten_list(commands) cmds = flatten_list(commands)
if cmds: if cmds:
if module.check_mode: if not module.check_mode:
module.exit_json(changed=True, commands=cmds)
else:
changed = True
load_config(module, cmds) load_config(module, cmds)
end_state = get_snmp_contact(module) if 'configure' in cmds:
if 'configure' in cmds: cmds.pop(0)
cmds.pop(0) results['changed'] = True
results['commands'] = cmds
results = {}
results['proposed'] = proposed
results['existing'] = existing
results['end_state'] = end_state
results['updates'] = cmds
results['changed'] = changed
results['warnings'] = warnings
module.exit_json(**results) module.exit_json(**results)

Loading…
Cancel
Save