mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-06-28 03:00:23 -07:00
Fixing docstring and more fix
This commit is contained in:
parent
346f83029b
commit
0c4c5467f7
1 changed files with 41 additions and 12 deletions
|
@ -67,11 +67,11 @@ end_state:
|
||||||
returned: always
|
returned: always
|
||||||
type: dict
|
type: dict
|
||||||
sample: {"contact": "New_Test"}
|
sample: {"contact": "New_Test"}
|
||||||
commands:
|
updates:
|
||||||
description: command string sent to the device
|
description: commands sent to the device
|
||||||
returned: always
|
returned: always
|
||||||
type: string
|
type: list
|
||||||
sample: "snmp-server contact New_Test ;"
|
sample: ["snmp-server contact New_Test"]
|
||||||
changed:
|
changed:
|
||||||
description: check to see if a change was made on the device
|
description: check to see if a change was made on the device
|
||||||
returned: always
|
returned: always
|
||||||
|
@ -238,12 +238,20 @@ def load_config(module, candidate):
|
||||||
|
|
||||||
def execute_config_command(commands, module):
|
def execute_config_command(commands, module):
|
||||||
try:
|
try:
|
||||||
body = module.configure(commands)
|
module.configure(commands)
|
||||||
|
except ShellError:
|
||||||
|
clie = get_exception()
|
||||||
|
module.fail_json(msg='Error sending CLI commands',
|
||||||
|
error=str(clie), commands=commands)
|
||||||
|
except AttributeError:
|
||||||
|
try:
|
||||||
|
commands.insert(0, 'configure')
|
||||||
|
module.cli.add_commands(commands, output='config')
|
||||||
|
module.cli.run_commands()
|
||||||
except ShellError:
|
except ShellError:
|
||||||
clie = get_exception()
|
clie = get_exception()
|
||||||
module.fail_json(msg='Error sending CLI commands',
|
module.fail_json(msg='Error sending CLI commands',
|
||||||
error=str(clie), commands=commands)
|
error=str(clie), commands=commands)
|
||||||
return body
|
|
||||||
|
|
||||||
|
|
||||||
def get_cli_body_ssh(command, response, module):
|
def get_cli_body_ssh(command, response, module):
|
||||||
|
@ -260,8 +268,10 @@ def get_cli_body_ssh(command, response, module):
|
||||||
body = response
|
body = response
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
response = response[0].replace(command + '\n\n', '').strip()
|
if isinstance(response[0], str):
|
||||||
body = [json.loads(response)]
|
body = [json.loads(response[0])]
|
||||||
|
else:
|
||||||
|
body = response
|
||||||
except ValueError:
|
except ValueError:
|
||||||
module.fail_json(msg='Command does not support JSON output',
|
module.fail_json(msg='Command does not support JSON output',
|
||||||
command=command)
|
command=command)
|
||||||
|
@ -269,6 +279,11 @@ def get_cli_body_ssh(command, response, module):
|
||||||
|
|
||||||
|
|
||||||
def execute_show(cmds, module, command_type=None):
|
def execute_show(cmds, module, command_type=None):
|
||||||
|
command_type_map = {
|
||||||
|
'cli_show': 'json',
|
||||||
|
'cli_show_ascii': 'text'
|
||||||
|
}
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if command_type:
|
if command_type:
|
||||||
response = module.execute(cmds, command_type=command_type)
|
response = module.execute(cmds, command_type=command_type)
|
||||||
|
@ -276,13 +291,27 @@ def execute_show(cmds, module, command_type=None):
|
||||||
response = module.execute(cmds)
|
response = module.execute(cmds)
|
||||||
except ShellError:
|
except ShellError:
|
||||||
clie = get_exception()
|
clie = get_exception()
|
||||||
module.fail_json(msg='Error sending {0}'.format(command),
|
module.fail_json(msg='Error sending {0}'.format(cmds),
|
||||||
|
error=str(clie))
|
||||||
|
except AttributeError:
|
||||||
|
try:
|
||||||
|
if command_type:
|
||||||
|
command_type = command_type_map.get(command_type)
|
||||||
|
module.cli.add_commands(cmds, output=command_type)
|
||||||
|
response = module.cli.run_commands()
|
||||||
|
else:
|
||||||
|
module.cli.add_commands(cmds)
|
||||||
|
response = module.cli.run_commands()
|
||||||
|
except ShellError:
|
||||||
|
clie = get_exception()
|
||||||
|
module.fail_json(msg='Error sending {0}'.format(cmds),
|
||||||
error=str(clie))
|
error=str(clie))
|
||||||
return response
|
return response
|
||||||
|
|
||||||
|
|
||||||
def execute_show_command(command, module, command_type='cli_show'):
|
def execute_show_command(command, module, command_type='cli_show'):
|
||||||
if module.params['transport'] == 'cli':
|
if module.params['transport'] == 'cli':
|
||||||
|
if 'show run' not in command:
|
||||||
command += ' | json'
|
command += ' | json'
|
||||||
cmds = [command]
|
cmds = [command]
|
||||||
response = execute_show(cmds, module)
|
response = execute_show(cmds, module)
|
||||||
|
@ -309,7 +338,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)[0]
|
body = execute_show_command(command, module, command_type='cli_show_ascii')[0]
|
||||||
|
|
||||||
try:
|
try:
|
||||||
match_contact = re.match(contact_regex, body, re.DOTALL)
|
match_contact = re.match(contact_regex, body, re.DOTALL)
|
||||||
|
@ -359,7 +388,7 @@ def main():
|
||||||
results['proposed'] = proposed
|
results['proposed'] = proposed
|
||||||
results['existing'] = existing
|
results['existing'] = existing
|
||||||
results['end_state'] = end_state
|
results['end_state'] = end_state
|
||||||
results['commands'] = cmds
|
results['updates'] = cmds
|
||||||
results['changed'] = changed
|
results['changed'] = changed
|
||||||
|
|
||||||
module.exit_json(**results)
|
module.exit_json(**results)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue