Introduction
Managing users and groups across dozens or hundreds of Linux servers manually is error-prone and time-consuming. Ansible's user and group modules automate the entire lifecycle — creating accounts, managing group memberships, setting passwords, distributing SSH keys, and enforcing policies consistently across your fleet.
Core Modules
| Module | Purpose |
|---|---|
ansible.builtin.user | Manage user accounts |
ansible.builtin.group | Manage groups |
ansible.posix.authorized_key | Manage SSH authorized keys |
ansible.builtin.lineinfile | Edit sudoers and config files |
Quick Examples
Create a User
- name: Create developer user
ansible.builtin.user:
name: developer
comment: "Developer Account"
groups: docker,sudo
append: true
shell: /bin/bash
state: present
Create a Group
- name: Create application group
ansible.builtin.group:
name: appteam
gid: 2000
state: present
Set Password
- name: Set user password
ansible.builtin.user:
name: developer
password: "{{ 'MySecurePass' | password_hash('sha512') }}"
update_password: on_create # Only set on initial creation
Add SSH Key
- name: Add authorized SSH key
ansible.posix.authorized_key:
user: developer
key: "{{ lookup('file', 'files/developer_id_rsa.pub') }}"
state: present
Complete User Provisioning Playbook
---
- name: Provision team users
hosts: all
become: true
vars:
team_users:
- name: alice
groups: [sudo, docker, appteam]
shell: /bin/bash
ssh_key: "ssh-rsa AAAA... alice@company.com"
- name: bob
groups: [docker, appteam]
shell: /bin/bash
ssh_key: "ssh-rsa AAAA... bob@company.com"
- name: charlie
groups: [appteam]
shell: /bin/bash
ssh_key: "ssh-rsa AAAA... charlie@company.com"
tasks:
- name: Create application group
ansible.builtin.group:
name: appteam
state: present
- name: Create user accounts
ansible.builtin.user:
name: "{{ item.name }}"
groups: "{{ item.groups }}"
append: true
shell: "{{ item.shell }}"
create_home: true
state: present
loop: "{{ team_users }}"
- name: Deploy SSH keys
ansible.posix.authorized_key:
user: "{{ item.name }}"
key: "{{ item.ssh_key }}"
exclusive: true
loop: "{{ team_users }}"
- name: Set password expiration policy
ansible.builtin.user:
name: "{{ item.name }}"
password_expire_max: 90
password_expire_min: 7
loop: "{{ team_users }}"
Tutorials
User Management
- Create User Account
- Add User to Secondary Group
- Change User Primary Group
- Change User Password
- User Password Expiration
- Enable User Account
- Disable User Account
- Remove User Account
Group Management
PostgreSQL Users
Windows User Management
- Create Local User on Windows
- Create Local Group on Windows
- Change Windows User Password
- Remove Windows User
- Remove Windows Group
AWX User Management
Troubleshooting
User Module Parameters Reference
| Parameter | Type | Description |
|---|---|---|
name | string | Username (required) |
state | string | present or absent |
uid | int | User ID |
group | string | Primary group |
groups | list | Secondary groups |
append | bool | Append to groups (don't replace) |
shell | string | Login shell |
home | string | Home directory path |
create_home | bool | Create home directory |
comment | string | GECOS comment field |
password | string | Hashed password |
update_password | string | always or on_create |
system | bool | Create system account |
expires | float | Account expiry (epoch) |
generate_ssh_key | bool | Generate SSH key pair |
ssh_key_bits | int | SSH key bits (default: 4096) |
ssh_key_type | string | Key type (rsa, ed25519, etc.) |
remove | bool | Remove home dir on absent |
force | bool | Force removal even if logged in |
password_expire_max | int | Max days between password changes |
password_expire_min | int | Min days between password changes |
Offboarding Users
- name: Offboard departed employees
hosts: all
become: true
vars:
departed_users:
- alice
- bob
tasks:
- name: Lock accounts
ansible.builtin.user:
name: "{{ item }}"
password_lock: true
loop: "{{ departed_users }}"
- name: Remove cron jobs
ansible.builtin.cron:
name: "*"
user: "{{ item }}"
state: absent
loop: "{{ departed_users }}"
ignore_errors: true
- name: Remove user accounts and home directories
ansible.builtin.user:
name: "{{ item }}"
state: absent
remove: true
force: true
loop: "{{ departed_users }}"
Related Articles
- Ansible Privilege Escalation
- Ansible Best Practices Guide
- Ansible for Windows
- Ansible Vault: Encrypt Sensitive Data
Conclusion
Ansible makes Linux user management scalable and consistent — from creating accounts with proper group memberships and SSH keys to enforcing password policies and offboarding departed employees. Use the user module for accounts, group for groups, and authorized_key for SSH access. Store passwords in Ansible Vault, use update_password: on_create to avoid resetting passwords on every run, and always use append: true when adding secondary groups to avoid removing existing memberships.