Now respecting the HAS_YAML variable

This commit is contained in:
John Westcott IV
2020-06-02 14:54:44 -04:00
parent 9d42b8f0f2
commit aa54cf097b

View File

@@ -154,37 +154,44 @@ class TowerModule(AnsibleModule):
# First try to yaml load the content (which will also load json) # First try to yaml load the content (which will also load json)
try: try:
config_data = yaml.load(config_string, Loader=yaml.SafeLoader) try_config_parsing = True
# If this is an actual ini file, yaml will return the whole thing as a string instead of a dict if HAS_YAML:
if type(config_data) is not dict: try:
raise AssertionError("The yaml config file is not properly formatted as a dict.") config_data = yaml.load(config_string, Loader=yaml.SafeLoader)
# If this is an actual ini file, yaml will return the whole thing as a string instead of a dict
if type(config_data) is not dict:
raise AssertionError("The yaml config file is not properly formatted as a dict.")
try_config_parsing = False
except(AttributeError, yaml.YAMLError, AssertionError): except(AttributeError, yaml.YAMLError, AssertionError):
# TowerCLI used to support a config file with a missing [general] section by prepending it if missing try_config_parsing = True
if '[general]' not in config_string:
config_string = '[general]{0}'.format(config_string)
config = ConfigParser() if try_config_parsing:
# TowerCLI used to support a config file with a missing [general] section by prepending it if missing
if '[general]' not in config_string:
config_string = '[general]{0}'.format(config_string)
try: config = ConfigParser()
placeholder_file = StringIO(config_string)
# py2 ConfigParser has readfp, that has been deprecated in favor of read_file in py3
# This "if" removes the deprecation warning
if hasattr(config, 'read_file'):
config.read_file(placeholder_file)
else:
config.readfp(placeholder_file)
# If we made it here then we have values from reading the ini file, so let's pull them out into a dict try:
config_data = {} placeholder_file = StringIO(config_string)
for honorred_setting in self.honorred_settings: # py2 ConfigParser has readfp, that has been deprecated in favor of read_file in py3
try: # This "if" removes the deprecation warning
config_data[honorred_setting] = config.get('general', honorred_setting) if hasattr(config, 'read_file'):
except (NoOptionError): config.read_file(placeholder_file)
pass else:
config.readfp(placeholder_file)
except Exception as e: # If we made it here then we have values from reading the ini file, so let's pull them out into a dict
raise ConfigFileException("An unknown exception occured trying to ini load config file: {0}".format(e)) config_data = {}
for honorred_setting in self.honorred_settings:
try:
config_data[honorred_setting] = config.get('general', honorred_setting)
except NoOptionError:
pass
except Exception as e:
raise ConfigFileException("An unknown exception occured trying to ini load config file: {0}".format(e))
except Exception as e: except Exception as e:
raise ConfigFileException("An unknown exception occured trying to load config file: {0}".format(e)) raise ConfigFileException("An unknown exception occured trying to load config file: {0}".format(e))