diff options
Diffstat (limited to 'lib/ansible/cli/adhoc.py')
-rw-r--r-- | lib/ansible/cli/adhoc.py | 79 |
1 files changed, 37 insertions, 42 deletions
diff --git a/lib/ansible/cli/adhoc.py b/lib/ansible/cli/adhoc.py index 9983e081ae..4c7f5c199d 100644 --- a/lib/ansible/cli/adhoc.py +++ b/lib/ansible/cli/adhoc.py @@ -1,24 +1,12 @@ -# (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/>. +# Copyright: (c) 2012, Michael DeHaan <michael.dehaan@gmail.com> +# Copyright: (c) 2018, Toshio Kuratomi <tkuratomi@ansible.com> +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) from __future__ import (absolute_import, division, print_function) __metaclass__ = type from ansible import constants as C +from ansible import context from ansible.cli import CLI from ansible.errors import AnsibleError, AnsibleOptionsError from ansible.executor.task_queue_manager import TaskQueueManager @@ -37,10 +25,9 @@ class AdHocCLI(CLI): this command allows you to define and run a single task 'playbook' against a set of hosts ''' - def parse(self): + def init_parser(self): ''' create an options parser for bin/ansible ''' - - self.parser = CLI.base_parser( + self.parser = super(AdHocCLI, self).init_parser( usage='%prog <host-pattern> [options]', runas_opts=True, inventory_opts=True, @@ -63,24 +50,32 @@ class AdHocCLI(CLI): self.parser.add_option('-m', '--module-name', dest='module_name', help="module name to execute (default=%s)" % C.DEFAULT_MODULE_NAME, default=C.DEFAULT_MODULE_NAME) + return self.parser + + def post_process_args(self, options, args): + '''Post process and validate options for bin/ansible ''' - super(AdHocCLI, self).parse() + options, args = super(AdHocCLI, self).post_process_args(options, args) - if len(self.args) < 1: + if len(args) < 1: raise AnsibleOptionsError("Missing target hosts") - elif len(self.args) > 1: + elif len(args) > 1: raise AnsibleOptionsError("Extraneous options or arguments") - display.verbosity = self.options.verbosity - self.validate_conflicts(runas_opts=True, vault_opts=True, fork_opts=True) + display.verbosity = options.verbosity + self.validate_conflicts(options, runas_opts=True, vault_opts=True, fork_opts=True) + + options = self.normalize_become_options(options) + + return options, args def _play_ds(self, pattern, async_val, poll): - check_raw = self.options.module_name in ('command', 'win_command', 'shell', 'win_shell', 'script', 'raw') + check_raw = context.CLIARGS['module_name'] in ('command', 'win_command', 'shell', 'win_shell', 'script', 'raw') - mytask = {'action': {'module': self.options.module_name, 'args': parse_kv(self.options.module_args, check_raw=check_raw)}} + mytask = {'action': {'module': context.CLIARGS['module_name'], 'args': parse_kv(context.CLIARGS['module_args'], check_raw=check_raw)}} # avoid adding to tasks that don't support it, unless set, then give user an error - if self.options.module_name not in ('include_role', 'include_tasks') or any(frozenset((async_val, poll))): + if context.CLIARGS['module_name'] not in ('include_role', 'include_tasks') or any(frozenset((async_val, poll))): mytask['async_val'] = async_val mytask['poll'] = poll @@ -96,46 +91,46 @@ class AdHocCLI(CLI): super(AdHocCLI, self).run() # only thing left should be host pattern - pattern = to_text(self.args[0], errors='surrogate_or_strict') + pattern = to_text(context.CLIARGS['args'][0], errors='surrogate_or_strict') sshpass = None becomepass = None - self.normalize_become_options() (sshpass, becomepass) = self.ask_passwords() passwords = {'conn_pass': sshpass, 'become_pass': becomepass} # dynamically load any plugins get_all_plugin_loaders() - loader, inventory, variable_manager = self._play_prereqs(self.options) + loader, inventory, variable_manager = self._play_prereqs() try: - hosts = CLI.get_host_list(inventory, self.options.subset, pattern) + hosts = self.get_host_list(inventory, context.CLIARGS['subset'], pattern) except AnsibleError: - if self.options.subset: + if context.CLIARGS['subset']: raise else: hosts = [] display.warning("No hosts matched, nothing to do") - if self.options.listhosts: + if context.CLIARGS['listhosts']: display.display(' hosts (%d):' % len(hosts)) for host in hosts: display.display(' %s' % host) return 0 - if self.options.module_name in C.MODULE_REQUIRE_ARGS and not self.options.module_args: - err = "No argument passed to %s module" % self.options.module_name + if context.CLIARGS['module_name'] in C.MODULE_REQUIRE_ARGS and not context.CLIARGS['module_args']: + err = "No argument passed to %s module" % context.CLIARGS['module_name'] if pattern.endswith(".yml"): err = err + ' (did you mean to run ansible-playbook?)' raise AnsibleOptionsError(err) # Avoid modules that don't work with ad-hoc - if self.options.module_name in ('import_playbook',): - raise AnsibleOptionsError("'%s' is not a valid action for ad-hoc commands" % self.options.module_name) + if context.CLIARGS['module_name'] in ('import_playbook',): + raise AnsibleOptionsError("'%s' is not a valid action for ad-hoc commands" + % context.CLIARGS['module_name']) - play_ds = self._play_ds(pattern, self.options.seconds, self.options.poll_interval) + play_ds = self._play_ds(pattern, context.CLIARGS['seconds'], context.CLIARGS['poll_interval']) play = Play().load(play_ds, variable_manager=variable_manager, loader=loader) # used in start callback @@ -145,7 +140,7 @@ class AdHocCLI(CLI): if self.callback: cb = self.callback - elif self.options.one_line: + elif context.CLIARGS['one_line']: cb = 'oneline' # Respect custom 'stdout_callback' only with enabled 'bin_ansible_callbacks' elif C.DEFAULT_LOAD_CALLBACK_PLUGINS and C.DEFAULT_STDOUT_CALLBACK != 'default': @@ -154,9 +149,9 @@ class AdHocCLI(CLI): cb = 'minimal' run_tree = False - if self.options.tree: + if context.CLIARGS['tree']: C.DEFAULT_CALLBACK_WHITELIST.append('tree') - C.TREE_DIR = self.options.tree + C.TREE_DIR = context.CLIARGS['tree'] run_tree = True # now create a task queue manager to execute the play @@ -166,11 +161,11 @@ class AdHocCLI(CLI): 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, + forks=context.CLIARGS['forks'], ) self._tqm.send_callback('v2_playbook_on_start', playbook) |