From 78844c800fd2a565d77ccbecdab0d011d0f72c6c Mon Sep 17 00:00:00 2001 From: Matt Williams Date: Mon, 1 Apr 2019 21:30:36 +0100 Subject: [PATCH] Fix Foreman returning host parameters (#54101) * Fix Foreman returning host parameters Foreman (1.20) returns the `all_parameters` key as a list of dicts, not a dict of key-value pairs. * Fix for type error The empty type here should be a dict, not a list as is has a `get` done on it next. (cherry picked from commit e94e80c79eab425637638205efa0eae368389dcd) * Return dict directly to avoid failing key lookup (cherry picked from commit 545b98645d04b2d26eb95354d8edf4d8e9d4e2a2) * Add changelog fragment for #54333 --- .../54101-backport-51034-foreman-inventory.yaml | 2 ++ lib/ansible/plugins/inventory/foreman.py | 10 +++++----- 2 files changed, 7 insertions(+), 5 deletions(-) create mode 100644 changelogs/fragments/54101-backport-51034-foreman-inventory.yaml diff --git a/changelogs/fragments/54101-backport-51034-foreman-inventory.yaml b/changelogs/fragments/54101-backport-51034-foreman-inventory.yaml new file mode 100644 index 00000000000..f793dd631da --- /dev/null +++ b/changelogs/fragments/54101-backport-51034-foreman-inventory.yaml @@ -0,0 +1,2 @@ +bugfixes: + - foreman - fix Foreman returning host parameters diff --git a/lib/ansible/plugins/inventory/foreman.py b/lib/ansible/plugins/inventory/foreman.py index e5c0685d125..24123a4f27b 100644 --- a/lib/ansible/plugins/inventory/foreman.py +++ b/lib/ansible/plugins/inventory/foreman.py @@ -166,8 +166,8 @@ class InventoryModule(BaseInventoryPlugin, Cacheable): url = "%s/api/v2/hosts/%s" % (self.foreman_url, hid) ret = self._get_json(url, [404]) if not ret or not isinstance(ret, MutableMapping) or not ret.get('all_parameters', False): - ret = {'all_parameters': [{}]} - return ret.get('all_parameters')[0] + return {} + return ret.get('all_parameters') def _get_facts_by_id(self, hid): url = "%s/api/v2/hosts/%s/facts" % (self.foreman_url, hid) @@ -220,11 +220,11 @@ class InventoryModule(BaseInventoryPlugin, Cacheable): # set host vars from params if self.get_option('want_params'): - for k, v in self._get_all_params_by_id(host['id']).items(): + for p in self._get_all_params_by_id(host['id']): try: - self.inventory.set_variable(host['name'], k, v) + self.inventory.set_variable(host['name'], p['name'], p['value']) except ValueError as e: - self.display.warning("Could not set parameter hostvar for %s, skipping %s: %s" % (host, k, to_native(e))) + self.display.warning("Could not set parameter hostvar for %s, skipping %s: %s" % (host, p['name'], to_native(p['value']))) # set host vars from facts if self.get_option('want_facts'):