From fbfad4674b96cea836448366066b8f734d654e86 Mon Sep 17 00:00:00 2001 From: Matt Martz Date: Tue, 26 Jun 2018 15:49:26 -0500 Subject: [PATCH] Backport #41860 for 2.6 - support group_by with implicit localhost (#41927) * Use inventory.get_host instead of direct access to inventory.hosts (#41860) * Use inventory.get_host instead of direct access to inventory.hosts. Fixes #32152 * Prevent potential side effect, by using self._inventory.localhost directly instead of get_host (cherry picked from commit 1a2ef0922fb63f09642fbcf05bf2069ee8684c07) * Add changelog for #41860 --- changelogs/fragments/group_by_localhost.yaml | 2 ++ lib/ansible/plugins/strategy/__init__.py | 7 ++++++- 2 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 changelogs/fragments/group_by_localhost.yaml diff --git a/changelogs/fragments/group_by_localhost.yaml b/changelogs/fragments/group_by_localhost.yaml new file mode 100644 index 00000000000..81835d3fba3 --- /dev/null +++ b/changelogs/fragments/group_by_localhost.yaml @@ -0,0 +1,2 @@ +bugfixes: +- group_by - support implicit localhost (https://github.com/ansible/ansible/pull/41860) diff --git a/lib/ansible/plugins/strategy/__init__.py b/lib/ansible/plugins/strategy/__init__.py index 32053b4da53..216bd7f98e1 100644 --- a/lib/ansible/plugins/strategy/__init__.py +++ b/lib/ansible/plugins/strategy/__init__.py @@ -717,7 +717,12 @@ class StrategyBase: # the host here is from the executor side, which means it was a # serialized/cloned copy and we'll need to look up the proper # host object from the master inventory - real_host = self._inventory.hosts[host.name] + real_host = self._inventory.hosts.get(host.name) + if real_host is None: + if host.name == self._inventory.localhost.name: + real_host = self._inventory.localhost + else: + raise AnsibleError('%s cannot be matched in inventory' % host.name) group_name = result_item.get('add_group') parent_group_names = result_item.get('parent_groups', [])