Score:0

Ansible says Invalid options for debug: ansible.builtin.debug

in flag

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

Zeitounator avatar
fr flag
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 (`{{ ... }}`)
Score:0
in flag

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.

Zeitounator avatar
fr flag
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.
Score:0
in flag

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.

Invader Zim avatar
in flag
Thnx for your reponse. I noticed I forgot a line. ``` register: blowfish ``` should be there as well.
in flag
Added the line. The solution stays the same.
Invader Zim avatar
in flag
Thnx. Ansible doesn't throw an error now, so that is good. But the variable password now gets filled with "lookup('community.general.random_string', length=16)": "10caIPtm4)p4F}.+" instead of just the password.
in flag
That would be a new question.
Zeitounator avatar
fr flag
The correct syntax is `msg: "{{ lookup('community.general.random_string', length=32) }}"`. But this is stilll functionnaly not right IMO.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.