refactor ios_command to use CommandRunner class

* fixes running commands in check mode.  now only show commands are allowd
* renamed waitfor to wait_for with alias to waitfor for consistency
reviewable/pr18780/r1
Peter Sprygada 8 years ago
parent 1f5335d5e0
commit 2be917851d

@ -37,7 +37,7 @@ options:
module is not returned until the condition is satisfied or module is not returned until the condition is satisfied or
the number of retires as expired. the number of retires as expired.
required: true required: true
waitfor: wait_for:
description: description:
- List of conditions to evaluate against the output of the - List of conditions to evaluate against the output of the
command. The task will wait for a each condition to be true command. The task will wait for a each condition to be true
@ -46,6 +46,8 @@ options:
See examples. See examples.
required: false required: false
default: null default: null
aliases: ['waitfor']
version_added: "2.2"
retries: retries:
description: description:
- Specifies the number of retries a command should by tried - Specifies the number of retries a command should by tried
@ -104,10 +106,9 @@ failed_conditions:
type: list type: list
sample: ['...', '...'] sample: ['...', '...']
""" """
from ansible.module_utils.netcmd import CommandRunner, FailedConditionsError
import time from ansible.module_utils.network import NetworkError
import shlex from ansible.module_utils.ios import get_module
import re
def to_lines(stdout): def to_lines(stdout):
for item in stdout: for item in stdout:
@ -118,54 +119,62 @@ def to_lines(stdout):
def main(): def main():
spec = dict( spec = dict(
commands=dict(type='list'), commands=dict(type='list'),
waitfor=dict(type='list'), wait_for=dict(type='list'),
retries=dict(default=10, type='int'), retries=dict(default=10, type='int'),
interval=dict(default=1, type='int') interval=dict(default=1, type='int')
) )
module = get_module(argument_spec=spec, module = get_module(argument_spec=spec,
connect_on_load=False,
supports_check_mode=True) supports_check_mode=True)
commands = module.params['commands'] commands = module.params['commands']
conditionals = module.params['wait_for'] or list()
warnings = list()
runner = CommandRunner(module)
for cmd in commands:
if module.check_mode and not cmd.startswith('show'):
warnings.append('only show commands are supported when using '
'check mode, not executing `%s`' % cmd)
else:
runner.add_command(cmd)
retries = module.params['retries'] for item in conditionals:
interval = module.params['interval'] runner.add_conditional(item)
runner.retries = module.params['retries']
runner.interval = module.params['interval']
try: try:
queue = set() runner.run()
for entry in (module.params['waitfor'] or list()): except FailedConditionsError:
queue.add(Conditional(entry)) exc = get_exception()
except AttributeError: module.fail_json(msg=str(exc), failed_conditions=exc.failed_conditions)
except NetworkError:
exc = get_exception() exc = get_exception()
module.fail_json(msg=exc.message) module.fail_json(msg=str(exc))
result = dict(changed=False) result = dict(changed=False)
while retries > 0: result['stdout'] = list()
response = module.execute(commands) for cmd in commands:
result['stdout'] = response try:
output = runner.get_command(cmd)
except ValueError:
output = 'command not executed due to check_mode, see warnings'
result['stdout'].append(output)
for item in list(queue):
if item(response):
queue.remove(item)
if not queue: result['warnings'] = warnings
break result['connected'] = module.connected
result['stdout_lines'] = list(to_lines(result['stdout']))
time.sleep(interval) module.exit_json(**result)
retries -= 1
else:
failed_conditions = [item.raw for item in queue]
module.fail_json(msg='timeout waiting for value', failed_conditions=failed_conditions)
result['stdout_lines'] = list(to_lines(result['stdout']))
return module.exit_json(**result)
from ansible.module_utils.basic import *
from ansible.module_utils.urls import *
from ansible.module_utils.shell import *
from ansible.module_utils.netcfg import *
from ansible.module_utils.ios import *
if __name__ == '__main__': if __name__ == '__main__':
main() main()

Loading…
Cancel
Save