From bf502749456a3e0c93d700a0f777531dcc78ccbe Mon Sep 17 00:00:00 2001 From: uberlinuxguy Date: Thu, 24 May 2018 22:45:21 -0400 Subject: [PATCH] Added ability to read config from env. instead of .init for use in Asible Tower (and AWX) (#36179) * Added ability to read config from env. instead of .init for use in Ansible Tower (and AWX) * Detect when NoneType is passed to cache_path * Formatting fixes --- contrib/inventory/cobbler.py | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/contrib/inventory/cobbler.py b/contrib/inventory/cobbler.py index 2546da0526..2762041dc9 100755 --- a/contrib/inventory/cobbler.py +++ b/contrib/inventory/cobbler.py @@ -88,8 +88,10 @@ class CobblerInventory(object): self.inventory = dict() # A list of groups and the hosts in that group self.cache = dict() # Details about hosts in the inventory + self.ignore_settings = False # used to only look at env vars for settings. - # Read settings and parse CLI arguments + # Read env vars, read settings, and parse CLI arguments + self.parse_env_vars() self.read_settings() self.parse_cli_args() @@ -137,6 +139,9 @@ class CobblerInventory(object): def read_settings(self): """ Reads the settings from the cobbler.ini file """ + if(self.ignore_settings): + return + config = ConfigParser.SafeConfigParser() config.read(os.path.dirname(os.path.realpath(__file__)) + '/cobbler.ini') @@ -154,6 +159,34 @@ class CobblerInventory(object): self.cache_path_inventory = cache_path + "/ansible-cobbler.index" self.cache_max_age = config.getint('cobbler', 'cache_max_age') + def parse_env_vars(self): + """ Reads the settings from the environment """ + + # Env. Vars: + # COBBLER_host + # COBBLER_username + # COBBLER_password + # COBBLER_cache_path + # COBBLER_cache_max_age + # COBBLER_ignore_settings + + self.cobbler_host = os.getenv('COBBLER_host', None) + self.cobbler_username = os.getenv('COBBLER_username', None) + self.cobbler_password = os.getenv('COBBLER_password', None) + + # Cache related + cache_path = os.getenv('COBBLER_cache_path', None) + if(cache_path is not None): + self.cache_path_cache = cache_path + "/ansible-cobbler.cache" + self.cache_path_inventory = cache_path + "/ansible-cobbler.index" + + self.cache_max_age = int(os.getenv('COBBLER_cache_max_age', "30")) + + # ignore_settings is used to ignore the settings file, for use in Ansible + # Tower (or AWX inventory scripts and not throw python exceptions.) + if(os.getenv('COBBLER_ignore_settings', False) == "True"): + self.ignore_settings = True + def parse_cli_args(self): """ Command line argument processing """