From de87b25a450f08307e7f7bcb241144e1b2c52aeb Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Fri, 14 Jun 2019 13:40:22 -0400 Subject: [PATCH] show host_vars/ also in --graph (#56307) * show host_vars/ also in --graph fixes #53422 --- .../fragments/show_host_vars_in_graph.yml | 2 ++ lib/ansible/cli/inventory.py | 18 ++++++++---------- 2 files changed, 10 insertions(+), 10 deletions(-) create mode 100644 changelogs/fragments/show_host_vars_in_graph.yml diff --git a/changelogs/fragments/show_host_vars_in_graph.yml b/changelogs/fragments/show_host_vars_in_graph.yml new file mode 100644 index 00000000000..fa747080a56 --- /dev/null +++ b/changelogs/fragments/show_host_vars_in_graph.yml @@ -0,0 +1,2 @@ +bugfixes: + - show host_vars in ansible-inventory's --graph option. diff --git a/lib/ansible/cli/inventory.py b/lib/ansible/cli/inventory.py index b24efc38d8e..81540db82e7 100644 --- a/lib/ansible/cli/inventory.py +++ b/lib/ansible/cli/inventory.py @@ -132,7 +132,6 @@ class InventoryCLI(CLI): raise AnsibleOptionsError("You must pass a single valid host to --host parameter") myvars = self._get_host_variables(host=hosts[0]) - self._remove_internal(myvars) # FIXME: should we template first? results = self.dump(myvars) @@ -224,21 +223,22 @@ class InventoryCLI(CLI): if group.priority != 1: res['ansible_group_priority'] = group.priority - return res + return self._remove_internal(res) def _get_host_variables(self, host): if context.CLIARGS['export']: + # only get vars defined directly host hostvars = host.get_vars() - # FIXME: add switch to skip vars plugins - # add vars plugin info + # 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: + # get all vars flattened by host, but skip magic hostvars hostvars = self.vm.get_vars(host=host, include_hostvars=False) - return hostvars + return self._remove_internal(hostvars) def _get_group(self, gname): group = self.inventory.groups.get(gname) @@ -251,6 +251,8 @@ class InventoryCLI(CLI): if internal in dump: del dump[internal] + return dump + @staticmethod def _remove_empty(dump): # remove empty keys @@ -261,7 +263,6 @@ class InventoryCLI(CLI): @staticmethod def _show_vars(dump, depth): result = [] - InventoryCLI._remove_internal(dump) if context.CLIARGS['show_vars']: for (name, val) in sorted(dump.items()): result.append(InventoryCLI._graph_name('{%s = %s}' % (name, val), depth)) @@ -283,7 +284,7 @@ class InventoryCLI(CLI): if group.name != 'all': for host in sorted(group.hosts, key=attrgetter('name')): 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)) @@ -329,7 +330,6 @@ class InventoryCLI(CLI): for host in hosts: hvars = self._get_host_variables(host) if hvars: - self._remove_internal(hvars) results['_meta']['hostvars'][host.name] = hvars return results @@ -358,7 +358,6 @@ class InventoryCLI(CLI): if h.name not in seen: # avoid defining host vars more than once seen.append(h.name) myvars = self._get_host_variables(host=h) - self._remove_internal(myvars) results[group.name]['hosts'][h.name] = myvars if context.CLIARGS['export']: @@ -393,7 +392,6 @@ class InventoryCLI(CLI): if host.name not in seen: seen.add(host.name) host_vars = self._get_host_variables(host=host) - self._remove_internal(host_vars) else: host_vars = {} try: