2016-01-25 13:55:24 +01: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/>.
|
|
|
|
#
|
|
|
|
|
|
|
|
DOCUMENTATION = """
|
|
|
|
---
|
|
|
|
module: eos_command
|
|
|
|
version_added: "2.1"
|
2016-06-06 02:34:31 +02:00
|
|
|
author: "Peter Sprygada (@privateip)"
|
2016-08-31 15:31:16 +02:00
|
|
|
short_description: Run arbitrary commands on an Arista EOS device
|
2016-01-25 13:55:24 +01:00
|
|
|
description:
|
2016-08-02 22:46:08 +02: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 13:55:24 +01:00
|
|
|
argument that will cause the module to wait for a specific condition
|
|
|
|
before returning or timing out if the condition is not met.
|
|
|
|
extends_documentation_fragment: eos
|
|
|
|
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 22:46:08 +02:00
|
|
|
is returned. If the I(wait_for) argument is provided, the
|
2016-01-25 13:55:24 +01:00
|
|
|
module is not returned until the condition is satisfied or
|
2016-08-02 22:46:08 +02:00
|
|
|
the number of I(retries) has been exceeded.
|
2016-01-25 13:55:24 +01:00
|
|
|
required: true
|
2016-06-06 02:34:31 +02:00
|
|
|
wait_for:
|
2016-01-25 13:55:24 +01: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
|
|
|
|
by the configured retries, the task fails. See examples.
|
|
|
|
required: false
|
|
|
|
default: null
|
2016-06-06 02:34:31 +02:00
|
|
|
aliases: ['waitfor']
|
|
|
|
version_added: "2.2"
|
2016-08-22 18:48:53 +02: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.
|
|
|
|
required: false
|
|
|
|
default: all
|
|
|
|
choices: ['any', 'all']
|
|
|
|
version_added: "2.2"
|
2016-01-25 13:55:24 +01:00
|
|
|
retries:
|
|
|
|
description:
|
2016-05-06 23:34:31 +02:00
|
|
|
- Specifies the number of retries a command should be tried
|
2016-01-25 13:55:24 +01:00
|
|
|
before it is considered failed. The command is run on the
|
2016-08-02 22:46:08 +02:00
|
|
|
target device every retry and evaluated against the I(wait_for)
|
|
|
|
conditionals.
|
2016-01-25 13:55:24 +01:00
|
|
|
required: false
|
|
|
|
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.
|
|
|
|
required: false
|
|
|
|
default: 1
|
|
|
|
"""
|
|
|
|
|
|
|
|
EXAMPLES = """
|
2016-08-22 18:48:53 +02:00
|
|
|
# Note: examples below use the following provider dict to handle
|
|
|
|
# transport and authentication to the node.
|
|
|
|
vars:
|
|
|
|
cli:
|
|
|
|
host: "{{ inventory_hostname }}"
|
|
|
|
username: admin
|
|
|
|
password: admin
|
|
|
|
transport: cli
|
|
|
|
|
|
|
|
- name: run show verion on remote devices
|
|
|
|
eos_command:
|
|
|
|
commands: show version
|
2016-09-10 15:46:30 +02:00
|
|
|
provider: "{{ cli }}"
|
2016-08-22 18:48:53 +02:00
|
|
|
|
|
|
|
- name: run show version and check to see if output contains Arista
|
|
|
|
eos_command:
|
|
|
|
commands: show version
|
|
|
|
wait_for: result[0] contains Arista
|
|
|
|
provider: "{{ cli }}"
|
|
|
|
|
|
|
|
- name: run multiple commands on remote nodes
|
|
|
|
eos_command:
|
2016-01-25 13:55:24 +01:00
|
|
|
commands:
|
2016-08-22 18:48:53 +02:00
|
|
|
- show version
|
|
|
|
- show interfaces
|
|
|
|
provider: "{{ cli }}"
|
2016-01-25 13:55:24 +01:00
|
|
|
|
2016-08-22 18:48:53 +02:00
|
|
|
- name: run multiple commands and evalute the output
|
|
|
|
eos_command:
|
2016-01-25 13:55:24 +01:00
|
|
|
commands:
|
|
|
|
- show version
|
2016-08-22 18:48:53 +02:00
|
|
|
- show interfaces
|
|
|
|
wait_for:
|
|
|
|
- result[0] contains Arista
|
|
|
|
- result[1] contains Loopback0
|
|
|
|
provider: "{{ cli }}"
|
|
|
|
|
|
|
|
- name: run commands and specify the output format
|
|
|
|
eos_command:
|
|
|
|
commands:
|
|
|
|
- command: show version
|
|
|
|
output: json
|
|
|
|
provider: "{{ cli }}"
|
2016-01-25 13:55:24 +01:00
|
|
|
"""
|
|
|
|
|
|
|
|
RETURN = """
|
2016-02-09 20:35:18 +01:00
|
|
|
stdout:
|
2016-01-25 13:55:24 +01:00
|
|
|
description: the set of responses from the commands
|
|
|
|
returned: always
|
|
|
|
type: list
|
|
|
|
sample: ['...', '...']
|
|
|
|
|
2016-02-09 20:35:18 +01:00
|
|
|
stdout_lines:
|
2016-05-06 23:34:31 +02:00
|
|
|
description: the value of stdout split into a list
|
2016-02-09 20:35:18 +01:00
|
|
|
returned: always
|
|
|
|
type: list
|
|
|
|
sample: [['...', '...'], ['...'], ['...']]
|
|
|
|
|
|
|
|
failed_conditions:
|
2016-01-25 13:55:24 +01:00
|
|
|
description: the conditionals that failed
|
|
|
|
retured: failed
|
|
|
|
type: list
|
|
|
|
sample: ['...', '...']
|
|
|
|
"""
|
2016-06-06 02:34:31 +02:00
|
|
|
from ansible.module_utils.basic import get_exception
|
2016-08-22 18:48:53 +02:00
|
|
|
from ansible.module_utils.netcli import CommandRunner
|
|
|
|
from ansible.module_utils.netcli import AddCommandError, FailedConditionsError
|
|
|
|
from ansible.module_utils.eos import NetworkModule, NetworkError
|
|
|
|
|
|
|
|
VALID_KEYS = ['command', 'output', 'prompt', 'response']
|
2016-01-25 13:55:24 +01:00
|
|
|
|
2016-06-06 02:34:31 +02:00
|
|
|
def to_lines(stdout):
|
2016-02-09 20:35:18 +01:00
|
|
|
for item in stdout:
|
|
|
|
if isinstance(item, basestring):
|
|
|
|
item = str(item).split('\n')
|
|
|
|
yield item
|
2016-01-25 13:55:24 +01:00
|
|
|
|
2016-08-22 18:48:53 +02:00
|
|
|
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', 'json']:
|
|
|
|
module.fail_json(msg='invalid output specified for command')
|
|
|
|
elif not set(cmd.keys()).issubset(VALID_KEYS):
|
|
|
|
module.fail_json(msg='unknown command keyword specified. Valid '
|
|
|
|
'values are %s' % ', '.join(VALID_KEYS))
|
|
|
|
yield cmd
|
|
|
|
|
2016-01-25 13:55:24 +01:00
|
|
|
def main():
|
|
|
|
spec = dict(
|
2016-08-22 18:48:53 +02:00
|
|
|
# { command: <str>, output: <str>, prompt: <str>, response: <str> }
|
2016-06-06 02:34:31 +02:00
|
|
|
commands=dict(type='list', required=True),
|
2016-08-22 18:48:53 +02:00
|
|
|
|
2016-06-06 02:34:31 +02:00
|
|
|
wait_for=dict(type='list', aliases=['waitfor']),
|
2016-08-22 18:48:53 +02:00
|
|
|
match=dict(default='all', choices=['all', 'any']),
|
|
|
|
|
2016-01-25 13:55:24 +01:00
|
|
|
retries=dict(default=10, type='int'),
|
|
|
|
interval=dict(default=1, type='int')
|
|
|
|
)
|
|
|
|
|
2016-08-22 18:48:53 +02:00
|
|
|
module = NetworkModule(argument_spec=spec,
|
|
|
|
connect_on_load=False,
|
|
|
|
supports_check_mode=True)
|
2016-01-25 13:55:24 +01:00
|
|
|
|
2016-08-22 18:48:53 +02:00
|
|
|
commands = list(parse_commands(module))
|
2016-06-06 02:34:31 +02:00
|
|
|
conditionals = module.params['wait_for'] or list()
|
|
|
|
|
|
|
|
warnings = list()
|
2016-01-25 13:55:24 +01:00
|
|
|
|
2016-06-06 02:34:31 +02:00
|
|
|
runner = CommandRunner(module)
|
|
|
|
|
|
|
|
for cmd in commands:
|
2016-08-22 18:48:53 +02:00
|
|
|
if module.check_mode and not cmd['command'].startswith('show'):
|
2016-06-06 02:34:31 +02:00
|
|
|
warnings.append('only show commands are supported when using '
|
2016-08-22 18:48:53 +02:00
|
|
|
'check mode, not executing `%s`' % cmd['command'])
|
2016-06-06 02:34:31 +02:00
|
|
|
else:
|
2016-08-22 18:48:53 +02:00
|
|
|
if cmd['command'].startswith('conf'):
|
|
|
|
module.fail_json(msg='eos_command does not support running '
|
|
|
|
'config mode commands. Please use '
|
|
|
|
'eos_config instead')
|
|
|
|
try:
|
|
|
|
runner.add_command(**cmd)
|
|
|
|
except AddCommandError:
|
|
|
|
exc = get_exception()
|
|
|
|
warnings.append('duplicate command detected: %s' % cmd)
|
2016-06-06 02:34:31 +02:00
|
|
|
|
|
|
|
for item in conditionals:
|
|
|
|
runner.add_conditional(item)
|
|
|
|
|
|
|
|
runner.retries = module.params['retries']
|
|
|
|
runner.interval = module.params['interval']
|
2016-08-22 18:48:53 +02:00
|
|
|
runner.match = module.params['match']
|
2016-01-25 13:55:24 +01:00
|
|
|
|
|
|
|
try:
|
2016-06-06 02:34:31 +02:00
|
|
|
runner.run()
|
|
|
|
except FailedConditionsError:
|
2016-05-18 18:08:30 +02:00
|
|
|
exc = get_exception()
|
2016-06-06 02:34:31 +02:00
|
|
|
module.fail_json(msg=str(exc), failed_conditions=exc.failed_conditions)
|
|
|
|
except NetworkError:
|
|
|
|
exc = get_exception()
|
|
|
|
module.fail_json(msg=str(exc))
|
2016-01-25 13:55:24 +01:00
|
|
|
|
2016-08-22 18:48:53 +02:00
|
|
|
result = dict(changed=False, stdout=list())
|
2016-01-25 13:55:24 +01:00
|
|
|
|
2016-06-06 02:34:31 +02:00
|
|
|
for cmd in commands:
|
|
|
|
try:
|
2016-08-22 18:48:53 +02:00
|
|
|
output = runner.get_command(cmd['command'], cmd.get('output'))
|
2016-06-06 02:34:31 +02:00
|
|
|
except ValueError:
|
|
|
|
output = 'command not executed due to check_mode, see warnings'
|
|
|
|
result['stdout'].append(output)
|
2016-01-25 13:55:24 +01:00
|
|
|
|
2016-06-06 02:34:31 +02:00
|
|
|
result['warnings'] = warnings
|
|
|
|
result['stdout_lines'] = list(to_lines(result['stdout']))
|
2016-01-25 13:55:24 +01:00
|
|
|
|
2016-06-06 02:34:31 +02:00
|
|
|
module.exit_json(**result)
|
2016-01-25 13:55:24 +01:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2016-06-06 02:34:31 +02:00
|
|
|
main()
|
2016-01-25 13:55:24 +01:00
|
|
|
|