mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-05-08 06:12:51 +00:00
Save the command line arguments into a global context
* Once cli args are parsed, they're constant. So, save the parsed args
into the global context for everyone else to use them from now on.
* Port cli scripts to use the CLIARGS in the context
* Refactor call to parse cli args into the run() method
* Fix unittests for changes to the internals of CLI arg parsing
* Port callback plugins to use context.CLIARGS
* Got rid of the private self._options attribute
* Use context.CLIARGS in the individual callback plugins instead.
* Also output positional arguments in default and unixy plugins
* Code has been simplified since we're now dealing with a dict rather
than Optparse.Value
This commit is contained in:
@@ -1,18 +1,6 @@
|
||||
# (c) 2017, Brian Coca <bcoca@ansible.com>
|
||||
#
|
||||
# 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) 2017, Brian Coca <bcoca@ansible.com>
|
||||
# Copyright: (c) 2018, Ansible Project
|
||||
# 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
|
||||
@@ -21,6 +9,7 @@ import optparse
|
||||
from operator import attrgetter
|
||||
|
||||
from ansible import constants as C
|
||||
from ansible import context
|
||||
from ansible.cli import CLI
|
||||
from ansible.errors import AnsibleError, AnsibleOptionsError
|
||||
from ansible.inventory.host import Host
|
||||
@@ -66,9 +55,9 @@ class InventoryCLI(CLI):
|
||||
|
||||
self._new_api = True
|
||||
|
||||
def parse(self):
|
||||
def init_parser(self):
|
||||
|
||||
self.parser = CLI.base_parser(
|
||||
self.parser = super(InventoryCLI, self).init_parser(
|
||||
usage='usage: %prog [options] [host|group]',
|
||||
epilog='Show Ansible inventory information, by default it uses the inventory script JSON format',
|
||||
inventory_opts=True,
|
||||
@@ -103,15 +92,15 @@ class InventoryCLI(CLI):
|
||||
# self.parser.add_option("--ignore-vars-plugins", action="store_true", default=False, dest='ignore_vars_plugins',
|
||||
# help="When doing an --list, skip vars data from vars plugins, by default, this would include group_vars/ and host_vars/")
|
||||
|
||||
super(InventoryCLI, self).parse()
|
||||
return self.parser
|
||||
|
||||
display.verbosity = self.options.verbosity
|
||||
|
||||
self.validate_conflicts(vault_opts=True)
|
||||
def post_process_args(self, options, args):
|
||||
display.verbosity = options.verbosity
|
||||
self.validate_conflicts(options, vault_opts=True)
|
||||
|
||||
# there can be only one! and, at least, one!
|
||||
used = 0
|
||||
for opt in (self.options.list, self.options.host, self.options.graph):
|
||||
for opt in (options.list, options.host, options.graph):
|
||||
if opt:
|
||||
used += 1
|
||||
if used == 0:
|
||||
@@ -120,22 +109,23 @@ class InventoryCLI(CLI):
|
||||
raise AnsibleOptionsError("Conflicting options used, only one of --host, --graph or --list can be used at the same time.")
|
||||
|
||||
# set host pattern to default if not supplied
|
||||
if len(self.args) > 0:
|
||||
self.options.pattern = self.args[0]
|
||||
if len(args) > 0:
|
||||
options.pattern = args[0]
|
||||
else:
|
||||
self.options.pattern = 'all'
|
||||
options.pattern = 'all'
|
||||
|
||||
return options, args
|
||||
|
||||
def run(self):
|
||||
|
||||
super(InventoryCLI, self).run()
|
||||
|
||||
results = None
|
||||
|
||||
# Initialize needed objects
|
||||
self.loader, self.inventory, self.vm = self._play_prereqs(self.options)
|
||||
self.loader, self.inventory, self.vm = self._play_prereqs()
|
||||
|
||||
if self.options.host:
|
||||
hosts = self.inventory.get_hosts(self.options.host)
|
||||
results = None
|
||||
if context.CLIARGS['host']:
|
||||
hosts = self.inventory.get_hosts(context.CLIARGS['host'])
|
||||
if len(hosts) != 1:
|
||||
raise AnsibleOptionsError("You must pass a single valid host to --host parameter")
|
||||
|
||||
@@ -145,13 +135,13 @@ class InventoryCLI(CLI):
|
||||
# FIXME: should we template first?
|
||||
results = self.dump(myvars)
|
||||
|
||||
elif self.options.graph:
|
||||
elif context.CLIARGS['graph']:
|
||||
results = self.inventory_graph()
|
||||
elif self.options.list:
|
||||
elif context.CLIARGS['list']:
|
||||
top = self._get_group('all')
|
||||
if self.options.yaml:
|
||||
if context.CLIARGS['yaml']:
|
||||
results = self.yaml_inventory(top)
|
||||
elif self.options.toml:
|
||||
elif context.CLIARGS['toml']:
|
||||
results = self.toml_inventory(top)
|
||||
else:
|
||||
results = self.json_inventory(top)
|
||||
@@ -166,11 +156,11 @@ class InventoryCLI(CLI):
|
||||
|
||||
def dump(self, stuff):
|
||||
|
||||
if self.options.yaml:
|
||||
if context.CLIARGS['yaml']:
|
||||
import yaml
|
||||
from ansible.parsing.yaml.dumper import AnsibleDumper
|
||||
results = yaml.dump(stuff, Dumper=AnsibleDumper, default_flow_style=False)
|
||||
elif self.options.toml:
|
||||
elif context.CLIARGS['toml']:
|
||||
from ansible.plugins.inventory.toml import toml_dumps, HAS_TOML
|
||||
if not HAS_TOML:
|
||||
raise AnsibleError(
|
||||
@@ -227,7 +217,7 @@ class InventoryCLI(CLI):
|
||||
|
||||
def _get_host_variables(self, host):
|
||||
|
||||
if self.options.export:
|
||||
if context.CLIARGS['export']:
|
||||
hostvars = host.get_vars()
|
||||
|
||||
# FIXME: add switch to skip vars plugins
|
||||
@@ -264,7 +254,7 @@ class InventoryCLI(CLI):
|
||||
def _show_vars(self, dump, depth):
|
||||
result = []
|
||||
self._remove_internal(dump)
|
||||
if self.options.show_vars:
|
||||
if context.CLIARGS['show_vars']:
|
||||
for (name, val) in sorted(dump.items()):
|
||||
result.append(self._graph_name('{%s = %s}' % (name, val), depth))
|
||||
return result
|
||||
@@ -292,7 +282,7 @@ class InventoryCLI(CLI):
|
||||
|
||||
def inventory_graph(self):
|
||||
|
||||
start_at = self._get_group(self.options.pattern)
|
||||
start_at = self._get_group(context.CLIARGS['pattern'])
|
||||
if start_at:
|
||||
return '\n'.join(self._graph_group(start_at))
|
||||
else:
|
||||
@@ -313,7 +303,7 @@ class InventoryCLI(CLI):
|
||||
if subgroup.name not in seen:
|
||||
results.update(format_group(subgroup))
|
||||
seen.add(subgroup.name)
|
||||
if self.options.export:
|
||||
if context.CLIARGS['export']:
|
||||
results[group.name]['vars'] = self._get_group_variables(group)
|
||||
|
||||
self._remove_empty(results[group.name])
|
||||
@@ -362,8 +352,7 @@ class InventoryCLI(CLI):
|
||||
self._remove_internal(myvars)
|
||||
results[group.name]['hosts'][h.name] = myvars
|
||||
|
||||
if self.options.export:
|
||||
|
||||
if context.CLIARGS['export']:
|
||||
gvars = self._get_group_variables(group)
|
||||
if gvars:
|
||||
results[group.name]['vars'] = gvars
|
||||
@@ -403,7 +392,7 @@ class InventoryCLI(CLI):
|
||||
except KeyError:
|
||||
results[group.name]['hosts'] = {host.name: host_vars}
|
||||
|
||||
if self.options.export:
|
||||
if context.CLIARGS['export']:
|
||||
results[group.name]['vars'] = self._get_group_variables(group)
|
||||
|
||||
self._remove_empty(results[group.name])
|
||||
|
||||
Reference in New Issue
Block a user