2014-11-14 23:14:08 +01:00
|
|
|
# (c) 2012, Michael DeHaan <michael.dehaan@gmail.com>
|
|
|
|
#
|
|
|
|
# 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/>.
|
|
|
|
|
2015-10-20 03:36:19 +02:00
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
__metaclass__ = type
|
|
|
|
|
2014-11-14 23:14:08 +01:00
|
|
|
########################################################
|
2015-10-23 22:33:16 +02:00
|
|
|
|
|
|
|
import os
|
|
|
|
|
2014-11-14 23:14:08 +01:00
|
|
|
from ansible import constants as C
|
2015-07-21 06:18:12 +02:00
|
|
|
from ansible.cli import CLI
|
2015-09-28 15:02:24 +02:00
|
|
|
from ansible.errors import AnsibleError, AnsibleOptionsError
|
2014-11-14 23:14:08 +01:00
|
|
|
from ansible.executor.task_queue_manager import TaskQueueManager
|
2016-09-07 07:54:17 +02:00
|
|
|
from ansible.module_utils._text import to_text
|
2014-11-14 23:14:08 +01:00
|
|
|
from ansible.parsing.splitter import parse_kv
|
2017-10-29 05:47:27 +01:00
|
|
|
from ansible.playbook import Playbook
|
2014-11-14 23:14:08 +01:00
|
|
|
from ansible.playbook.play import Play
|
2017-08-15 22:38:59 +02:00
|
|
|
from ansible.plugins.loader import get_all_plugin_loaders
|
2014-11-14 23:14:08 +01:00
|
|
|
|
2015-11-10 20:40:55 +01:00
|
|
|
try:
|
|
|
|
from __main__ import display
|
|
|
|
except ImportError:
|
|
|
|
from ansible.utils.display import Display
|
|
|
|
display = Display()
|
|
|
|
|
|
|
|
|
2014-11-14 23:14:08 +01:00
|
|
|
########################################################
|
|
|
|
|
2015-05-01 00:43:53 +02:00
|
|
|
class AdHocCLI(CLI):
|
2017-03-23 06:11:40 +01:00
|
|
|
''' is an extra-simple tool/framework/API for doing 'remote things'.
|
|
|
|
this command allows you to define and run a single task 'playbook' against a set of hosts
|
|
|
|
'''
|
2014-11-14 23:14:08 +01:00
|
|
|
|
|
|
|
def parse(self):
|
|
|
|
''' create an options parser for bin/ansible '''
|
|
|
|
|
2015-04-27 13:31:41 +02:00
|
|
|
self.parser = CLI.base_parser(
|
2014-11-14 23:14:08 +01:00
|
|
|
usage='%prog <host-pattern> [options]',
|
2015-03-21 05:35:56 +01:00
|
|
|
runas_opts=True,
|
2015-08-18 09:17:58 +02:00
|
|
|
inventory_opts=True,
|
2014-11-14 23:14:08 +01:00
|
|
|
async_opts=True,
|
2015-03-21 05:35:56 +01:00
|
|
|
output_opts=True,
|
|
|
|
connect_opts=True,
|
2014-11-14 23:14:08 +01:00
|
|
|
check_opts=True,
|
2015-05-01 03:22:23 +02:00
|
|
|
runtask_opts=True,
|
|
|
|
vault_opts=True,
|
2015-06-09 23:35:19 +02:00
|
|
|
fork_opts=True,
|
2015-08-26 00:10:03 +02:00
|
|
|
module_opts=True,
|
2017-10-31 20:41:30 +01:00
|
|
|
basedir_opts=True,
|
2017-03-22 21:38:49 +01:00
|
|
|
desc="Define and run a single task 'playbook' against a set of hosts",
|
|
|
|
epilog="Some modules do not make sense in Ad-Hoc (include, meta, etc)",
|
2014-11-14 23:14:08 +01:00
|
|
|
)
|
|
|
|
|
2015-03-21 05:35:56 +01:00
|
|
|
# options unique to ansible ad-hoc
|
2015-04-27 13:31:41 +02:00
|
|
|
self.parser.add_option('-a', '--args', dest='module_args',
|
2017-06-02 13:14:11 +02:00
|
|
|
help="module arguments", default=C.DEFAULT_MODULE_ARGS)
|
2015-04-27 13:31:41 +02:00
|
|
|
self.parser.add_option('-m', '--module-name', dest='module_name',
|
2017-06-02 13:14:11 +02:00
|
|
|
help="module name to execute (default=%s)" % C.DEFAULT_MODULE_NAME,
|
|
|
|
default=C.DEFAULT_MODULE_NAME)
|
2014-11-14 23:14:08 +01:00
|
|
|
|
2016-09-29 23:14:02 +02:00
|
|
|
super(AdHocCLI, self).parse()
|
2014-11-14 23:14:08 +01:00
|
|
|
|
2016-06-03 15:53:41 +02:00
|
|
|
if len(self.args) < 1:
|
2015-05-01 00:43:53 +02:00
|
|
|
raise AnsibleOptionsError("Missing target hosts")
|
2016-06-03 15:56:56 +02:00
|
|
|
elif len(self.args) > 1:
|
2017-01-11 13:18:06 +01:00
|
|
|
raise AnsibleOptionsError("Extraneous options or arguments")
|
2016-06-03 15:53:41 +02:00
|
|
|
|
2015-11-10 20:40:55 +01:00
|
|
|
display.verbosity = self.options.verbosity
|
2015-08-02 10:40:42 +02:00
|
|
|
self.validate_conflicts(runas_opts=True, vault_opts=True, fork_opts=True)
|
2014-11-14 23:14:08 +01:00
|
|
|
|
2015-08-17 02:05:10 +02:00
|
|
|
def _play_ds(self, pattern, async, poll):
|
2016-09-15 20:25:56 +02:00
|
|
|
check_raw = self.options.module_name in ('command', 'win_command', 'shell', 'win_shell', 'script', 'raw')
|
2015-06-02 21:16:39 +02:00
|
|
|
return dict(
|
2017-06-02 13:14:11 +02:00
|
|
|
name="Ansible Ad-Hoc",
|
|
|
|
hosts=pattern,
|
|
|
|
gather_facts='no',
|
|
|
|
tasks=[dict(action=dict(module=self.options.module_name, args=parse_kv(self.options.module_args, check_raw=check_raw)), async=async, poll=poll)]
|
2015-06-02 21:16:39 +02:00
|
|
|
)
|
2014-11-14 23:14:08 +01:00
|
|
|
|
2015-04-27 13:31:41 +02:00
|
|
|
def run(self):
|
2017-03-22 21:38:49 +01:00
|
|
|
''' create and execute the single task playbook '''
|
2014-11-14 23:14:08 +01:00
|
|
|
|
2015-07-04 16:23:30 +02:00
|
|
|
super(AdHocCLI, self).run()
|
|
|
|
|
2015-04-27 13:31:41 +02:00
|
|
|
# only thing left should be host pattern
|
2016-09-07 07:54:17 +02:00
|
|
|
pattern = to_text(self.args[0], errors='surrogate_or_strict')
|
2014-11-14 23:14:08 +01:00
|
|
|
|
2017-06-02 13:14:11 +02:00
|
|
|
sshpass = None
|
2015-08-26 00:10:03 +02:00
|
|
|
becomepass = None
|
2015-03-21 06:19:07 +01:00
|
|
|
|
2015-04-27 13:31:41 +02:00
|
|
|
self.normalize_become_options()
|
|
|
|
(sshpass, becomepass) = self.ask_passwords()
|
2017-06-02 13:14:11 +02:00
|
|
|
passwords = {'conn_pass': sshpass, 'become_pass': becomepass}
|
2015-03-21 06:19:07 +01:00
|
|
|
|
2017-08-15 22:38:59 +02:00
|
|
|
# dynamically load any plugins
|
|
|
|
get_all_plugin_loaders()
|
|
|
|
|
2017-05-23 23:16:49 +02:00
|
|
|
loader, inventory, variable_manager = self._play_prereqs(self.options)
|
2014-11-14 23:14:08 +01:00
|
|
|
|
2016-02-25 18:36:44 +01:00
|
|
|
no_hosts = False
|
2016-05-31 18:15:19 +02:00
|
|
|
if len(inventory.list_hosts()) == 0:
|
2016-02-25 18:36:44 +01:00
|
|
|
# Empty inventory
|
|
|
|
display.warning("provided hosts list is empty, only localhost is available")
|
|
|
|
no_hosts = True
|
2015-09-23 14:28:38 +02:00
|
|
|
|
2016-02-25 18:36:44 +01:00
|
|
|
inventory.subset(self.options.subset)
|
2016-01-25 23:37:39 +01:00
|
|
|
hosts = inventory.list_hosts(pattern)
|
2016-09-28 17:21:07 +02:00
|
|
|
if len(hosts) == 0:
|
|
|
|
if no_hosts is False and self.options.subset:
|
|
|
|
# Invalid limit
|
|
|
|
raise AnsibleError("Specified --limit does not match any hosts")
|
|
|
|
else:
|
|
|
|
display.warning("No hosts matched, nothing to do")
|
2014-11-14 23:14:08 +01:00
|
|
|
|
2015-04-27 13:31:41 +02:00
|
|
|
if self.options.listhosts:
|
2015-11-10 20:40:55 +01:00
|
|
|
display.display(' hosts (%d):' % len(hosts))
|
2014-11-14 23:14:08 +01:00
|
|
|
for host in hosts:
|
2015-11-10 20:40:55 +01:00
|
|
|
display.display(' %s' % host)
|
2015-04-27 13:31:41 +02:00
|
|
|
return 0
|
2014-11-14 23:14:08 +01:00
|
|
|
|
2015-04-27 13:31:41 +02:00
|
|
|
if self.options.module_name in C.MODULE_REQUIRE_ARGS and not self.options.module_args:
|
2015-07-27 09:13:21 +02:00
|
|
|
err = "No argument passed to %s module" % self.options.module_name
|
|
|
|
if pattern.endswith(".yml"):
|
|
|
|
err = err + ' (did you mean to run ansible-playbook?)'
|
|
|
|
raise AnsibleOptionsError(err)
|
2014-11-14 23:14:08 +01:00
|
|
|
|
2016-09-06 02:07:58 +02:00
|
|
|
# Avoid modules that don't work with ad-hoc
|
2017-10-09 21:52:52 +02:00
|
|
|
if self.options.module_name.startswith(('include', 'import_')):
|
2016-09-06 02:07:58 +02:00
|
|
|
raise AnsibleOptionsError("'%s' is not a valid action for ad-hoc commands" % self.options.module_name)
|
|
|
|
|
2015-08-17 02:05:10 +02:00
|
|
|
play_ds = self._play_ds(pattern, self.options.seconds, self.options.poll_interval)
|
2014-11-14 23:14:08 +01:00
|
|
|
play = Play().load(play_ds, variable_manager=variable_manager, loader=loader)
|
|
|
|
|
2017-10-29 05:47:27 +01:00
|
|
|
# used in start callback
|
|
|
|
playbook = Playbook(loader)
|
|
|
|
playbook._entries.append(play)
|
|
|
|
playbook._file_name = '__adhoc_playbook__'
|
|
|
|
|
2016-09-07 07:54:17 +02:00
|
|
|
if self.callback:
|
2015-12-10 14:04:06 +01:00
|
|
|
cb = self.callback
|
|
|
|
elif self.options.one_line:
|
2015-07-13 21:42:47 +02:00
|
|
|
cb = 'oneline'
|
2017-06-26 22:01:06 +02:00
|
|
|
# Respect custom 'stdout_callback' only with enabled 'bin_ansible_callbacks'
|
|
|
|
elif C.DEFAULT_LOAD_CALLBACK_PLUGINS and C.DEFAULT_STDOUT_CALLBACK != 'default':
|
|
|
|
cb = C.DEFAULT_STDOUT_CALLBACK
|
2015-07-13 21:42:47 +02:00
|
|
|
else:
|
|
|
|
cb = 'minimal'
|
|
|
|
|
2017-06-02 13:14:11 +02:00
|
|
|
run_tree = False
|
2015-08-12 01:18:10 +02:00
|
|
|
if self.options.tree:
|
|
|
|
C.DEFAULT_CALLBACK_WHITELIST.append('tree')
|
|
|
|
C.TREE_DIR = self.options.tree
|
2017-06-02 13:14:11 +02:00
|
|
|
run_tree = True
|
2015-08-12 01:18:10 +02:00
|
|
|
|
2014-11-14 23:14:08 +01:00
|
|
|
# now create a task queue manager to execute the play
|
2015-06-15 20:04:46 +02:00
|
|
|
self._tqm = None
|
2014-11-14 23:14:08 +01:00
|
|
|
try:
|
2015-06-15 20:04:46 +02:00
|
|
|
self._tqm = TaskQueueManager(
|
2017-01-29 08:28:53 +01:00
|
|
|
inventory=inventory,
|
|
|
|
variable_manager=variable_manager,
|
|
|
|
loader=loader,
|
|
|
|
options=self.options,
|
|
|
|
passwords=passwords,
|
|
|
|
stdout_callback=cb,
|
|
|
|
run_additional_callbacks=C.DEFAULT_LOAD_CALLBACK_PLUGINS,
|
|
|
|
run_tree=run_tree,
|
|
|
|
)
|
2016-02-06 06:53:01 +01:00
|
|
|
|
2017-10-29 05:47:27 +01:00
|
|
|
self._tqm.send_callback('v2_playbook_on_start', playbook)
|
|
|
|
|
2015-06-15 20:04:46 +02:00
|
|
|
result = self._tqm.run(play)
|
2017-10-29 05:47:27 +01:00
|
|
|
|
|
|
|
self._tqm.send_callback('v2_playbook_on_stats', self._tqm._stats)
|
2015-04-27 13:31:41 +02:00
|
|
|
finally:
|
2015-06-15 20:04:46 +02:00
|
|
|
if self._tqm:
|
|
|
|
self._tqm.cleanup()
|
2016-04-14 16:31:39 +02:00
|
|
|
if loader:
|
|
|
|
loader.cleanup_all_tmp_files()
|
2014-11-14 23:14:08 +01:00
|
|
|
|
2015-01-12 23:04:56 +01:00
|
|
|
return result
|