AnsiblePilot — Master Ansible Automation

AnsiblePilot is the leading resource for learning Ansible automation, DevOps, and infrastructure as code. Browse over 1,400 tutorials covering Ansible modules, playbooks, roles, collections, and real-world examples. Whether you are a beginner or an experienced engineer, our step-by-step guides help you automate Linux, Windows, cloud, containers, and network infrastructure.

Popular Topics

About Luca Berton

Luca Berton is an Ansible automation expert, author of 8 Ansible books published by Apress and Leanpub including "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example", and creator of the Ansible Pilot YouTube channel. He shares practical automation knowledge through tutorials, books, and video courses to help IT professionals and DevOps engineers master infrastructure automation.

Ansible Linux Users and Groups: Complete Management Guide (Examples)

By Luca Berton · Published 2024-01-01 · Category: troubleshooting

Complete guide to managing Linux users and groups with Ansible. Create users, set passwords, manage groups, SSH keys, home directories, and sudo access.

Ansible Linux Users and Groups: Complete Management Guide (Examples)

Automating your Linux user management (local user and group) with Ansible enables you to achieve Infrastructure As Code (IaC). Using IaC you're going to be able to automate your workflow, your CI/CD pipelines for example, and be faster about your critical business demands.

Beginners

Ansible provides various modules to manage user management (local user and group). I'll show you step by step how to prepare your Ansible controller to interact with the Linux user and group directory. This initial configuration sometimes is a roadblock for some Docker users to start using Ansible.

See also: Add Secondary Groups to Linux Users with Ansible Playbook

User management

Create user account - Ansible module userAdd a User to a Second Group on Linux - Ansible module user " >}}) • Change the User Primary Group on Linux - Ansible module userChange user password - Ansible module user.mdUser password expiration - Ansible module userEnable user account - Ansible module user.mdDisable user account - Ansible module userRemove user account - Ansible module user

Group management

Create a group - Ansible module groupDelete a group - Ansible module group

See also: Change the User Primary Group on Linux with Ansible

PostgreSQL

Allow md5 Connection for a PostgreSQL User or Role - Ansible module postgresql_pg_hbaCreate a PostgreSQL User or Role - Ansible module postgresql_userGrant Privileges to User or Role on PostgreSQL Database - Ansible module postgresql_privs

Windows

Change local user password on Windows-like systems - Ansible module win_user.mdCreate a local group on Windows-like systems - Ansible module win_group.mdCreate a local user on Windows-like systems - Ansible module win_userRemove a local group on Windows-like systems - Ansible module win_group.mdRemove a local user on Windows-like systems - Ansible module win_user.md

See also: ansible.builtin.user: Change User Password with Ansible (Secure Guide)

AWX

Create Ansible AWX superuser in Docker containers - Ansible AWX.md

Troubleshooting

Ansible troubleshooting - This command has to be run under the root userAnsible troubleshooting - Unhandled exception while executing module win_userAnsible troubleshooting - user module bug.md

{{< promote-video-book-containers >}}

Conclusion

Now you know how you could automate your Linux user management (local user and group) using Ansible Automation technology.

Create User with All Options

- name: Create application user
  ansible.builtin.user:
    name: appuser
    comment: "Application Service Account"
    uid: 1500
    group: appgroup
    groups: docker,sudo
    append: true
    shell: /bin/bash
    home: /home/appuser
    create_home: true
    password: "{{ 'SecurePass' | password_hash('sha512') }}"
    generate_ssh_key: true
    ssh_key_bits: 4096
  become: true

Bulk User Management

- vars:
    users:
      - { name: alice, groups: "developers,docker", key: "ssh-ed25519 AAAA..." }
      - { name: bob, groups: "developers", key: "ssh-ed25519 BBBB..." }
      - { name: charlie, groups: "operations,docker", key: "ssh-ed25519 CCCC..." }

block: - name: Create users ansible.builtin.user: name: "{{ item.name }}" groups: "{{ item.groups }}" append: true shell: /bin/bash state: present loop: "{{ users }}"

- name: Deploy SSH keys ansible.posix.authorized_key: user: "{{ item.name }}" key: "{{ item.key }}" loop: "{{ users }}" become: true

Sudo Access

- name: Grant sudo access
  ansible.builtin.copy:
    content: "{{ item }} ALL=(ALL) NOPASSWD: ALL"
    dest: "/etc/sudoers.d/{{ item }}"
    mode: '0440'
    validate: 'visudo -cf %s'
  loop:
    - alice
    - bob
  become: true

Remove Users

- name: Remove departed users
  ansible.builtin.user:
    name: "{{ item }}"
    state: absent
    remove: true  # Remove home directory
  loop:
    - old_contractor
    - temp_user
  become: true

Password Expiry Policy

- name: Set password policy
  ansible.builtin.user:
    name: "{{ item }}"
    password_expire_max: 90
    password_expire_min: 7
  loop: "{{ all_users }}"
  become: true

System Accounts (No Login)

- name: Create service account
  ansible.builtin.user:
    name: prometheus
    system: true
    shell: /usr/sbin/nologin
    home: /opt/prometheus
    create_home: false
  become: true

Complete Role Pattern

# roles/users/tasks/main.yml
- name: Create groups
  ansible.builtin.group:
    name: "{{ item.name }}"
    gid: "{{ item.gid | default(omit) }}"
  loop: "{{ user_groups }}"

- name: Create users ansible.builtin.user: name: "{{ item.name }}" groups: "{{ item.groups | default(omit) }}" shell: "{{ item.shell | default('/bin/bash') }}" append: true loop: "{{ managed_users }}"

- name: Set authorized keys ansible.posix.authorized_key: user: "{{ item.name }}" key: "{{ item.ssh_key }}" exclusive: true loop: "{{ managed_users }}" when: item.ssh_key is defined

FAQ

How do I lock/unlock an account?

# Lock
- command: usermod -L username
# Unlock
- command: usermod -U username

How do I check if a user exists?

- getent:
    database: passwd
    key: username
  register: user_check
  failed_when: false

What's the difference between group and groups?

group: Primary group (one only) • groups: Supplementary groups (comma-separated list) • append: true: Add to groups without removing from existing ones

Related Articles

orchestrating containers via AnsibleAnsible AWX use casesAnsible Windows administration walkthrough

Category: troubleshooting

Browse all Ansible tutorials · AnsiblePilot Home