From f0b6f76bc61cbef5f45fcf8cfb39ca5767f074f6 Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Wed, 13 May 2020 15:03:30 -0400 Subject: [PATCH] only show_vars when showing vars (#69365) * only show_vars when showing vars avoid processing function params that can be very expensive and might not be used at all in called function. fixes #69357 * Update changelogs/fragments/69357_optimize_inventory_graph_wo_vars.yml Co-authored-by: Sloane Hertel Co-authored-by: Sloane Hertel --- .../69357_optimize_inventory_graph_wo_vars.yml | 2 ++ lib/ansible/cli/inventory.py | 11 ++++++----- 2 files changed, 8 insertions(+), 5 deletions(-) create mode 100644 changelogs/fragments/69357_optimize_inventory_graph_wo_vars.yml diff --git a/changelogs/fragments/69357_optimize_inventory_graph_wo_vars.yml b/changelogs/fragments/69357_optimize_inventory_graph_wo_vars.yml new file mode 100644 index 00000000000..10f8cd3cb2a --- /dev/null +++ b/changelogs/fragments/69357_optimize_inventory_graph_wo_vars.yml @@ -0,0 +1,2 @@ +bugfixes: + - Avoid running subfunctions that are passed to show_vars function when it will be a noop. diff --git a/lib/ansible/cli/inventory.py b/lib/ansible/cli/inventory.py index 7ff24e75813..f8bf3c703cf 100644 --- a/lib/ansible/cli/inventory.py +++ b/lib/ansible/cli/inventory.py @@ -240,9 +240,8 @@ class InventoryCLI(CLI): @staticmethod def _show_vars(dump, depth): result = [] - if context.CLIARGS['show_vars']: - for (name, val) in sorted(dump.items()): - result.append(InventoryCLI._graph_name('{%s = %s}' % (name, val), depth)) + for (name, val) in sorted(dump.items()): + result.append(InventoryCLI._graph_name('{%s = %s}' % (name, val), depth)) return result @staticmethod @@ -261,9 +260,11 @@ 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(self._get_host_variables(host), depth + 1)) + if context.CLIARGS['show_vars']: + result.extend(self._show_vars(self._get_host_variables(host), depth + 1)) - result.extend(self._show_vars(self._get_group_variables(group), depth)) + if context.CLIARGS['show_vars']: + result.extend(self._show_vars(self._get_group_variables(group), depth)) return result