Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate single reusable random password with ansible

Tags:

ansible

That is to say: How to evaluate the password lookup only once?

- name: Demo
  hosts: localhost
  gather_facts: False
  vars:
    my_pass: "{{ lookup('password', '/dev/null length=15 chars=ascii_letters') }}"
  tasks:
  - debug:
      msg: "{{ my_pass }}"
  - debug:
      msg: "{{ my_pass }}"
  - debug:
      msg: "{{ my_pass }}"

each debug statement will print out a different value, e.g:

PLAY [Demo] *************

TASK [debug] ************
ok: [localhost] => {
    "msg": "ZfyzacMsqZaYqwW"
}

TASK [debug] ************
ok: [localhost] => {
    "msg": "mKcfRedImqxgXnE"
}

TASK [debug] ************
ok: [localhost] => {
    "msg": "POpqMQoJWTiDpEW"
}

PLAY RECAP ************
localhost                  : ok=3    changed=0    unreachable=0    failed=0   

ansible 2.3.2.0

like image 489
Kuba Avatar asked Oct 13 '17 14:10

Kuba


People also ask

How do I make an Ansible random password?

Usage of variables like "{{ inventory_hostname }}" in the filepath can be used to set up random passwords per host, which simplifies password management in "host_vars" variables. A special case is using /dev/null as a path.

How do you create an encrypted password user module in Ansible?

You can use ansible-vault for using secret keys in playbooks. Define your password in yml. And then you can use your variables where you want. No one can read them without vault-key.

How do I bypass Ansible playbook password?

If you used default options and the prompt password source when encrypting the data used in this playbook, you can use the option --ask-vault-pass to make Ansible prompt you for the password: ansible-playbook myplaybook. yml --ask-vault-pass.


2 Answers

Use set_fact to assign permanent fact:

- name: Demo
  hosts: localhost
  gather_facts: False
  vars:
    pwd_alias: "{{ lookup('password', '/dev/null length=15 chars=ascii_letters') }}"
  tasks:
    - set_fact:
        my_pass: "{{ pwd_alias }}"
    - debug:
        msg: "{{ my_pass }}"
    - debug:
        msg: "{{ my_pass }}"
    - debug:
        msg: "{{ my_pass }}"
like image 190
Konstantin Suvorov Avatar answered Oct 14 '22 08:10

Konstantin Suvorov


I've been doing it this way and never had an issue.

- name: Demo
  hosts: localhost
  gather_facts: False 

  tasks:
   - set_fact:
       my_pass: "{{ lookup('password', '/dev/null length=15 chars=ascii_letters') }}"
   - debug:
       msg: "{{ my_pass }}"
like image 41
D.Fitz Avatar answered Oct 14 '22 08:10

D.Fitz