From c1343cc304296bfcec242e31faa3ccf3e4a9880c Mon Sep 17 00:00:00 2001 From: Matt Davis <6775756+nitzmahone@users.noreply.github.com> Date: Tue, 3 Oct 2023 17:20:14 -0400 Subject: [PATCH] switch available_hosts to `frozenset` from `list` in ansible-inventory (#81870) * major speedup large inventories, since it's consulted iteratively --- changelogs/fragments/inv_available_hosts_to_frozenset.yml | 2 ++ lib/ansible/cli/inventory.py | 6 +++--- 2 files changed, 5 insertions(+), 3 deletions(-) create mode 100644 changelogs/fragments/inv_available_hosts_to_frozenset.yml diff --git a/changelogs/fragments/inv_available_hosts_to_frozenset.yml b/changelogs/fragments/inv_available_hosts_to_frozenset.yml new file mode 100644 index 00000000000..094fe6ddd54 --- /dev/null +++ b/changelogs/fragments/inv_available_hosts_to_frozenset.yml @@ -0,0 +1,2 @@ +bugfixes: + - ansible-inventory - index available_hosts for major performance boost when dumping large inventories \ No newline at end of file diff --git a/lib/ansible/cli/inventory.py b/lib/ansible/cli/inventory.py index 448214cee43..3550079bf8e 100755 --- a/lib/ansible/cli/inventory.py +++ b/lib/ansible/cli/inventory.py @@ -325,7 +325,7 @@ class InventoryCLI(CLI): return results hosts = self.inventory.get_hosts(top.name) - results = format_group(top, [h.name for h in hosts]) + results = format_group(top, frozenset(h.name for h in hosts)) # populate meta results['_meta'] = {'hostvars': {}} @@ -381,7 +381,7 @@ class InventoryCLI(CLI): return results - available_hosts = [h.name for h in self.inventory.get_hosts(top.name)] + available_hosts = frozenset(h.name for h in self.inventory.get_hosts(top.name)) return format_group(top, available_hosts) def toml_inventory(self, top): @@ -425,7 +425,7 @@ class InventoryCLI(CLI): return results - available_hosts = [h.name for h in self.inventory.get_hosts(top.name)] + available_hosts = frozenset(h.name for h in self.inventory.get_hosts(top.name)) results = format_group(top, available_hosts) return results