Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions plugins/inventory/foreman.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@
description: Toggle, if true the plugin will create Ansible groups for host collections
type: boolean
default: False
want_ansible_roles:
description: Toggle, if true the plugin will retrieve the ansible roles as a host var
type: boolean
default: False
legacy_hostvars:
description:
- Toggle, if true the plugin will build legacy hostvars present in the foreman script
Expand Down Expand Up @@ -330,6 +334,13 @@ def _get_hostvars(self, host, vars_prefix='', omitted_vars=()):
hostvars[vars_prefix + k] = v
return hostvars

def _get_ansible_roles(self, hid):
"""Fetch all ansible roles of the host"""
url = "%s/api/v2/hosts/%s/ansible_roles" % (self.foreman_url, hid)
ret = self._get_json(url)

return [r['name'] for r in ret]

def _fetch_params(self):
options = ("no", "yes")
params = dict()
Expand All @@ -348,6 +359,7 @@ def _fetch_params(self):
self.want_content_facet_attributes = report_options.get('want_content_facet_attributes', self.get_option('want_content_facet_attributes'))
self.want_params = self.get_option('want_params')
self.want_facts = self.get_option('want_facts')
self.want_ansible_roles = self.get_option('want_ansible_roles')
self.host_filters = self.get_option('host_filters')

params["Organization"] = options[self.want_organization]
Expand All @@ -362,6 +374,7 @@ def _fetch_params(self):
params["Smart Proxies"] = options[self.want_smart_proxies]
params["Content Attributes"] = options[self.want_content_facet_attributes]
params["Host Parameters"] = options[self.want_params]
params["Ansible Roles"] = options[self.want_ansible_roles]
if self.host_filters:
params["Hosts"] = self.host_filters
return params
Expand Down Expand Up @@ -567,6 +580,12 @@ def _populate_report_api(self):
except ValueError as e:
self.display.warning("Could not set hostvar %s to '%s' for the '%s' host, skipping: %s" %
(k, to_native(v), host, to_native(e)))

# Set ansible roles
if self.get_option('want_ansible_roles'):
ansible_roles = self._get_ansible_roles(host['id'])
self.inventory.set_variable(host_name, 'foreman_ansible_roles', ansible_roles)

hostvars = self.inventory.get_host(host_name).get_vars()
self._set_composite_vars(self.get_option('compose'), hostvars, host_name, strict)
self._add_host_to_composed_groups(self.get_option('groups'), hostvars, host_name, strict)
Expand Down Expand Up @@ -631,6 +650,11 @@ def _populate_host_api(self):
self.display.warning("Could not set hostvar %s to '%s' for the '%s' host, skipping: %s" %
(k, to_native(v), host, to_native(e)))

# Set ansible roles
if self.get_option('want_ansible_roles'):
ansible_roles = self._get_ansible_roles(host['id'])
self.inventory.set_variable(host_name, 'foreman_ansible_roles', ansible_roles)

# set host vars from facts
if self.get_option('want_facts'):
self.inventory.set_variable(host_name, 'foreman_facts', self._get_facts(host))
Expand Down