From 98a71a9b1dd8d5fb253f353d1d0087d641357afc Mon Sep 17 00:00:00 2001 From: Ariel Salvo Date: Thu, 2 Jun 2016 18:28:40 -0300 Subject: Use Foreman to calculate resulting set of params --- foreman_ansible_inventory.py | 37 ++++++++++++++++++++++++------------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/foreman_ansible_inventory.py b/foreman_ansible_inventory.py index 2b323d6..8aa05d9 100755 --- a/foreman_ansible_inventory.py +++ b/foreman_ansible_inventory.py @@ -108,6 +108,11 @@ class ForemanInventory(object): self.foreman_pw = config.get('foreman', 'password') self.foreman_ssl_verify = config.getboolean('foreman', 'ssl_verify') + if config.has_option('foreman', 'server_param_resolution'): + self.foreman_server_param_resolution = config.getboolean('foreman', 'server_param_resolution') + else: + self.foreman_server_param_resolution = False + # Ansible related try: group_patterns = config.get('ansible', 'group_patterns') @@ -175,8 +180,13 @@ class ForemanInventory(object): return self.hostgroups[hid] def _get_params_by_id(self, hid): - url = "%s/api/v2/hosts/%s/parameters" % (self.foreman_url, hid) - return self._get_json(url, [404]) + if not self.foreman_server_param_resolution: + url = "%s/api/v2/hosts/%s/parameters" % (self.foreman_url, hid) + return self._get_json(url, [404]) + else: + url = "%s/api/v2/hosts/%s" % (self.foreman_url, hid) + host = self._get_json(url, [404]) + return host["all_parameters"] if "all_parameters" in host.keys() else {} def _get_facts_by_id(self, hid): url = "%s/api/v2/hosts/%s/facts" % (self.foreman_url, hid) @@ -187,20 +197,21 @@ class ForemanInventory(object): Resolve all host group params of the host using the top level hostgroup and the ancestry. """ - hostgroup_id = host['hostgroup_id'] paramgroups = [] params = {} - if hostgroup_id: - hostgroup = self._get_hostgroup_by_id(hostgroup_id) - ancestry_path = hostgroup.get('ancestry', '') - ancestry = ancestry_path.split('/') if ancestry_path is not None else [] - - # Append top level hostgroup last to overwrite lower levels - # values - ancestry.append(hostgroup_id) - paramgroups = [self._get_hostgroup_by_id(hostgroup_id)['parameters'] - for hostgroup_id in ancestry] + if not self.foreman_server_param_resolution: + hostgroup_id = host['hostgroup_id'] + if hostgroup_id: + hostgroup = self._get_hostgroup_by_id(hostgroup_id) + ancestry_path = hostgroup.get('ancestry', '') + ancestry = ancestry_path.split('/') if ancestry_path is not None else [] + + # Append top level hostgroup last to overwrite lower levels + # values + ancestry.append(hostgroup_id) + paramgroups = [self._get_hostgroup_by_id(hostgroup_id)['parameters'] + for hostgroup_id in ancestry] paramgroups += [self._get_params_by_id(host['id'])] for paramgroup in paramgroups: -- cgit v1.2.3