Add possibility to interpret global parameters value as JSON with rich_params flag (#26180)

This commit is contained in:
Eric L 2017-08-07 20:41:41 +02:00 committed by Chris Alfonso
parent f88f4394c8
commit 1fe14da226
2 changed files with 20 additions and 1 deletions

View file

@ -145,6 +145,13 @@ want_facts = True
# the script for stand-alone Foreman. # the script for stand-alone Foreman.
want_hostcollections = False want_hostcollections = False
# Whether to interpret global parameters value as JSON (if possible, else
# take as is). Only tested with Katello (Red Hat Satellite).
# This allows to define lists and dictionaries (and more complicated structures)
# variables by entering them as JSON string in Foreman parameters.
# Disabled by default as the change would else not be backward compatible.
rich_params = False
[cache] [cache]
path = . path = .
max_age = 60 max_age = 60

View file

@ -113,6 +113,12 @@ class ForemanInventory(object):
except (ConfigParser.NoOptionError, ConfigParser.NoSectionError): except (ConfigParser.NoOptionError, ConfigParser.NoSectionError):
self.want_hostcollections = False self.want_hostcollections = False
# Do we want parameters to be interpreted if possible as JSON? (no by default)
try:
self.rich_params = config.getboolean('ansible', 'rich_params')
except (ConfigParser.NoOptionError, ConfigParser.NoSectionError):
self.rich_params = False
try: try:
self.host_filters = config.get('foreman', 'host_filters') self.host_filters = config.get('foreman', 'host_filters')
except (ConfigParser.NoOptionError, ConfigParser.NoSectionError): except (ConfigParser.NoOptionError, ConfigParser.NoSectionError):
@ -209,6 +215,12 @@ class ForemanInventory(object):
for param in host_params: for param in host_params:
name = param['name'] name = param['name']
if self.rich_params:
try:
params[name] = json.loads(param['value'])
except ValueError:
params[name] = param['value']
else:
params[name] = param['value'] params[name] = param['value']
return params return params