System Administration & Network Administration
ansible ansible-playbook syntax yaml
Updated Fri, 26 Aug 2022 10:31:46 GMT

Ansible Playbook and syntax issue with variables

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
      salt: "{{ lookup('password', '/dev/null length=10' chars=ascii_letters) }}"
      pasw: "{{ lookup('password', '/dev/null length=15') }}"
      hash: "{{ pasw }} | password_hash('sha512', {{ salt }}) "
      - 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: []: 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)  }}"}


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') }}"

Comments (1)

  • +0 – oh you'r perfectly right, that's so stupid of me thanks a lot for your assistance there — Nov 03, 2018 at 13:12