0

I've been quite busy building a script. At some point I need to generate a password and store it in a file. The password is generated with

  - name: Generate new password
    debug:
      ansible.builtin.debug:
        var: lookup('community.general.random_string', length=32)
      register: password

This worked better than expected, on all test runs it actually works. I added some more code to the playbook and now I keep getting

FAILED! => {"msg": "Invalid options for debug: ansible.builtin.debug"}

as an error message and I cannot figure out why this is now an issue.

Using CentOS.

/edited original posted code to include register: password

  • Registering the result of a debug task to later reuse its content is basically non-sense. Just declare the vars you need and use them. Moreover, the `var` option to `debug` is expecting the name of a variable, not a value to display. In this later case you have to use the `msg` option. Last, `lookup(...).` is a jinja2 expression and (except in specific options like `when`...) should be surrounded by jinja2 expansion markers (`{{ ... }}`) – Zeitounator Apr 23 '22 at 09:18

2 Answers2

0

The proper syntax is:

- name: Generate new password
  ansible.builtin.debug:
    var: lookup('community.general.random_string', length=32)
  register: password

Side note: You might want to check if set_fact is more suitable.

Gerald Schneider
  • 19,757
  • 8
  • 52
  • 79
0

This code works and does what I need:

  vars:
    password: "{{ lookup('password', '/dev/null length=16') }}"

  tasks:
  - debug:
      msg: '{{ password }}'

I can use variable password further on.

  • Note that, as is, the password will change each time you call the `password` variable. If you need to generate a password and keep it stable throughout your entire ansible play, have a look at the `set_fact` module. – Zeitounator Apr 23 '22 at 09:23