network command modules cleanup (#45242)

* Standardize _command modules

* Centralize command transformation process

* Fix check mode error test
This commit is contained in:
Nathaniel Case 2018-09-13 08:55:35 -04:00 committed by GitHub
parent df768914a1
commit 285b927889
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 115 additions and 204 deletions

View file

@ -124,38 +124,21 @@ failed_conditions:
"""
import time
from ansible.module_utils._text import to_text
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.network.common.parsing import Conditional, FailedConditionalError
from ansible.module_utils.network.common.utils import ComplexList
from ansible.module_utils.network.common.utils import transform_commands, to_lines
from ansible.module_utils.network.nxos.nxos import check_args, nxos_argument_spec, run_commands
from ansible.module_utils.six import string_types
from ansible.module_utils._text import to_native
def to_lines(stdout):
lines = list()
for item in stdout:
if isinstance(item, string_types):
item = str(item).split('\n')
lines.append(item)
return lines
def parse_commands(module, warnings):
transform = ComplexList(dict(
command=dict(key=True),
output=dict(),
prompt=dict(),
answer=dict()
), module)
commands = transform(module.params['commands'])
commands = transform_commands(module)
if module.check_mode:
for item in list(commands):
if not item['command'].startswith('show'):
warnings.append(
'Only show commands are supported when using check_mode, not '
'Only show commands are supported when using check mode, not '
'executing %s' % item['command']
)
commands.remove(item)
@ -189,19 +172,16 @@ def main():
module = AnsibleModule(argument_spec=argument_spec,
supports_check_mode=True)
result = {'changed': False}
warnings = list()
result = {'changed': False, 'warnings': warnings}
check_args(module, warnings)
commands = parse_commands(module, warnings)
result['warnings'] = warnings
wait_for = module.params['wait_for'] or list()
try:
conditionals = [Conditional(c) for c in wait_for]
except AttributeError as exc:
module.fail_json(msg=to_native(exc))
module.fail_json(msg=to_text(exc))
retries = module.params['retries']
interval = module.params['interval']
@ -218,7 +198,7 @@ def main():
break
conditionals.remove(item)
except FailedConditionalError as exc:
module.fail_json(msg=to_native(exc))
module.fail_json(msg=to_text(exc))
if not conditionals:
break
@ -233,7 +213,7 @@ def main():
result.update({
'stdout': responses,
'stdout_lines': to_lines(responses)
'stdout_lines': list(to_lines(responses)),
})
module.exit_json(**result)