From 1fe14da226ab129577d4c3902fbd376bd3a5d9a8 Mon Sep 17 00:00:00 2001 From: Eric L Date: Mon, 7 Aug 2017 20:41:41 +0200 Subject: [PATCH] Add possibility to interpret global parameters value as JSON with rich_params flag (#26180) --- contrib/inventory/foreman.ini | 7 +++++++ contrib/inventory/foreman.py | 14 +++++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/contrib/inventory/foreman.ini b/contrib/inventory/foreman.ini index d9c294b5996..32091a58f06 100644 --- a/contrib/inventory/foreman.ini +++ b/contrib/inventory/foreman.ini @@ -145,6 +145,13 @@ want_facts = True # the script for stand-alone Foreman. 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] path = . max_age = 60 diff --git a/contrib/inventory/foreman.py b/contrib/inventory/foreman.py index e3f6a2a8e2d..b21ca11be85 100755 --- a/contrib/inventory/foreman.py +++ b/contrib/inventory/foreman.py @@ -113,6 +113,12 @@ class ForemanInventory(object): except (ConfigParser.NoOptionError, ConfigParser.NoSectionError): 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: self.host_filters = config.get('foreman', 'host_filters') except (ConfigParser.NoOptionError, ConfigParser.NoSectionError): @@ -209,7 +215,13 @@ class ForemanInventory(object): for param in host_params: name = param['name'] - params[name] = param['value'] + if self.rich_params: + try: + params[name] = json.loads(param['value']) + except ValueError: + params[name] = param['value'] + else: + params[name] = param['value'] return params