W3cubDocs

/Ansible

community.general.random_string – Generates random string

Note

This plugin is part of the community.general collection (version 3.8.1).

You might already have this collection installed if you are using the ansible package. It is not included in ansible-core. To check whether it is installed, run ansible-galaxy collection list.

To install it, use: ansible-galaxy collection install community.general.

To use it in a playbook, specify: community.general.random_string.

New in version 3.2.0: of community.general

Synopsis

  • Generates random string based upon the given constraints.

Parameters

Parameter Choices/Defaults Configuration Comments
base64
boolean
    Choices:
  • no
  • yes
Returns base64 encoded string.
length
integer
Default:
8
The length of the string.
lower
boolean
    Choices:
  • no
  • yes
Include lowercase letters in the string.
min_lower
integer
Default:
0
Minimum number of lowercase alphabets in the string.
If set, overrides lower=false.
min_numeric
integer
Default:
0
Minimum number of numeric characters in the string.
If set, overrides numbers=false.
min_special
integer
Default:
0
Minimum number of special character in the string.
min_upper
integer
Default:
0
Minimum number of uppercase alphabets in the string.
If set, overrides upper=false.
numbers
boolean
    Choices:
  • no
  • yes
Include numbers in the string.
override_all
string
Override all values of numbers, upper, lower, and special with the given list of characters.
override_special
string
Overide a list of special characters to use in the string.
If set min_special should be set to a non-default value.
special
boolean
    Choices:
  • no
  • yes
Include special characters in the string.
Special characters are taken from Python standard library string. See the documentation of string.punctuation for which characters will be used.
The choice of special characters can be changed to setting override_special.
upper
boolean
    Choices:
  • no
  • yes
Include uppercase letters in the string.

Examples

- name: Generate random string
  ansible.builtin.debug:
    var: lookup('community.general.random_string')
  # Example result: ['DeadBeeF']

- name: Generate random string with length 12
  ansible.builtin.debug:
    var: lookup('community.general.random_string', length=12)
  # Example result: ['Uan0hUiX5kVG']

- name: Generate base64 encoded random string
  ansible.builtin.debug:
    var: lookup('community.general.random_string', base64=True)
  # Example result: ['NHZ6eWN5Qk0=']

- name: Generate a random string with 1 lower, 1 upper, 1 number and 1 special char (atleast)
  ansible.builtin.debug:
    var: lookup('community.general.random_string', min_lower=1, min_upper=1, min_special=1, min_numeric=1)
  # 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)
  # 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'
  # 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'
  # Example result: ['D2A40737']

Return Values

Common return values are documented here, the following are the fields unique to this lookup:

Key Returned Description
_raw
list / elements=string
success
A one-element list containing a random string



Authors

  • Abhijeet Kasurde (@Akasurde)

© 2012–2018 Michael DeHaan
© 2018–2021 Red Hat, Inc.
Licensed under the GNU General Public License version 3.
https://docs.ansible.com/ansible/latest/collections/community/general/random_string_lookup.html