mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Contributing lib/ansible/modules/network/cloudengine/ce_command.py module to manage HUAWEI data center CloudEngine (#21669)
* update ce_command.py update ce_command.py * update ce_command update ce_command * update ce_command.py update ce_command.py * update ce_command module update ce_command module * update ce_command module update ce_command module
This commit is contained in:
parent
f39fa7fd22
commit
0fe0538a43
1 changed files with 119 additions and 123 deletions
|
@ -28,13 +28,12 @@ DOCUMENTATION = """
|
||||||
module: ce_command
|
module: ce_command
|
||||||
version_added: "2.3"
|
version_added: "2.3"
|
||||||
author: "JackyGao2016 (@CloudEngine-Ansible)"
|
author: "JackyGao2016 (@CloudEngine-Ansible)"
|
||||||
short_description: Run arbitrary command on HUAWEI CloudEngine devices
|
short_description: Run arbitrary command on HUAWEI CloudEngine devices.
|
||||||
description:
|
description:
|
||||||
- Sends an arbitrary command to an HUAWEI CloudEngine node and returns
|
- Sends an arbitrary command to an HUAWEI CloudEngine node and returns
|
||||||
the results read from the device. The ce_command module includes an
|
the results read from the device. The ce_command module includes an
|
||||||
argument that will cause the module to wait for a specific condition
|
argument that will cause the module to wait for a specific condition
|
||||||
before returning or timing out if the condition is not met.
|
before returning or timing out if the condition is not met.
|
||||||
extends_documentation_fragment: cloudengine
|
|
||||||
options:
|
options:
|
||||||
commands:
|
commands:
|
||||||
description:
|
description:
|
||||||
|
@ -85,49 +84,49 @@ options:
|
||||||
EXAMPLES = """
|
EXAMPLES = """
|
||||||
# Note: examples below use the following provider dict to handle
|
# Note: examples below use the following provider dict to handle
|
||||||
# transport and authentication to the node.
|
# transport and authentication to the node.
|
||||||
---
|
|
||||||
vars:
|
|
||||||
cli:
|
|
||||||
host: "{{ inventory_hostname }}"
|
|
||||||
username: admin
|
|
||||||
password: admin
|
|
||||||
transport: cli
|
|
||||||
|
|
||||||
---
|
- name: CloudEngine command test
|
||||||
- name: run display version on remote devices
|
hosts: cloudengine
|
||||||
ce_command:
|
connection: local
|
||||||
commands: display version
|
gather_facts: no
|
||||||
provider: "{{ cli }}"
|
vars:
|
||||||
|
cli:
|
||||||
|
host: "{{ inventory_hostname }}"
|
||||||
|
port: "{{ ansible_ssh_port }}"
|
||||||
|
username: "{{ username }}"
|
||||||
|
password: "{{ password }}"
|
||||||
|
transport: cli
|
||||||
|
|
||||||
- name: run display version and check to see if output contains HUAWEI
|
tasks:
|
||||||
ce_command:
|
|
||||||
commands: display version
|
|
||||||
wait_for: result[0] contains HUAWEI
|
|
||||||
provider: "{{ cli }}"
|
|
||||||
|
|
||||||
- name: run multiple commands on remote nodes
|
tasks:
|
||||||
ce_command:
|
- name: "Run display version on remote devices"
|
||||||
commands:
|
ce_command:
|
||||||
- display version
|
commands: display version
|
||||||
- display device
|
provider: "{{ cli }}"
|
||||||
provider: "{{ cli }}"
|
|
||||||
|
|
||||||
- name: run multiple commands and evaluate the output
|
- name: "Run display version and check to see if output contains HUAWEI"
|
||||||
ce_command:
|
ce_command:
|
||||||
commands:
|
commands: display version
|
||||||
- display version
|
wait_for: result[0] contains HUAWEI
|
||||||
- display device
|
provider: "{{ cli }}"
|
||||||
wait_for:
|
|
||||||
- result[0] contains HUAWEI
|
|
||||||
- result[1] contains Device
|
|
||||||
provider: "{{ cli }}"
|
|
||||||
|
|
||||||
- name: run commands and specify the output format
|
- name: "Run multiple commands on remote nodes"
|
||||||
ce_command:
|
ce_command:
|
||||||
commands:
|
commands:
|
||||||
- command: display version
|
- display version
|
||||||
output: json
|
- display device
|
||||||
provider: "{{ cli }}"
|
provider: "{{ cli }}"
|
||||||
|
|
||||||
|
- name: "Run multiple commands and evaluate the output"
|
||||||
|
ce_command:
|
||||||
|
commands:
|
||||||
|
- display version
|
||||||
|
- display device
|
||||||
|
wait_for:
|
||||||
|
- result[0] contains HUAWEI
|
||||||
|
- result[1] contains Device
|
||||||
|
provider: "{{ cli }}"
|
||||||
"""
|
"""
|
||||||
|
|
||||||
RETURN = """
|
RETURN = """
|
||||||
|
@ -151,46 +150,55 @@ failed_conditions:
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
from ansible.module_utils.basic import get_exception
|
import time
|
||||||
from ansible.module_utils.network import NetworkModule, NetworkError
|
|
||||||
from ansible.module_utils.netcli import CommandRunner
|
|
||||||
from ansible.module_utils.netcli import FailedConditionsError
|
|
||||||
from ansible.module_utils.netcli import FailedConditionalError
|
|
||||||
from ansible.module_utils.netcli import AddCommandError, AddConditionError
|
|
||||||
import ansible.module_utils.cloudengine
|
|
||||||
|
|
||||||
|
from ansible.module_utils.ce import run_commands
|
||||||
VALID_KEYS = ['command', 'output', 'prompt', 'response']
|
from ansible.module_utils.pycompat24 import get_exception
|
||||||
|
from ansible.module_utils.basic import AnsibleModule
|
||||||
|
from ansible.module_utils.netcli import Conditional
|
||||||
|
from ansible.module_utils.network_common import ComplexList
|
||||||
|
from ansible.module_utils.ce import ce_argument_spec, check_args
|
||||||
|
|
||||||
|
|
||||||
def to_lines(stdout):
|
def to_lines(stdout):
|
||||||
""" to lines """
|
lines = list()
|
||||||
|
|
||||||
for item in stdout:
|
for item in stdout:
|
||||||
if isinstance(item, basestring):
|
if isinstance(item, basestring):
|
||||||
item = str(item).split('\n')
|
item = str(item).split('\n')
|
||||||
yield item
|
lines.append(item)
|
||||||
|
return lines
|
||||||
|
|
||||||
|
|
||||||
def parse_commands(module):
|
def parse_commands(module, warnings):
|
||||||
""" parse commands """
|
transform = ComplexList(dict(
|
||||||
|
command=dict(key=True),
|
||||||
|
output=dict(),
|
||||||
|
prompt=dict(),
|
||||||
|
response=dict()
|
||||||
|
), module)
|
||||||
|
|
||||||
for cmd in module.params['commands']:
|
commands = transform(module.params['commands'])
|
||||||
if isinstance(cmd, basestring):
|
|
||||||
cmd = dict(command=cmd, output=None)
|
for _, item in enumerate(commands):
|
||||||
elif 'command' not in cmd:
|
if module.check_mode and not item['command'].startswith('dis'):
|
||||||
module.fail_json(msg='command keyword argument is required')
|
warnings.append(
|
||||||
elif cmd.get('output') not in [None, 'text', 'json']:
|
'Only display commands are supported when using check_mode, not '
|
||||||
module.fail_json(msg='invalid output specified for command')
|
'executing %s' % item['command']
|
||||||
elif not set(cmd.keys()).issubset(VALID_KEYS):
|
)
|
||||||
module.fail_json(msg='unknown keyword specified')
|
|
||||||
yield cmd
|
return commands
|
||||||
|
|
||||||
|
|
||||||
|
def to_cli(obj):
|
||||||
|
cmd = obj['command']
|
||||||
|
return cmd
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
""" main """
|
"""entry point for module execution
|
||||||
|
"""
|
||||||
spec = dict(
|
argument_spec = dict(
|
||||||
|
# { command: <str>, output: <str>, prompt: <str>, response: <str> }
|
||||||
commands=dict(type='list', required=True),
|
commands=dict(type='list', required=True),
|
||||||
|
|
||||||
wait_for=dict(type='list', aliases=['waitfor']),
|
wait_for=dict(type='list', aliases=['waitfor']),
|
||||||
|
@ -200,67 +208,55 @@ def main():
|
||||||
interval=dict(default=1, type='int')
|
interval=dict(default=1, type='int')
|
||||||
)
|
)
|
||||||
|
|
||||||
module = NetworkModule(argument_spec=spec,
|
argument_spec.update(ce_argument_spec)
|
||||||
|
|
||||||
|
module = AnsibleModule(argument_spec=argument_spec,
|
||||||
supports_check_mode=True)
|
supports_check_mode=True)
|
||||||
|
|
||||||
commands = list(parse_commands(module))
|
result = {'changed': False}
|
||||||
conditionals = module.params['wait_for'] or list()
|
|
||||||
|
|
||||||
warnings = list()
|
warnings = list()
|
||||||
|
check_args(module, warnings)
|
||||||
runner = CommandRunner(module)
|
commands = parse_commands(module, warnings)
|
||||||
|
|
||||||
for cmd in commands:
|
|
||||||
if module.check_mode and not cmd['command'].startswith('dis'):
|
|
||||||
warnings.append('only display commands are supported when using '
|
|
||||||
'check mode, not executing `%s`' % cmd['command'])
|
|
||||||
else:
|
|
||||||
if cmd['command'].startswith('sys'):
|
|
||||||
module.fail_json(msg='ce_command does not support running '
|
|
||||||
'config mode commands. Please use '
|
|
||||||
'ce_config instead')
|
|
||||||
try:
|
|
||||||
runner.add_command(**cmd)
|
|
||||||
except AddCommandError:
|
|
||||||
exc = get_exception()
|
|
||||||
warnings.append('duplicate command detected: %s' % cmd)
|
|
||||||
|
|
||||||
try:
|
|
||||||
for item in conditionals:
|
|
||||||
runner.add_conditional(item)
|
|
||||||
except AddConditionError:
|
|
||||||
exc = get_exception()
|
|
||||||
module.fail_json(msg=str(exc), condition=exc.condition)
|
|
||||||
|
|
||||||
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 FailedConditionalError:
|
|
||||||
exc = get_exception()
|
|
||||||
module.fail_json(
|
|
||||||
msg=str(exc), failed_conditional=exc.failed_conditional)
|
|
||||||
except NetworkError:
|
|
||||||
exc = get_exception()
|
|
||||||
module.fail_json(msg=str(exc), **exc.kwargs)
|
|
||||||
|
|
||||||
result = dict(changed=False)
|
|
||||||
|
|
||||||
result['stdout'] = list()
|
|
||||||
for cmd in commands:
|
|
||||||
try:
|
|
||||||
output = runner.get_command(cmd['command'], cmd.get('output'))
|
|
||||||
except ValueError:
|
|
||||||
output = 'command not executed due to check_mode, see warnings'
|
|
||||||
result['stdout'].append(output)
|
|
||||||
|
|
||||||
result['warnings'] = warnings
|
result['warnings'] = warnings
|
||||||
result['stdout_lines'] = list(to_lines(result['stdout']))
|
|
||||||
|
wait_for = module.params['wait_for'] or list()
|
||||||
|
|
||||||
|
try:
|
||||||
|
conditionals = [Conditional(c) for c in wait_for]
|
||||||
|
except AttributeError:
|
||||||
|
exc = get_exception()
|
||||||
|
module.fail_json(msg=str(exc))
|
||||||
|
|
||||||
|
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({
|
||||||
|
'stdout': responses,
|
||||||
|
'stdout_lines': to_lines(responses)
|
||||||
|
})
|
||||||
|
|
||||||
module.exit_json(**result)
|
module.exit_json(**result)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue