diff --git a/changelogs/fragments/7242_ignore_similar_chars.yml b/changelogs/fragments/7242_ignore_similar_chars.yml new file mode 100644 index 0000000000..5b65a123d9 --- /dev/null +++ b/changelogs/fragments/7242_ignore_similar_chars.yml @@ -0,0 +1,2 @@ +minor_changes: + - random_string - added new ``ignore_similar_chars`` and ``similar_chars`` option to ignore certain chars (https://github.com/ansible-collections/community.general/pull/7242). diff --git a/plugins/lookup/random_string.py b/plugins/lookup/random_string.py index 3e873e1cf1..d810f59366 100644 --- a/plugins/lookup/random_string.py +++ b/plugins/lookup/random_string.py @@ -80,6 +80,19 @@ DOCUMENTATION = r""" - Override all values of O(numbers), O(upper), O(lower), and O(special) with the given list of characters. type: str + ignore_similar_chars: + description: + - Ignore similar characters, such as V(l) and V(1), or V(O) and V(0). + - These characters can be configured in O(similar_chars). + default: false + type: bool + version_added: 7.5.0 + similar_chars: + description: + - Overide a list of characters not to be use in the string. + default: "il1LoO0" + type: str + version_added: 7.5.0 base64: description: - Returns base64 encoded string. @@ -173,9 +186,17 @@ class LookupModule(LookupBase): length = self.get_option("length") base64_flag = self.get_option("base64") override_all = self.get_option("override_all") + ignore_similar_chars = self.get_option("ignore_similar_chars") + similar_chars = self.get_option("similar_chars") values = "" available_chars_set = "" + if ignore_similar_chars: + number_chars = "".join([sc for sc in number_chars if sc not in similar_chars]) + lower_chars = "".join([sc for sc in lower_chars if sc not in similar_chars]) + upper_chars = "".join([sc for sc in upper_chars if sc not in similar_chars]) + special_chars = "".join([sc for sc in special_chars if sc not in similar_chars]) + if override_all: # Override all the values available_chars_set = override_all