Interview Prep/Ansible/Ad-hoc Commands, Modules and Idempotency

How do you create a user with a hashed password using the `user` module?

IntermediateConceptAd-hoc Commands, Modules and Idempotency
Answer

The password parameter needs a hash, never plain text. Generate it with the password_hash filter.

- ansible.builtin.user:
    name: deploy
    shell: /bin/bash
    groups: [sudo]
    append: true
    password: "{{ vault_deploy_password | password_hash('sha512', 'fixedsalt') }}"
    update_password: on_create

Without a fixed salt a new random hash is made each run and the task reports changed every time. update_password: on_create also avoids resetting passwords users changed. For SSH access, prefer ansible.posix.authorized_key.

All Ansible questions Roadmap: Ansible

More on Ad-hoc Commands, Modules and Idempotency