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

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
This commit is contained in:
Peter Sprygada 2016-06-10 15:37:45 -04:00 committed by Matt Clay
parent 35b9810e39
commit b0bdf09a00

View file

@ -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()
retries = module.params['retries'] warnings = list()
interval = module.params['interval']
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)
for item in conditionals:
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))
except AttributeError:
exc = get_exception() exc = get_exception()
module.fail_json(msg=exc.message) module.fail_json(msg=str(exc), failed_conditions=exc.failed_conditions)
except NetworkError:
exc = get_exception()
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:
break
time.sleep(interval)
retries -= 1
else:
failed_conditions = [item.raw for item in queue]
module.fail_json(msg='timeout waiting for value', failed_conditions=failed_conditions)
result['warnings'] = warnings
result['connected'] = module.connected
result['stdout_lines'] = list(to_lines(result['stdout'])) result['stdout_lines'] = list(to_lines(result['stdout']))
return module.exit_json(**result)
from ansible.module_utils.basic import * module.exit_json(**result)
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()