diff options
author | Peter Sprygada <privateip@users.noreply.github.com> | 2017-07-17 21:23:38 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-07-17 21:23:38 -0400 |
commit | 8e2dcaf9f64abfda3077a2cf5ead590f5f6dc91c (patch) | |
tree | 6896d8f97921eee7d799b84ff49d16e3cacf4d21 /lib/ansible/modules/network/asa/asa_command.py | |
parent | e976f299f81381997c0c1b89f0bafb8e30f0fefc (diff) | |
download | ansible-8e2dcaf9f64abfda3077a2cf5ead590f5f6dc91c.tar.gz |
update asa to use network_cli connection plugin (#26899)
* WIP update asa to use network_cli connection plugin
* add asa.py to cliconf plugins
* update asa.py terminal plugin to support regexp and events
* update constants to map asa modules to asa action handler
* update asa action handler to implement persistent connections
* update asa shared module to use persistent connections
* update asa_command module to use new connection
* fixed pep8 issues
Diffstat (limited to 'lib/ansible/modules/network/asa/asa_command.py')
-rw-r--r-- | lib/ansible/modules/network/asa/asa_command.py | 115 |
1 files changed, 47 insertions, 68 deletions
diff --git a/lib/ansible/modules/network/asa/asa_command.py b/lib/ansible/modules/network/asa/asa_command.py index c28df85d28..5e927cf2c6 100644 --- a/lib/ansible/modules/network/asa/asa_command.py +++ b/lib/ansible/modules/network/asa/asa_command.py @@ -133,28 +133,18 @@ failed_conditions: type: list sample: ['...', '...'] """ -from ansible.module_utils.basic import get_exception -from ansible.module_utils.netcli import CommandRunner -from ansible.module_utils.netcli import AddCommandError, FailedConditionsError -from ansible.module_utils.asa import NetworkModule, NetworkError +from ansible.module_utils.basic import AnsibleModule +from ansible.module_utils.asa import asa_argument_spec, check_args +from ansible.module_utils.asa import run_commands +from ansible.module_utils.six import string_types -VALID_KEYS = ['command', 'prompt', 'response'] def to_lines(stdout): for item in stdout: - if isinstance(item, basestring): + if isinstance(item, string_types): item = str(item).split('\n') 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 not set(cmd.keys()).issubset(VALID_KEYS): - module.fail_json(msg='unknown keyword specified') - yield cmd def main(): spec = dict( @@ -168,59 +158,48 @@ def main(): interval=dict(default=1, type='int') ) - module = NetworkModule(argument_spec=spec, - connect_on_load=False, - supports_check_mode=True) - - commands = list(parse_commands(module)) - conditionals = module.params['wait_for'] or list() - - warnings = list() - - runner = CommandRunner(module) - - for cmd in commands: - if module.check_mode and not cmd['command'].startswith('show'): - warnings.append('only show commands are supported when using ' - 'check mode, not executing `%s`' % cmd['command']) - else: - if cmd['command'].startswith('conf'): - module.fail_json(msg='asa_command does not support running ' - 'config mode commands. Please use ' - 'asa_config instead') - try: - runner.add_command(**cmd) - except AddCommandError: - exc = get_exception() - warnings.append('duplicate command detected: %s' % cmd) - - for item in conditionals: - runner.add_conditional(item) - - runner.retries = module.params['retries'] - runner.interval = module.params['interval'] - runner.match = module.params['match'] - - try: - runner.run() - except FailedConditionsError: - exc = get_exception() - 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, stdout=list()) - - for cmd in commands: - try: - output = runner.get_command(cmd['command']) - except ValueError: - output = 'command not executed due to check_mode, see warnings' - result['stdout'].append(output) - - result['warnings'] = warnings - result['stdout_lines'] = list(to_lines(result['stdout'])) + spec.update(asa_argument_spec) + + module = AnsibleModule(argument_spec=spec, supports_check_mode=True) + check_args(module) + + result = {'changed': False} + + wait_for = module.params['wait_for'] or list() + conditionals = [Conditional(c) for c in wait_for] + + commands = module.params['commands'] + retries = module.params['retries'] + interval = module.params['interval'] + match = module.params['match'] + + while retries > 0: + responses = run_commands(module, commands) + + for item in list(conditionals): + if item(responses): + if match == 'any': + conditionals = list() + break + conditionals.remove(item) + + if not conditionals: + break + + time.sleep(interval) + retries -= 1 + + if conditionals: + failed_conditions = [item.raw for item in conditionals] + msg = 'One or more conditional statements have not be satisfied' + module.fail_json(msg=msg, failed_conditions=failed_conditions) + + + result.update({ + 'changed': False, + 'stdout': responses, + 'stdout_lines': list(to_lines(responses)) + }) module.exit_json(**result) |