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/>.
|
|
|
|
|
|
|
|
########################################################
|
|
|
|
from ansible import constants as C
|
2015-07-21 06:18:12 +02:00
|
|
|
from ansible.cli import CLI
|
2015-06-28 19:34:29 +02:00
|
|
|
from ansible.errors import AnsibleOptionsError
|
2014-11-14 23:14:08 +01:00
|
|
|
from ansible.executor.task_queue_manager import TaskQueueManager
|
|
|
|
from ansible.inventory import Inventory
|
|
|
|
from ansible.parsing import DataLoader
|
|
|
|
from ansible.parsing.splitter import parse_kv
|
|
|
|
from ansible.playbook.play import Play
|
2015-07-21 06:18:12 +02:00
|
|
|
from ansible.utils.vars import load_extra_vars
|
2014-11-14 23:14:08 +01:00
|
|
|
from ansible.vars import VariableManager
|
|
|
|
|
|
|
|
########################################################
|
|
|
|
|
2015-05-01 00:43:53 +02:00
|
|
|
class AdHocCLI(CLI):
|
2015-04-27 13:31:41 +02:00
|
|
|
''' code behind ansible ad-hoc cli'''
|
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,
|
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',
|
2014-11-14 23:14:08 +01: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',
|
2014-11-14 23:14:08 +01:00
|
|
|
help="module name to execute (default=%s)" % C.DEFAULT_MODULE_NAME,
|
|
|
|
default=C.DEFAULT_MODULE_NAME)
|
|
|
|
|
2015-04-27 13:31:41 +02:00
|
|
|
self.options, self.args = self.parser.parse_args()
|
2014-11-14 23:14:08 +01:00
|
|
|
|
2015-04-27 13:31:41 +02:00
|
|
|
if len(self.args) != 1:
|
2015-05-01 00:43:53 +02:00
|
|
|
raise AnsibleOptionsError("Missing target hosts")
|
2014-11-14 23:14:08 +01:00
|
|
|
|
2015-04-27 13:31:41 +02:00
|
|
|
self.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-04-27 13:31:41 +02:00
|
|
|
return True
|
2014-11-14 23:14:08 +01:00
|
|
|
|
2015-08-17 02:05:10 +02:00
|
|
|
def _play_ds(self, pattern, async, poll):
|
2015-06-02 21:16:39 +02:00
|
|
|
return dict(
|
|
|
|
name = "Ansible Ad-Hoc",
|
|
|
|
hosts = pattern,
|
|
|
|
gather_facts = 'no',
|
2015-08-17 02:05:10 +02:00
|
|
|
tasks = [ dict(action=dict(module=self.options.module_name, args=parse_kv(self.options.module_args)), 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):
|
2014-11-14 23:14:08 +01:00
|
|
|
''' use Runner lib to do SSH things '''
|
|
|
|
|
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
|
|
|
|
pattern = self.args[0]
|
2014-11-14 23:14:08 +01:00
|
|
|
|
2015-04-27 13:31:41 +02:00
|
|
|
# ignore connection password cause we are local
|
|
|
|
if self.options.connection == "local":
|
|
|
|
self.options.ask_pass = False
|
2015-03-21 06:23:28 +01:00
|
|
|
|
2015-03-21 06:19:07 +01:00
|
|
|
sshpass = None
|
2015-08-26 00:10:03 +02:00
|
|
|
becomepass = None
|
2015-03-21 06:19:07 +01:00
|
|
|
vault_pass = None
|
|
|
|
|
2015-04-27 13:31:41 +02:00
|
|
|
self.normalize_become_options()
|
|
|
|
(sshpass, becomepass) = self.ask_passwords()
|
2015-04-07 04:31:55 +02:00
|
|
|
passwords = { 'conn_pass': sshpass, 'become_pass': becomepass }
|
2015-03-21 06:19:07 +01:00
|
|
|
|
2015-04-27 13:31:41 +02:00
|
|
|
if self.options.vault_password_file:
|
|
|
|
# read vault_pass from a file
|
2015-07-11 20:24:45 +02:00
|
|
|
vault_pass = CLI.read_vault_password_file(self.options.vault_password_file)
|
2015-04-27 13:31:41 +02:00
|
|
|
elif self.options.ask_vault_pass:
|
|
|
|
vault_pass = self.ask_vault_passwords(ask_vault_pass=True, ask_new_vault_pass=False, confirm_new=False)[0]
|
2015-03-21 06:23:28 +01:00
|
|
|
|
2015-03-21 06:33:10 +01:00
|
|
|
loader = DataLoader(vault_password=vault_pass)
|
2014-11-14 23:14:08 +01:00
|
|
|
variable_manager = VariableManager()
|
2015-07-21 06:18:12 +02:00
|
|
|
variable_manager.extra_vars = load_extra_vars(loader=loader, options=self.options)
|
2014-11-14 23:14:08 +01:00
|
|
|
|
2015-04-27 13:31:41 +02:00
|
|
|
inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list=self.options.inventory)
|
2015-05-07 19:53:22 +02:00
|
|
|
variable_manager.set_inventory(inventory)
|
2014-11-14 23:14:08 +01:00
|
|
|
|
|
|
|
hosts = inventory.list_hosts(pattern)
|
|
|
|
if len(hosts) == 0:
|
2015-04-27 13:31:41 +02:00
|
|
|
self.display.warning("provided hosts list is empty, only localhost is available")
|
2014-11-14 23:14:08 +01:00
|
|
|
|
2015-04-27 13:31:41 +02:00
|
|
|
if self.options.listhosts:
|
2015-07-29 20:56:22 +02:00
|
|
|
self.display.display(' hosts (%d):' % len(hosts))
|
2014-11-14 23:14:08 +01:00
|
|
|
for host in hosts:
|
2015-05-01 03:22:23 +02:00
|
|
|
self.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
|
|
|
|
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)
|
|
|
|
|
2015-07-13 21:42:47 +02:00
|
|
|
if self.options.one_line:
|
|
|
|
cb = 'oneline'
|
|
|
|
else:
|
|
|
|
cb = 'minimal'
|
|
|
|
|
2015-08-12 01:18:10 +02:00
|
|
|
if self.options.tree:
|
|
|
|
C.DEFAULT_CALLBACK_WHITELIST.append('tree')
|
|
|
|
C.TREE_DIR = self.options.tree
|
|
|
|
|
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(
|
2015-04-27 13:31:41 +02:00
|
|
|
inventory=inventory,
|
|
|
|
variable_manager=variable_manager,
|
|
|
|
loader=loader,
|
|
|
|
display=self.display,
|
|
|
|
options=self.options,
|
|
|
|
passwords=passwords,
|
2015-07-13 21:42:47 +02:00
|
|
|
stdout_callback=cb,
|
2015-04-27 13:31:41 +02:00
|
|
|
)
|
2015-06-15 20:04:46 +02:00
|
|
|
result = self._tqm.run(play)
|
2015-04-27 13:31:41 +02:00
|
|
|
finally:
|
2015-06-15 20:04:46 +02:00
|
|
|
if self._tqm:
|
|
|
|
self._tqm.cleanup()
|
2014-11-14 23:14:08 +01:00
|
|
|
|
2015-01-12 23:04:56 +01:00
|
|
|
return result
|