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-05-01 03:22:23 +02:00
|
|
|
from ansible.errors import AnsibleError, 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-05-01 03:22:23 +02:00
|
|
|
from ansible.cli import CLI
|
2015-04-03 19:17:01 +02:00
|
|
|
from ansible.utils.display import Display
|
2015-03-21 06:23:28 +01:00
|
|
|
from ansible.utils.vault import read_vault_file
|
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,
|
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,
|
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-06-09 23:24:06 +02:00
|
|
|
self.validate_conflicts(runas_opts=True, vault_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-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-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
|
|
|
|
becomepass = None
|
|
|
|
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
|
|
|
|
vault_pass = read_vault_file(self.options.vault_password_file)
|
|
|
|
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-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:
|
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-05-13 17:15:04 +02:00
|
|
|
raise AnsibleOptionsError("No argument passed to %s module" % self.options.module_name)
|
2014-11-14 23:14:08 +01:00
|
|
|
|
2015-04-27 13:31:41 +02:00
|
|
|
#TODO: implement async support
|
|
|
|
#if self.options.seconds:
|
2014-11-14 23:14:08 +01:00
|
|
|
# callbacks.display("background launch...\n\n", color='cyan')
|
2015-04-27 13:31:41 +02:00
|
|
|
# results, poller = runner.run_async(self.options.seconds)
|
|
|
|
# results = self.poll_while_needed(poller)
|
2014-11-14 23:14:08 +01:00
|
|
|
#else:
|
|
|
|
# results = runner.run()
|
|
|
|
|
|
|
|
# create a pseudo-play to execute the specified module via a single task
|
|
|
|
play_ds = dict(
|
2015-04-27 13:31:41 +02:00
|
|
|
name = "Ansible Ad-Hoc",
|
2014-11-14 23:14:08 +01:00
|
|
|
hosts = pattern,
|
|
|
|
gather_facts = 'no',
|
2015-04-27 13:31:41 +02:00
|
|
|
tasks = [ dict(action=dict(module=self.options.module_name, args=parse_kv(self.options.module_args))), ]
|
2014-11-14 23:14:08 +01:00
|
|
|
)
|
2015-01-12 23:04:56 +01:00
|
|
|
|
2014-11-14 23:14:08 +01:00
|
|
|
play = Play().load(play_ds, variable_manager=variable_manager, loader=loader)
|
|
|
|
|
|
|
|
# now create a task queue manager to execute the play
|
2015-05-01 00:43:53 +02:00
|
|
|
tqm = None
|
2014-11-14 23:14:08 +01:00
|
|
|
try:
|
2015-04-27 13:31:41 +02:00
|
|
|
tqm = TaskQueueManager(
|
|
|
|
inventory=inventory,
|
|
|
|
variable_manager=variable_manager,
|
|
|
|
loader=loader,
|
|
|
|
display=self.display,
|
|
|
|
options=self.options,
|
|
|
|
passwords=passwords,
|
|
|
|
stdout_callback='minimal',
|
|
|
|
)
|
2014-11-14 23:14:08 +01:00
|
|
|
result = tqm.run(play)
|
2015-04-27 13:31:41 +02:00
|
|
|
finally:
|
|
|
|
if tqm:
|
|
|
|
tqm.cleanup()
|
2014-11-14 23:14:08 +01:00
|
|
|
|
2015-01-12 23:04:56 +01:00
|
|
|
return result
|
2014-11-14 23:14:08 +01:00
|
|
|
|
|
|
|
# ----------------------------------------------
|
|
|
|
|
2015-04-27 13:31:41 +02:00
|
|
|
def poll_while_needed(self, poller):
|
2014-11-14 23:14:08 +01:00
|
|
|
''' summarize results from Runner '''
|
|
|
|
|
|
|
|
# BACKGROUND POLL LOGIC when -B and -P are specified
|
2015-04-27 13:31:41 +02:00
|
|
|
if self.options.seconds and self.options.poll_interval > 0:
|
|
|
|
poller.wait(self.options.seconds, self.options.poll_interval)
|
2014-11-14 23:14:08 +01:00
|
|
|
|
|
|
|
return poller.results
|
|
|
|
|