random length password

62 views
Skip to first unread message

Kathy L

unread,
Apr 18, 2023, 10:39:50 AM4/18/23
to Ansible Project
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?

Abhijeet Kasurde

unread,
Apr 18, 2023, 12:30:19 PM4/18/23
to ansible...@googlegroups.com
Have you tried -

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


 # Example result: ['Uan0hUiX5kVG']

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/51262064-7d5e-493a-a615-71cd3a0362e6n%40googlegroups.com.


--
Thanks,
Abhijeet Kasurde

Kathy Lyons

unread,
Apr 18, 2023, 12:54:54 PM4/18/23
to ansible...@googlegroups.com
I looked at that, but we want to vary the length of the password each time this is run.

You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/jcj1d_cfNEc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CAFwWkHrR1Lf4K0MeEE807_me3758YMYmB7OoQ%2B9eeU2DCAgX1w%40mail.gmail.com.

Matt Martz

unread,
Apr 18, 2023, 1:01:22 PM4/18/23
to ansible...@googlegroups.com
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.



--
Matt Martz
@sivel
sivel.net
Reply all
Reply to author
Forward
0 new messages