Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inv export #36188

Merged
merged 4 commits into from
Feb 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
89 changes: 83 additions & 6 deletions lib/ansible/cli/inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@
import optparse
from operator import attrgetter

from ansible import constants as C
from ansible.cli import CLI
from ansible.errors import AnsibleOptionsError
from ansible.errors import AnsibleError, AnsibleOptionsError
from ansible.inventory.host import Host
from ansible.plugins.loader import vars_loader
from ansible.parsing.dataloader import DataLoader
from ansible.utils.vars import combine_vars

try:
from __main__ import display
Expand Down Expand Up @@ -83,11 +87,20 @@ def parse(self):
self.parser.add_option_group(action_group)

# Options

# graph
self.parser.add_option("-y", "--yaml", action="store_true", default=False, dest='yaml',
help='Use YAML format instead of default JSON, ignored for --graph')
self.parser.add_option("--vars", action="store_true", default=False, dest='show_vars',
help='Add vars to graph display, ignored unless used with --graph')

# list
self.parser.add_option("--export", action="store_true", default=C.INVENTORY_EXPORT, dest='export',
help="When doing an --list, represent in a way that is optimized for export,"
"not as an accurate representation of how Ansible has processed it")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The help blurb is kind of vague. optimized how and for what?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for export .. as for 'how' .. it seems too long to detail here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to make an attempt at word-smithing

When doing an --list, represent shared variables inside groups or the inventory,
which has a smaller memory footprint but is not Ansible's internal representation

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'll just merge as is for now, let the 'wordsmithing' update it in latter patches so as to not keep this waiting

# 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()

display.verbosity = self.options.verbosity
Expand Down Expand Up @@ -183,11 +196,63 @@ def dump(self, stuff):

return results

# FIXME: refactor to use same for VM
def get_plugin_vars(self, path, entity):

data = {}

def _get_plugin_vars(plugin, path, entities):
data = {}
try:
data = plugin.get_vars(self.loader, path, entity)
except AttributeError:
try:
if isinstance(entity, Host):
data.update(plugin.get_host_vars(entity.name))
else:
data.update(plugin.get_group_vars(entity.name))
except AttributeError:
if hasattr(plugin, 'run'):
raise AnsibleError("Cannot use v1 type vars plugin %s from %s" % (plugin._load_name, plugin._original_path))
else:
raise AnsibleError("Invalid vars plugin %s from %s" % (plugin._load_name, plugin._original_path))
return data

for plugin in vars_loader.all():
data = combine_vars(data, _get_plugin_vars(plugin, path, entity))

return data

def _get_group_variables(self, group):

# get info from inventory source
res = group.get_vars()

# FIXME: add switch to skip vars plugins
# add vars plugin info
for inventory_dir in self.inventory._sources:
res = combine_vars(res, self.get_plugin_vars(inventory_dir, group))

if group.priority != 1:
res['ansible_group_priority'] = group.priority

return res

def _get_host_variables(self, host):
if self._new_api:
hostvars = self.vm.get_vars(host=host)

if self.options.export:
hostvars = host.get_vars()

# FIXME: add switch to skip vars plugins
# add vars plugin info
for inventory_dir in self.inventory._sources:
hostvars = combine_vars(hostvars, self.get_plugin_vars(inventory_dir, host))
else:
hostvars = self.vm.get_vars(self.loader, host=host)
if self._new_api:
hostvars = self.vm.get_vars(host=host, include_hostvars=False)
else:
hostvars = self.vm.get_vars(self.loader, host=host, include_hostvars=False)

return hostvars

def _get_group(self, gname):
Expand Down Expand Up @@ -257,8 +322,11 @@ def format_group(group):
for subgroup in sorted(group.child_groups, key=attrgetter('name')):
results[group.name]['children'].append(subgroup.name)
results.update(format_group(subgroup))
if self.options.export:
results[group.name]['vars'] = self._get_group_variables(group)

self._remove_empty(results[group.name])

return results

results = format_group(top)
Expand All @@ -267,8 +335,10 @@ def format_group(group):
results['_meta'] = {'hostvars': {}}
hosts = self.inventory.get_hosts()
for host in hosts:
results['_meta']['hostvars'][host.name] = self._get_host_variables(host=host)
self._remove_internal(results['_meta']['hostvars'][host.name])
hvars = self._get_host_variables(host)
if hvars:
self._remove_internal(hvars)
results['_meta']['hostvars'][host.name] = hvars

return results

Expand Down Expand Up @@ -299,7 +369,14 @@ def format_group(group):
self._remove_internal(myvars)
results[group.name]['hosts'][h.name] = myvars

if self.options.export:

gvars = self._get_group_variables(group)
if gvars:
results[group.name]['vars'] = gvars

self._remove_empty(results[group.name])

return results

return format_group(top)
8 changes: 8 additions & 0 deletions lib/ansible/config/base.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1309,6 +1309,14 @@ INVENTORY_ENABLED:
ini:
- {key: enable_plugins, section: inventory}
type: list
INVENTORY_EXPORT:
name: Set ansible-inventory into export mode
default: False
description: Controls if ansible-inventory will accurately reflect Ansible's view into inventory or its optimized for exporting.
env: [{name: ANSIBLE_INVENTORY_EXPORT}]
ini:
- {key: export, section: inventory}
type: bool
INVENTORY_IGNORE_EXTS:
name: Inventory ignore extensions
default: "{{(BLACKLIST_EXTS + ( '~', '.orig', '.ini', '.cfg', '.retry'))}}"
Expand Down