Ansible Playbook and syntax issue with variables

Nicolas asked:

I’m trying to execute the below playbook I wrote but it fails on some syntax errors.

I tried few things from the online doc but without sucess so far. The aim of these variable is to generate a random password to be used by the user module in a role.

 - hosts: all
   remote_user: root
   become : no
   vars:
      salt: "{{ lookup('password', '/dev/null length=10' chars=ascii_letters) }}"
      pasw: "{{ lookup('password', '/dev/null length=15') }}"
      hash: "{{ pasw }} | password_hash('sha512', {{ salt }}) "
   roles:
      - ansiuser

the error from ansible-playbook execution (it fails at role execution so I guess my variables aren’t doing what I’m looking for: generating strings)

    TASK [ansiuser : Set the password fact for this host] ******************************************************************************************************************
    fatal: [192.168.11.100]: FAILED! => {"msg": "An unhandled exception occurred while templating '{{ pasw }} | password_hash('sha512', {{ salt }}) '. 
Error was a <class 'ansible.errors.AnsibleError'>, original message: An unhandled exception occurred while templating '{{ lookup('password', '/dev/null length=10' chars=ascii_letters)  }}'. 

Error was a <class 'ansible.errors.AnsibleError'>, original message: template error while templating string: expected token ',', got 'chars'. String: {{ lookup('password', '/dev/null length=10' chars=ascii_letters)  }}"}

My answer:


Yes, it’s a simple typo.

      salt: "{{ lookup('password', '/dev/null length=10' chars=ascii_letters) }}"

You can see that the error complained about the literal text chars appearing unexpectedly. That’s because you closed the quote early, where it was supposed to contain all of the options, i.e.:

      salt: "{{ lookup('password', '/dev/null length=10 chars=ascii_letters') }}"

View the full question and any other answers on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.