From 61e82c50e49efcbecf11a5eea7180a9e58dd4bbc Mon Sep 17 00:00:00 2001 From: "patchback[bot]" <45432694+patchback[bot]@users.noreply.github.com> Date: Wed, 20 Sep 2023 22:04:29 +0200 Subject: [PATCH] [PR #7242/c3fd14e1 backport][stable-7] Ignore similar chars in random_string (#7292) Ignore similar chars in random_string (#7242) * Added the option to ignore certain characters This can be usefull for eliminating confusion. * Removed the loop and added each char_sets The variable name is not known inside the loop so updating it does not work. * Changelog fragment file * Forgot the file extention for the fragment yaml file * Update plugins/lookup/random_string.py Co-authored-by: Felix Fontein * Update plugins/lookup/random_string.py Co-authored-by: Felix Fontein * Update plugins/lookup/random_string.py Co-authored-by: Felix Fontein --------- Co-authored-by: Felix Fontein (cherry picked from commit c3fd14e18f7ced21df42abc4e9f310f070ad7a55) Co-authored-by: Gustav Alerby --- .../fragments/7242_ignore_similar_chars.yml | 2 ++ plugins/lookup/random_string.py | 21 +++++++++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 changelogs/fragments/7242_ignore_similar_chars.yml 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