diff --git a/plugins/module_utils/ansible_freeipa_module.py b/plugins/module_utils/ansible_freeipa_module.py index 3c25c7953c9bb72fbb54a9262c82297128326cf3..17d02d0e2490125064fd6d371c857f850c998e73 100644 --- a/plugins/module_utils/ansible_freeipa_module.py +++ b/plugins/module_utils/ansible_freeipa_module.py @@ -398,11 +398,32 @@ else: return value - def module_params_get(module, name): - return _afm_convert(module.params.get(name)) - - def module_params_get_lowercase(module, name): + def module_params_get(module, name, allow_empty_string=False): value = _afm_convert(module.params.get(name)) + + # Fail on empty strings in the list or if allow_empty_string is True + # if there is another entry in the list together with the empty + # string. + # Due to an issue in Ansible it is possible to use the empty string + # "" for lists with choices, even if the empty list is not part of + # the choices. + # Ansible issue https://github.com/ansible/ansible/issues/77108 + if isinstance(value, list): + for val in value: + if isinstance(val, (str, unicode)) and not val: + if not allow_empty_string: + module.fail_json( + msg="Parameter '%s' contains an empty string" % + name) + elif len(value) > 1: + module.fail_json( + msg="Parameter '%s' may not contain another " + "entry together with an empty string" % name) + + return value + + def module_params_get_lowercase(module, name, allow_empty_string=False): + value = module_params_get(module, name, allow_empty_string) if isinstance(value, list): value = [v.lower() for v in value] if isinstance(value, (str, unicode)): @@ -897,7 +918,7 @@ else: finally: temp_kdestroy(ccache_dir, ccache_name) - def params_get(self, name): + def params_get(self, name, allow_empty_string=False): """ Retrieve value set for module parameter. @@ -905,11 +926,13 @@ else: ---------- name: string The name of the parameter to retrieve. + allow_empty_string: bool + The parameter allowes to have empty strings in a list """ - return module_params_get(self, name) + return module_params_get(self, name, allow_empty_string) - def params_get_lowercase(self, name): + def params_get_lowercase(self, name, allow_empty_string=False): """ Retrieve value set for module parameter as lowercase, if not None. @@ -917,9 +940,11 @@ else: ---------- name: string The name of the parameter to retrieve. + allow_empty_string: bool + The parameter allowes to have empty strings in a list """ - return module_params_get_lowercase(self, name) + return module_params_get_lowercase(self, name, allow_empty_string) def params_fail_used_invalid(self, invalid_params, state, action=None): """