Ansible’s user module is a powerful tool for managing user accounts on remote systems. It allows you to create, update, and remove users, set passwords, manage groups, and configure SSH keys. This is essential in automating system administration tasks.
See the below basic example for creating a user named username with a hashed password, adding them to the wheel group, and generating an SSH key.
-name:Create a new useransible.builtin.user:name:"username"password:"{{'password'|password_hash('sha512')}}"groups:"wheel"shell:/bin/bashstate:presentcreate_home:truegenerate_ssh_key:true
See the below advanced example, including custom home, expiry, and comment
-name:Create a user with custom optionsansible.builtin.user:name:"devops"comment:"DevOpsEngineer"home:"/opt/devops"expires:1751328000# Unix timestamp for expirypassword:"{{'SuperSecret123'|password_hash('sha512')}}"groups:"sudo"shell:/bin/zshstate:presentcreate_home:true