aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGuido Günther <agx@sigxcpu.org>2016-06-12 14:15:13 +0200
committerGuido Günther <agx@sigxcpu.org>2016-06-12 14:33:01 +0200
commit8471ade8fd3dcb4d43eed91af45cfbe0b4deae4a (patch)
treec0ff69c877bb1dcd773ae95332f2d64a0ffb836d
parent98a71a9b1dd8d5fb253f353d1d0087d641357afc (diff)
Resolve params via all_parameters only
No need to do manual resolution since Foreman does this internally already. Also reduces the number of API requests since we don't need to resolve hostgroups. Closes: #9
-rwxr-xr-xforeman_ansible_inventory.py41
1 files changed, 7 insertions, 34 deletions
diff --git a/foreman_ansible_inventory.py b/foreman_ansible_inventory.py
index 8aa05d9..2b2e68a 100755
--- a/foreman_ansible_inventory.py
+++ b/foreman_ansible_inventory.py
@@ -108,11 +108,6 @@ 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')
@@ -179,14 +174,9 @@ class ForemanInventory(object):
self.hostgroups[hid] = self._get_json(url)
return self.hostgroups[hid]
- def _get_params_by_id(self, hid):
- 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_all_params_by_id(self, hid):
+ url = "%s/api/v2/hosts/%s" % (self.foreman_url, hid)
+ return self._get_json(url, [404]).get('all_parameters', {})
def _get_facts_by_id(self, hid):
url = "%s/api/v2/hosts/%s/facts" % (self.foreman_url, hid)
@@ -194,30 +184,13 @@ class ForemanInventory(object):
def _resolve_params(self, host):
"""
- Resolve all host group params of the host using the top level
- hostgroup and the ancestry.
+ Fetch host params and convert to dict
"""
- paramgroups = []
params = {}
- 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:
- for param in paramgroup:
- name = param['name']
- params[name] = param['value']
+ for param in self._get_all_params_by_id(host['id']):
+ name = param['name']
+ params[name] = param['value']
return params