random length password

Currently we are using pwgen to generate our random length passwords, but we would like to use an Ansible module to make things easier. What I have is not working -

set_fact:
user_password: “{{ lookup(‘ansible.builtin.password’, chars=[‘ascii_letters’, ‘digits’], length = {{ range(8-12)|random }} ) }}”

Of course it does not like a variable within a variable. Is there another way to do this with an Ansible module that I am missing?

Have you tried -

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

Example result: [‘Uan0hUiX5kVG’]

I looked at that, but we want to vary the length of the password each time this is run.

I believe this is roughly what you want:

{{ lookup(‘ansible.builtin.password’, ‘/dev/null’, chars=[‘ascii_letters’, ‘digits’], length=range(8, 12)|random) }}

  1. You cannot nest {{ }}
  2. Instead of range(8-12) I think you want range(8, 13). First you want a comma, and then range is non-inclusive of the last number, so you have to add 1.
  3. The password lookup requires a file to write to, even if that is just /dev/null (a normal file is how it has historically achieved idempotency.