# random length password

**URL:** <https://forum.ansible.com/t/random-length-password/37898>\
**Category:** Ansible Project\
**Created:** [April 18, 2023, 2:39pm UTC](https://forum.ansible.com/t/random-length-password/37898 "2023-04-18T14:39:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kathy\_L](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/kathy_l/32/2652_2.png) [@Kathy\_L](https://forum.ansible.com/u/Kathy_L)\
**Post date:** [April 18, 2023, 2:39pm UTC](https://forum.ansible.com/t/random-length-password/37898/1 "2023-04-18T14:39:50Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Akasurde](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/akasurde/32/230_2.png) [@Akasurde](https://forum.ansible.com/u/Akasurde)\
**Post date:** [April 18, 2023, 4:29pm UTC](https://forum.ansible.com/t/random-length-password/37898/2 "2023-04-18T16:29:45Z")

</div>

Have you tried -

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

# Example result: [‘Uan0hUiX5kVG’]

---

<div class="post-metadata">

**Author:** ![Kathy\_L](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/kathy_l/32/2652_2.png) [@Kathy\_L](https://forum.ansible.com/u/Kathy_L)\
**Post date:** [April 18, 2023, 4:54pm UTC](https://forum.ansible.com/t/random-length-password/37898/3 "2023-04-18T16:54:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [April 18, 2023, 5:00pm UTC](https://forum.ansible.com/t/random-length-password/37898/4 "2023-04-18T17:00:33Z")

</div>

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.
