7171 description: Toggle, if true the plugin will create Ansible groups for host collections
7272 type: boolean
7373 default: False
74+ want_ansible_roles:
75+ description: Toggle, if true the plugin will retrieve the ansible roles as a host var
76+ type: boolean
77+ default: False
7478 legacy_hostvars:
7579 description:
7680 - Toggle, if true the plugin will build legacy hostvars present in the foreman script
@@ -330,6 +334,13 @@ def _get_hostvars(self, host, vars_prefix='', omitted_vars=()):
330334 hostvars [vars_prefix + k ] = v
331335 return hostvars
332336
337+ def _get_ansible_roles (self , hid ):
338+ """Fetch all ansible roles of the host"""
339+ url = "%s/api/v2/hosts/%s/ansible_roles" % (self .foreman_url , hid )
340+ ret = self ._get_json (url )
341+
342+ return [r ['name' ] for r in ret ]
343+
333344 def _fetch_params (self ):
334345 options = ("no" , "yes" )
335346 params = dict ()
@@ -348,6 +359,7 @@ def _fetch_params(self):
348359 self .want_content_facet_attributes = report_options .get ('want_content_facet_attributes' , self .get_option ('want_content_facet_attributes' ))
349360 self .want_params = self .get_option ('want_params' )
350361 self .want_facts = self .get_option ('want_facts' )
362+ self .want_ansible_roles = self .get_option ('want_ansible_roles' )
351363 self .host_filters = self .get_option ('host_filters' )
352364
353365 params ["Organization" ] = options [self .want_organization ]
@@ -362,6 +374,7 @@ def _fetch_params(self):
362374 params ["Smart Proxies" ] = options [self .want_smart_proxies ]
363375 params ["Content Attributes" ] = options [self .want_content_facet_attributes ]
364376 params ["Host Parameters" ] = options [self .want_params ]
377+ params ["Ansible Roles" ] = options [self .want_ansible_roles ]
365378 if self .host_filters :
366379 params ["Hosts" ] = self .host_filters
367380 return params
@@ -567,6 +580,12 @@ def _populate_report_api(self):
567580 except ValueError as e :
568581 self .display .warning ("Could not set hostvar %s to '%s' for the '%s' host, skipping: %s" %
569582 (k , to_native (v ), host , to_native (e )))
583+
584+ # Set ansible roles
585+ if self .get_option ('want_ansible_roles' ):
586+ ansible_roles = self ._get_ansible_roles (host ['id' ])
587+ self .inventory .set_variable (host_name , 'foreman_roles' , ansible_roles )
588+
570589 hostvars = self .inventory .get_host (host_name ).get_vars ()
571590 self ._set_composite_vars (self .get_option ('compose' ), hostvars , host_name , strict )
572591 self ._add_host_to_composed_groups (self .get_option ('groups' ), hostvars , host_name , strict )
@@ -631,6 +650,11 @@ def _populate_host_api(self):
631650 self .display .warning ("Could not set hostvar %s to '%s' for the '%s' host, skipping: %s" %
632651 (k , to_native (v ), host , to_native (e )))
633652
653+ # Set ansible roles
654+ if self .get_option ('want_ansible_roles' ):
655+ ansible_roles = self ._get_ansible_roles (host ['id' ])
656+ self .inventory .set_variable (host_name , 'foreman_roles' , ansible_roles )
657+
634658 # set host vars from facts
635659 if self .get_option ('want_facts' ):
636660 self .inventory .set_variable (host_name , 'foreman_facts' , self ._get_facts (host ))
0 commit comments