mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
added new functionality to vyos_command
* commands argument now accepts a dict arguments * waitfor has been renamed to wait_for with an alias to waitfor * only show commands are allowd when check mode is specified * config mode is no longer allowed in the command stack * add argument match with valid values any, all
This commit is contained in:
parent
003b6da05a
commit
bbcde06e75
1 changed files with 46 additions and 17 deletions
|
@ -48,6 +48,17 @@ options:
|
||||||
required: false
|
required: false
|
||||||
default: null
|
default: null
|
||||||
aliases: ['waitfor']
|
aliases: ['waitfor']
|
||||||
|
match:
|
||||||
|
description:
|
||||||
|
- The I(match) argument is used in conjunction with the
|
||||||
|
I(wait_for) argument to specify the match policy. Valid
|
||||||
|
values are C(all) or C(any). If the value is set to C(all)
|
||||||
|
then all conditionals in the wait_for must be satisfied. If
|
||||||
|
the value is set to C(any) then only one of the values must be
|
||||||
|
satisfied.
|
||||||
|
required: false
|
||||||
|
default: all
|
||||||
|
choices: ['any', 'all']
|
||||||
retries:
|
retries:
|
||||||
description:
|
description:
|
||||||
- Specifies the number of retries a command should be tried
|
- Specifies the number of retries a command should be tried
|
||||||
|
@ -115,11 +126,12 @@ warnings:
|
||||||
type: list
|
type: list
|
||||||
sample: ['...', '...']
|
sample: ['...', '...']
|
||||||
"""
|
"""
|
||||||
|
from ansible.module_utils.basic import get_exception
|
||||||
from ansible.module_utils.netcmd import CommandRunner, FailedConditionsError
|
from ansible.module_utils.netcli import CommandRunner
|
||||||
|
from ansible.module_utils.netcli import AddCommandError, FailedConditionsError
|
||||||
from ansible.module_utils.vyos import NetworkModule, NetworkError
|
from ansible.module_utils.vyos import NetworkModule, NetworkError
|
||||||
from ansible.module_utils.vyos import vyos_argument_spec, get_exception
|
|
||||||
|
|
||||||
|
VALID_KEYS = ['command', 'output', 'prompt', 'response']
|
||||||
|
|
||||||
def to_lines(stdout):
|
def to_lines(stdout):
|
||||||
for item in stdout:
|
for item in stdout:
|
||||||
|
@ -127,23 +139,35 @@ def to_lines(stdout):
|
||||||
item = str(item).split('\n')
|
item = str(item).split('\n')
|
||||||
yield item
|
yield item
|
||||||
|
|
||||||
|
def parse_commands(module):
|
||||||
|
for cmd in module.params['commands']:
|
||||||
|
if isinstance(cmd, basestring):
|
||||||
|
cmd = dict(command=cmd, output=None)
|
||||||
|
elif 'command' not in cmd:
|
||||||
|
module.fail_json(msg='command keyword argument is required')
|
||||||
|
elif cmd.get('output') not in [None, 'text']:
|
||||||
|
module.fail_json(msg='invalid output specified for command')
|
||||||
|
elif not set(cmd.keys()).issubset(VALID_KEYS):
|
||||||
|
module.fail_json(msg='unknown keyword specified')
|
||||||
|
yield cmd
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
"""Main entry point for Ansible module execution
|
spec = dict(
|
||||||
"""
|
# { command: <str>, output: <str>, prompt: <str>, response: <str> }
|
||||||
argument_spec = dict(
|
commands=dict(type='list', required=True),
|
||||||
commands=dict(type='list'),
|
|
||||||
wait_for=dict(type='list', aliases=['waitfor']),
|
wait_for=dict(type='list', aliases=['waitfor']),
|
||||||
|
match=dict(default='all', choices=['all', 'any']),
|
||||||
|
|
||||||
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 = NetworkModule(argument_spec=argument_spec,
|
module = NetworkModule(argument_spec=spec,
|
||||||
connect_on_load=False,
|
connect_on_load=False,
|
||||||
supports_check_mode=True)
|
supports_check_mode=True)
|
||||||
|
|
||||||
commands = module.params['commands']
|
commands = list(parse_commands(module))
|
||||||
conditionals = module.params['wait_for'] or list()
|
conditionals = module.params['wait_for'] or list()
|
||||||
|
|
||||||
warnings = list()
|
warnings = list()
|
||||||
|
@ -151,21 +175,26 @@ def main():
|
||||||
runner = CommandRunner(module)
|
runner = CommandRunner(module)
|
||||||
|
|
||||||
for cmd in commands:
|
for cmd in commands:
|
||||||
if module.check_mode and not cmd.startswith('show'):
|
if module.check_mode and not cmd['command'].startswith('show'):
|
||||||
warnings.append('only show commands are supported when using '
|
warnings.append('only show commands are supported when using '
|
||||||
'check mode, not executing `%s`' % cmd)
|
'check mode, not executing `%s`' % cmd['command'])
|
||||||
else:
|
else:
|
||||||
if cmd.startswith('conf'):
|
if cmd['command'].startswith('conf'):
|
||||||
module.fail_json(msg='vyos_command does not support running '
|
module.fail_json(msg='vyos_command does not support running '
|
||||||
'config mode commands. Please use '
|
'config mode commands. Please use '
|
||||||
'vyos_config instead')
|
'vyos_config instead')
|
||||||
runner.add_command(cmd)
|
try:
|
||||||
|
runner.add_command(**cmd)
|
||||||
|
except AddCommandError:
|
||||||
|
exc = get_exception()
|
||||||
|
warnings.append('duplicate command detected: %s' % cmd)
|
||||||
|
|
||||||
for item in conditionals:
|
for item in conditionals:
|
||||||
runner.add_conditional(item)
|
runner.add_conditional(item)
|
||||||
|
|
||||||
runner.retries = module.params['retries']
|
runner.retries = module.params['retries']
|
||||||
runner.interval = module.params['interval']
|
runner.interval = module.params['interval']
|
||||||
|
runner.match = module.params['match']
|
||||||
|
|
||||||
try:
|
try:
|
||||||
runner.run()
|
runner.run()
|
||||||
|
@ -176,12 +205,11 @@ def main():
|
||||||
exc = get_exception()
|
exc = get_exception()
|
||||||
module.fail_json(msg=str(exc))
|
module.fail_json(msg=str(exc))
|
||||||
|
|
||||||
result = dict(changed=False, warnings=warnings)
|
result = dict(changed=False, stdout=list())
|
||||||
|
|
||||||
result['stdout'] = list()
|
|
||||||
for cmd in commands:
|
for cmd in commands:
|
||||||
try:
|
try:
|
||||||
output = runner.get_command(cmd)
|
output = runner.get_command(cmd['command'])
|
||||||
except ValueError:
|
except ValueError:
|
||||||
output = 'command not executed due to check_mode, see warnings'
|
output = 'command not executed due to check_mode, see warnings'
|
||||||
result['stdout'].append(output)
|
result['stdout'].append(output)
|
||||||
|
@ -194,3 +222,4 @@ def main():
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue