Skip to content

Commit

Permalink
Ignore similar chars in random_string (#7242)
Browse files Browse the repository at this point in the history
* 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 <felix@fontein.de>

* Update plugins/lookup/random_string.py

Co-authored-by: Felix Fontein <felix@fontein.de>

* Update plugins/lookup/random_string.py

Co-authored-by: Felix Fontein <felix@fontein.de>

---------

Co-authored-by: Felix Fontein <felix@fontein.de>
(cherry picked from commit c3fd14e)
  • Loading branch information
gbyx3 authored and patchback[bot] committed Sep 20, 2023
1 parent 24b6441 commit 95ee217
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/7242_ignore_similar_chars.yml
Original file line number Diff line number Diff line change
@@ -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).
21 changes: 21 additions & 0 deletions plugins/lookup/random_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,19 @@
- 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.
Expand Down Expand Up @@ -173,9 +186,17 @@ def run(self, terms, variables=None, **kwargs):
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
Expand Down

0 comments on commit 95ee217

Please sign in to comment.