community.general.random_string (8.5.0) — lookup

Generates random string

| "added in version" 3.2.0 of community.general"

Authors: Abhijeet Kasurde (@Akasurde)

Install collection

Install with ansible-galaxy collection install community.general:==8.5.0


Add to requirements.yml

  collections:
    - name: community.general
      version: 8.5.0

Description

Generates random string based upon the given constraints.

Uses L(random.SystemRandom,https://docs.python.org/3/library/random.html#random.SystemRandom), so should be strong enough for cryptographic purposes.

Usage examples

  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Generate random string
  ansible.builtin.debug:
    var: lookup('community.general.random_string')
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
  # Example result: ['DeadBeeF']

- name: Generate random string with length 12
  ansible.builtin.debug:
    var: lookup('community.general.random_string', length=12)
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
  # Example result: ['Uan0hUiX5kVG']

- name: Generate base64 encoded random string
  ansible.builtin.debug:
    var: lookup('community.general.random_string', base64=True)
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
  # Example result: ['NHZ6eWN5Qk0=']

- name: Generate a random string with 1 lower, 1 upper, 1 number and 1 special char (at least)
  ansible.builtin.debug:
    var: lookup('community.general.random_string', min_lower=1, min_upper=1, min_special=1, min_numeric=1)
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
  # Example result: ['&Qw2|E[-']

- name: Generate a random string with all lower case characters
  debug:
    var: query('community.general.random_string', upper=false, numbers=false, special=false)
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
  # Example result: ['exolxzyz']

- name: Generate random hexadecimal string
  debug:
    var: query('community.general.random_string', upper=false, lower=false, override_special=hex_chars, numbers=false)
  vars:
    hex_chars: '0123456789ABCDEF'
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
  # Example result: ['D2A40737']

- name: Generate random hexadecimal string with override_all
  debug:
    var: query('community.general.random_string', override_all=hex_chars)
  vars:
    hex_chars: '0123456789ABCDEF'

Inputs

    
lower:
    default: true
    description:
    - Include lowercase letters in the string.
    type: bool

upper:
    default: true
    description:
    - Include uppercase letters in the string.
    type: bool

base64:
    default: false
    description:
    - Returns base64 encoded string.
    type: bool

length:
    default: 8
    description: The length of the string.
    type: int

numbers:
    default: true
    description:
    - Include numbers in the string.
    type: bool

special:
    default: true
    description:
    - Include special characters in the string.
    - Special characters are taken from Python standard library C(string). See L(the documentation
      of string.punctuation,https://docs.python.org/3/library/string.html#string.punctuation)
      for which characters will be used.
    - The choice of special characters can be changed to setting O(override_special).
    type: bool

min_lower:
    default: 0
    description:
    - Minimum number of lowercase alphabets in the string.
    - If set, overrides O(lower=false).
    type: int

min_upper:
    default: 0
    description:
    - Minimum number of uppercase alphabets in the string.
    - If set, overrides O(upper=false).
    type: int

min_numeric:
    default: 0
    description:
    - Minimum number of numeric characters in the string.
    - If set, overrides O(numbers=false).
    type: int

min_special:
    default: 0
    description:
    - Minimum number of special character in the string.
    type: int

override_all:
    description:
    - Override all values of O(numbers), O(upper), O(lower), and O(special) with the given
      list of characters.
    type: str

similar_chars:
    default: il1LoO0
    description:
    - Override a list of characters not to be use in the string.
    type: str
    version_added: 7.5.0
    version_added_collection: community.general

override_special:
    description:
    - Override a list of special characters to use in the string.
    - If set O(min_special) should be set to a non-default value.
    type: str

ignore_similar_chars:
    default: false
    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).
    type: bool
    version_added: 7.5.0
    version_added_collection: community.general

Outputs

_raw:
  description: A one-element list containing a random string
  elements: str
  type: list