aboutsummaryrefslogtreecommitdiff
path: root/foreman_ansible_inventory.py
diff options
context:
space:
mode:
authorAriel Salvo <asalvo@vurbia.com>2016-06-02 18:28:40 -0300
committerGuido Günther <agx@sigxcpu.org>2016-06-12 14:20:08 +0200
commit98a71a9b1dd8d5fb253f353d1d0087d641357afc (patch)
treef520f33fed61025609c0c25b85ececb837088b43 /foreman_ansible_inventory.py
parent2143ec967c03eb91ebd0d7c28d4a45ba06ce70aa (diff)
Use Foreman to calculate resulting set of params
Diffstat (limited to 'foreman_ansible_inventory.py')
-rwxr-xr-xforeman_ansible_inventory.py37
1 files 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: