2016-01-25 12:55:24 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
#
|
|
|
|
# This file is part of Ansible
|
|
|
|
#
|
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
|
2017-08-16 03:16:38 +00:00
|
|
|
ANSIBLE_METADATA = {'metadata_version': '1.1',
|
2017-03-14 16:07:22 +00:00
|
|
|
'status': ['preview'],
|
2017-08-16 04:10:36 +00:00
|
|
|
'supported_by': 'network'}
|
2017-03-14 16:07:22 +00:00
|
|
|
|
2016-12-06 10:35:05 +00:00
|
|
|
|
2016-01-25 12:55:24 +00:00
|
|
|
DOCUMENTATION = """
|
|
|
|
---
|
|
|
|
module: eos_command
|
|
|
|
version_added: "2.1"
|
2016-06-06 00:34:31 +00:00
|
|
|
author: "Peter Sprygada (@privateip)"
|
2016-08-31 13:31:16 +00:00
|
|
|
short_description: Run arbitrary commands on an Arista EOS device
|
2016-01-25 12:55:24 +00:00
|
|
|
description:
|
2016-08-02 20:46:08 +00:00
|
|
|
- Sends an arbitrary set of commands to an EOS node and returns the results
|
|
|
|
read from the device. This module includes an
|
2016-01-25 12:55:24 +00:00
|
|
|
argument that will cause the module to wait for a specific condition
|
|
|
|
before returning or timing out if the condition is not met.
|
2017-03-05 08:37:01 +00:00
|
|
|
extends_documentation_fragment: eos
|
2017-08-23 15:09:31 +00:00
|
|
|
notes:
|
|
|
|
- Tested against EOS 4.15
|
2016-01-25 12:55:24 +00:00
|
|
|
options:
|
|
|
|
commands:
|
|
|
|
description:
|
|
|
|
- The commands to send to the remote EOS device over the
|
|
|
|
configured provider. The resulting output from the command
|
2016-08-02 20:46:08 +00:00
|
|
|
is returned. If the I(wait_for) argument is provided, the
|
2016-01-25 12:55:24 +00:00
|
|
|
module is not returned until the condition is satisfied or
|
2016-08-02 20:46:08 +00:00
|
|
|
the number of I(retries) has been exceeded.
|
2016-01-25 12:55:24 +00:00
|
|
|
required: true
|
2016-06-06 00:34:31 +00:00
|
|
|
wait_for:
|
2016-01-25 12:55:24 +00:00
|
|
|
description:
|
|
|
|
- Specifies what to evaluate from the output of the command
|
|
|
|
and what conditionals to apply. This argument will cause
|
|
|
|
the task to wait for a particular conditional to be true
|
|
|
|
before moving forward. If the conditional is not true
|
2018-02-16 06:18:12 +00:00
|
|
|
by the configured retries, the task fails.
|
|
|
|
Note - With I(wait_for) the value in C(result['stdout']) can be accessed
|
|
|
|
using C(result), that is to access C(result['stdout'][0]) use C(result[0]) See examples.
|
2016-06-06 00:34:31 +00:00
|
|
|
aliases: ['waitfor']
|
|
|
|
version_added: "2.2"
|
2016-08-22 16:48:53 +00:00
|
|
|
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 I(wait_for) must be satisfied. If
|
|
|
|
the value is set to C(any) then only one of the values must be
|
|
|
|
satisfied.
|
|
|
|
default: all
|
|
|
|
choices: ['any', 'all']
|
|
|
|
version_added: "2.2"
|
2016-01-25 12:55:24 +00:00
|
|
|
retries:
|
|
|
|
description:
|
2016-05-06 21:34:31 +00:00
|
|
|
- Specifies the number of retries a command should be tried
|
2016-01-25 12:55:24 +00:00
|
|
|
before it is considered failed. The command is run on the
|
2016-08-02 20:46:08 +00:00
|
|
|
target device every retry and evaluated against the I(wait_for)
|
|
|
|
conditionals.
|
2016-01-25 12:55:24 +00:00
|
|
|
default: 10
|
|
|
|
interval:
|
|
|
|
description:
|
|
|
|
- Configures the interval in seconds to wait between retries
|
|
|
|
of the command. If the command does not pass the specified
|
|
|
|
conditional, the interval indicates how to long to wait before
|
|
|
|
trying the command again.
|
|
|
|
default: 1
|
|
|
|
"""
|
|
|
|
|
|
|
|
EXAMPLES = """
|
2016-10-13 14:47:50 +00:00
|
|
|
- name: run show version on remote devices
|
2016-08-22 16:48:53 +00:00
|
|
|
eos_command:
|
|
|
|
commands: show version
|
|
|
|
|
|
|
|
- name: run show version and check to see if output contains Arista
|
|
|
|
eos_command:
|
|
|
|
commands: show version
|
|
|
|
wait_for: result[0] contains Arista
|
|
|
|
|
|
|
|
- name: run multiple commands on remote nodes
|
2017-02-07 21:39:24 +00:00
|
|
|
eos_command:
|
2016-01-25 12:55:24 +00:00
|
|
|
commands:
|
2016-08-22 16:48:53 +00:00
|
|
|
- show version
|
|
|
|
- show interfaces
|
2016-01-25 12:55:24 +00:00
|
|
|
|
2016-10-13 14:47:50 +00:00
|
|
|
- name: run multiple commands and evaluate the output
|
2016-08-22 16:48:53 +00:00
|
|
|
eos_command:
|
2016-01-25 12:55:24 +00:00
|
|
|
commands:
|
|
|
|
- show version
|
2016-08-22 16:48:53 +00:00
|
|
|
- show interfaces
|
|
|
|
wait_for:
|
|
|
|
- result[0] contains Arista
|
|
|
|
- result[1] contains Loopback0
|
|
|
|
|
|
|
|
- name: run commands and specify the output format
|
|
|
|
eos_command:
|
|
|
|
commands:
|
|
|
|
- command: show version
|
|
|
|
output: json
|
2018-02-16 06:18:12 +00:00
|
|
|
|
|
|
|
- name: using cli transport, check whether the switch is in maintenance mode
|
|
|
|
eos_command:
|
|
|
|
commands: show maintenance
|
|
|
|
wait_for: result[0] contains 'Under Maintenance'
|
|
|
|
|
|
|
|
- name: using cli transport, check whether the switch is in maintenance mode using json output
|
|
|
|
eos_command:
|
|
|
|
commands: show maintenance | json
|
|
|
|
wait_for: result[0].units.System.state eq 'underMaintenance'
|
|
|
|
|
|
|
|
- name: "using eapi transport check whether the switch is in maintenance,
|
|
|
|
with 8 retries and 2 second interval between retries"
|
|
|
|
eos_command:
|
|
|
|
commands: show maintenance
|
|
|
|
wait_for: result[0]['units']['System']['state'] eq 'underMaintenance'
|
|
|
|
interval: 2
|
|
|
|
retries: 8
|
|
|
|
provider:
|
|
|
|
transport: eapi
|
2016-01-25 12:55:24 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
RETURN = """
|
2017-04-13 15:59:23 +00:00
|
|
|
stdout:
|
|
|
|
description: The set of responses from the commands
|
|
|
|
returned: always apart from low level errors (such as action plugin)
|
|
|
|
type: list
|
|
|
|
sample: ['...', '...']
|
|
|
|
stdout_lines:
|
|
|
|
description: The value of stdout split into a list
|
|
|
|
returned: always apart from low level errors (such as action plugin)
|
|
|
|
type: list
|
|
|
|
sample: [['...', '...'], ['...'], ['...']]
|
2016-02-09 19:35:18 +00:00
|
|
|
failed_conditions:
|
2017-04-13 15:59:23 +00:00
|
|
|
description: The list of conditionals that have failed
|
2016-10-13 14:47:50 +00:00
|
|
|
returned: failed
|
2016-01-25 12:55:24 +00:00
|
|
|
type: list
|
|
|
|
sample: ['...', '...']
|
|
|
|
"""
|
2017-01-27 13:45:13 +00:00
|
|
|
import time
|
2016-09-19 16:25:32 +00:00
|
|
|
|
2017-01-27 13:45:13 +00:00
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
2017-12-26 03:03:21 +00:00
|
|
|
from ansible.module_utils._text import to_native
|
2016-09-27 21:06:03 +00:00
|
|
|
from ansible.module_utils.six import string_types
|
2017-12-03 16:12:30 +00:00
|
|
|
from ansible.module_utils.network.common.parsing import Conditional
|
2017-12-13 07:37:52 +00:00
|
|
|
from ansible.module_utils.network.common.utils import ComplexList
|
2017-12-03 16:12:30 +00:00
|
|
|
from ansible.module_utils.network.eos.eos import run_commands
|
2017-12-13 07:37:52 +00:00
|
|
|
from ansible.module_utils.network.eos.eos import eos_argument_spec, check_args
|
2017-01-27 13:45:13 +00:00
|
|
|
|
2016-08-22 16:48:53 +00:00
|
|
|
VALID_KEYS = ['command', 'output', 'prompt', 'response']
|
2016-01-25 12:55:24 +00:00
|
|
|
|
2017-12-07 16:27:06 +00:00
|
|
|
|
2016-06-06 00:34:31 +00:00
|
|
|
def to_lines(stdout):
|
2017-01-27 13:45:13 +00:00
|
|
|
lines = list()
|
2016-02-09 19:35:18 +00:00
|
|
|
for item in stdout:
|
2016-09-27 21:06:03 +00:00
|
|
|
if isinstance(item, string_types):
|
2016-02-09 19:35:18 +00:00
|
|
|
item = str(item).split('\n')
|
2017-01-27 13:45:13 +00:00
|
|
|
lines.append(item)
|
|
|
|
return lines
|
|
|
|
|
2017-12-07 16:27:06 +00:00
|
|
|
|
2017-01-27 13:45:13 +00:00
|
|
|
def parse_commands(module, warnings):
|
2017-12-13 07:37:52 +00:00
|
|
|
spec = dict(
|
|
|
|
command=dict(key=True),
|
|
|
|
output=dict(),
|
|
|
|
prompt=dict(),
|
|
|
|
answer=dict()
|
|
|
|
)
|
|
|
|
|
|
|
|
transform = ComplexList(spec, module)
|
|
|
|
commands = transform(module.params['commands'])
|
|
|
|
|
2017-08-22 18:02:55 +00:00
|
|
|
if module.check_mode:
|
|
|
|
for item in list(commands):
|
|
|
|
if not item['command'].startswith('show'):
|
|
|
|
warnings.append(
|
|
|
|
'Only show commands are supported when using check_mode, not '
|
|
|
|
'executing %s' % item['command']
|
|
|
|
)
|
|
|
|
commands.remove(item)
|
2017-02-03 03:10:14 +00:00
|
|
|
|
2017-01-27 13:45:13 +00:00
|
|
|
return commands
|
|
|
|
|
2017-12-07 16:27:06 +00:00
|
|
|
|
2017-01-27 13:45:13 +00:00
|
|
|
def to_cli(obj):
|
|
|
|
cmd = obj['command']
|
|
|
|
if obj.get('output') == 'json':
|
|
|
|
cmd += ' | json'
|
|
|
|
return cmd
|
2016-08-22 16:48:53 +00:00
|
|
|
|
2017-12-07 16:27:06 +00:00
|
|
|
|
2016-01-25 12:55:24 +00:00
|
|
|
def main():
|
2017-01-27 13:45:13 +00:00
|
|
|
"""entry point for module execution
|
|
|
|
"""
|
|
|
|
argument_spec = dict(
|
2017-12-13 07:37:52 +00:00
|
|
|
commands=dict(type='list', required=True),
|
2016-08-22 16:48:53 +00:00
|
|
|
|
2016-06-06 00:34:31 +00:00
|
|
|
wait_for=dict(type='list', aliases=['waitfor']),
|
2016-08-22 16:48:53 +00:00
|
|
|
match=dict(default='all', choices=['all', 'any']),
|
|
|
|
|
2016-01-25 12:55:24 +00:00
|
|
|
retries=dict(default=10, type='int'),
|
|
|
|
interval=dict(default=1, type='int')
|
|
|
|
)
|
|
|
|
|
2017-02-14 01:22:10 +00:00
|
|
|
argument_spec.update(eos_argument_spec)
|
2016-06-06 00:34:31 +00:00
|
|
|
|
2017-02-14 01:22:10 +00:00
|
|
|
module = AnsibleModule(argument_spec=argument_spec,
|
|
|
|
supports_check_mode=True)
|
2016-01-25 12:55:24 +00:00
|
|
|
|
2017-01-27 13:45:13 +00:00
|
|
|
result = {'changed': False}
|
|
|
|
|
2017-02-14 01:22:10 +00:00
|
|
|
warnings = list()
|
2017-12-13 07:37:52 +00:00
|
|
|
check_args(module, warnings)
|
2017-01-27 13:45:13 +00:00
|
|
|
commands = parse_commands(module, warnings)
|
|
|
|
if warnings:
|
|
|
|
result['warnings'] = warnings
|
|
|
|
|
|
|
|
wait_for = module.params['wait_for'] or list()
|
2017-02-14 18:47:29 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
conditionals = [Conditional(c) for c in wait_for]
|
2017-12-26 03:03:21 +00:00
|
|
|
except AttributeError as exc:
|
|
|
|
module.fail_json(msg=to_native(exc))
|
2017-01-27 13:45:13 +00:00
|
|
|
|
|
|
|
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]
|
2018-02-21 06:14:18 +00:00
|
|
|
msg = 'One or more conditional statements have not been satisfied'
|
2017-01-27 13:45:13 +00:00
|
|
|
module.fail_json(msg=msg, failed_conditions=failed_conditions)
|
|
|
|
|
|
|
|
result.update({
|
|
|
|
'changed': False,
|
|
|
|
'stdout': responses,
|
|
|
|
'stdout_lines': to_lines(responses)
|
|
|
|
})
|
2016-01-25 12:55:24 +00:00
|
|
|
|
2016-06-06 00:34:31 +00:00
|
|
|
module.exit_json(**result)
|
2016-01-25 12:55:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2016-06-06 00:34:31 +00:00
|
|
|
main()
|