show host_vars/ also in --graph (#56307)
* show host_vars/ also in --graph fixes #53422
This commit is contained in:
parent
e9c83b7a17
commit
de87b25a45
2 changed files with 10 additions and 10 deletions
2
changelogs/fragments/show_host_vars_in_graph.yml
Normal file
2
changelogs/fragments/show_host_vars_in_graph.yml
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
bugfixes:
|
||||||
|
- show host_vars in ansible-inventory's --graph option.
|
|
@ -132,7 +132,6 @@ class InventoryCLI(CLI):
|
||||||
raise AnsibleOptionsError("You must pass a single valid host to --host parameter")
|
raise AnsibleOptionsError("You must pass a single valid host to --host parameter")
|
||||||
|
|
||||||
myvars = self._get_host_variables(host=hosts[0])
|
myvars = self._get_host_variables(host=hosts[0])
|
||||||
self._remove_internal(myvars)
|
|
||||||
|
|
||||||
# FIXME: should we template first?
|
# FIXME: should we template first?
|
||||||
results = self.dump(myvars)
|
results = self.dump(myvars)
|
||||||
|
@ -224,21 +223,22 @@ class InventoryCLI(CLI):
|
||||||
if group.priority != 1:
|
if group.priority != 1:
|
||||||
res['ansible_group_priority'] = group.priority
|
res['ansible_group_priority'] = group.priority
|
||||||
|
|
||||||
return res
|
return self._remove_internal(res)
|
||||||
|
|
||||||
def _get_host_variables(self, host):
|
def _get_host_variables(self, host):
|
||||||
|
|
||||||
if context.CLIARGS['export']:
|
if context.CLIARGS['export']:
|
||||||
|
# only get vars defined directly host
|
||||||
hostvars = host.get_vars()
|
hostvars = host.get_vars()
|
||||||
|
|
||||||
# FIXME: add switch to skip vars plugins
|
# FIXME: add switch to skip vars plugins, add vars plugin info
|
||||||
# add vars plugin info
|
|
||||||
for inventory_dir in self.inventory._sources:
|
for inventory_dir in self.inventory._sources:
|
||||||
hostvars = combine_vars(hostvars, self.get_plugin_vars(inventory_dir, host))
|
hostvars = combine_vars(hostvars, self.get_plugin_vars(inventory_dir, host))
|
||||||
else:
|
else:
|
||||||
|
# get all vars flattened by host, but skip magic hostvars
|
||||||
hostvars = self.vm.get_vars(host=host, include_hostvars=False)
|
hostvars = self.vm.get_vars(host=host, include_hostvars=False)
|
||||||
|
|
||||||
return hostvars
|
return self._remove_internal(hostvars)
|
||||||
|
|
||||||
def _get_group(self, gname):
|
def _get_group(self, gname):
|
||||||
group = self.inventory.groups.get(gname)
|
group = self.inventory.groups.get(gname)
|
||||||
|
@ -251,6 +251,8 @@ class InventoryCLI(CLI):
|
||||||
if internal in dump:
|
if internal in dump:
|
||||||
del dump[internal]
|
del dump[internal]
|
||||||
|
|
||||||
|
return dump
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _remove_empty(dump):
|
def _remove_empty(dump):
|
||||||
# remove empty keys
|
# remove empty keys
|
||||||
|
@ -261,7 +263,6 @@ class InventoryCLI(CLI):
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _show_vars(dump, depth):
|
def _show_vars(dump, depth):
|
||||||
result = []
|
result = []
|
||||||
InventoryCLI._remove_internal(dump)
|
|
||||||
if context.CLIARGS['show_vars']:
|
if context.CLIARGS['show_vars']:
|
||||||
for (name, val) in sorted(dump.items()):
|
for (name, val) in sorted(dump.items()):
|
||||||
result.append(InventoryCLI._graph_name('{%s = %s}' % (name, val), depth))
|
result.append(InventoryCLI._graph_name('{%s = %s}' % (name, val), depth))
|
||||||
|
@ -283,7 +284,7 @@ class InventoryCLI(CLI):
|
||||||
if group.name != 'all':
|
if group.name != 'all':
|
||||||
for host in sorted(group.hosts, key=attrgetter('name')):
|
for host in sorted(group.hosts, key=attrgetter('name')):
|
||||||
result.append(self._graph_name(host.name, depth))
|
result.append(self._graph_name(host.name, depth))
|
||||||
result.extend(self._show_vars(host.get_vars(), depth + 1))
|
result.extend(self._show_vars(self._get_host_variables(host), depth + 1))
|
||||||
|
|
||||||
result.extend(self._show_vars(self._get_group_variables(group), depth))
|
result.extend(self._show_vars(self._get_group_variables(group), depth))
|
||||||
|
|
||||||
|
@ -329,7 +330,6 @@ class InventoryCLI(CLI):
|
||||||
for host in hosts:
|
for host in hosts:
|
||||||
hvars = self._get_host_variables(host)
|
hvars = self._get_host_variables(host)
|
||||||
if hvars:
|
if hvars:
|
||||||
self._remove_internal(hvars)
|
|
||||||
results['_meta']['hostvars'][host.name] = hvars
|
results['_meta']['hostvars'][host.name] = hvars
|
||||||
|
|
||||||
return results
|
return results
|
||||||
|
@ -358,7 +358,6 @@ class InventoryCLI(CLI):
|
||||||
if h.name not in seen: # avoid defining host vars more than once
|
if h.name not in seen: # avoid defining host vars more than once
|
||||||
seen.append(h.name)
|
seen.append(h.name)
|
||||||
myvars = self._get_host_variables(host=h)
|
myvars = self._get_host_variables(host=h)
|
||||||
self._remove_internal(myvars)
|
|
||||||
results[group.name]['hosts'][h.name] = myvars
|
results[group.name]['hosts'][h.name] = myvars
|
||||||
|
|
||||||
if context.CLIARGS['export']:
|
if context.CLIARGS['export']:
|
||||||
|
@ -393,7 +392,6 @@ class InventoryCLI(CLI):
|
||||||
if host.name not in seen:
|
if host.name not in seen:
|
||||||
seen.add(host.name)
|
seen.add(host.name)
|
||||||
host_vars = self._get_host_variables(host=host)
|
host_vars = self._get_host_variables(host=host)
|
||||||
self._remove_internal(host_vars)
|
|
||||||
else:
|
else:
|
||||||
host_vars = {}
|
host_vars = {}
|
||||||
try:
|
try:
|
||||||
|
|
Loading…
Reference in a new issue