ansible-collections / community.general

Ansible Community General Collection
https://galaxy.ansible.com/ui/repo/published/community/general/
GNU General Public License v3.0
833 stars 1.53k forks source link

[PR #8827/afd754e3 backport][stable-9] (doc) random_string lookup: fix examples #8842

Closed patchback[bot] closed 2 months ago

patchback[bot] commented 2 months ago

This is a backport of PR #8827 as merged into main (afd754e384118768c03f0acee3849900827464cc).

SUMMARY

The doc for random_string lookup include examples with lookup and query. While lookup returns a string and query returns an array of one element, the examples say that both calls return an array.

ISSUE TYPE
COMPONENT NAME

random_string lookup

ADDITIONAL INFORMATION

I've also replaced debug calls by ansible.builtin.debug (in this example, both forms where used).

Sample playbook file :

- hosts: localhost
  gather_facts: no
  tasks:
  - name: Generate random string
    ansible.builtin.debug:
      var: lookup('community.general.random_string')

  - name: Generate random string with length 12
    ansible.builtin.debug:
      var: lookup('community.general.random_string', length=12)

  - name: Generate base64 encoded random string
    ansible.builtin.debug:
      var: lookup('community.general.random_string', base64=True)

  - 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)

  - name: Generate a random string with all lower case characters
    debug:
      var: query('community.general.random_string', upper=false, numbers=false, special=false)

  - 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'

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

Results I got :

PLAY [localhost] ***********************************************************************************************************************************

TASK [Generate random string] **********************************************************************************************************************
ok: [localhost] => {
    "lookup('community.general.random_string')": "zfb^GVrD"
}

TASK [Generate random string with length 12] *******************************************************************************************************
ok: [localhost] => {
    "lookup('community.general.random_string', length=12)": "Znks;pp-lQWd"
}

TASK [Generate base64 encoded random string] *******************************************************************************************************
ok: [localhost] => {
    "lookup('community.general.random_string', base64=True)": "XXRKR3NZcDk="
}

TASK [Generate a random string with 1 lower, 1 upper, 1 number and 1 special char (at least)] ******************************************************
ok: [localhost] => {
    "lookup('community.general.random_string', min_lower=1, min_upper=1, min_special=1, min_numeric=1)": "#9qv;JoB"
}

TASK [Generate a random string with all lower case characters] *************************************************************************************
ok: [localhost] => {
    "query('community.general.random_string', upper=false, numbers=false, special=false)": [
        "jkwdqnuu"
    ]
}

TASK [Generate random hexadecimal string] **********************************************************************************************************
ok: [localhost] => {
    "query('community.general.random_string', upper=false, lower=false, override_special=hex_chars, numbers=false)": [
        "759BBEC6"
    ]
}

TASK [Generate random hexadecimal string with override_all] ****************************************************************************************
ok: [localhost] => {
    "query('community.general.random_string', override_all=hex_chars)": [
        "B7768D21"
    ]
}

PLAY RECAP *****************************************************************************************************************************************
localhost                  : ok=7    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0