Ansible Pilot

Change the User Primary Group on Linux - Ansible module user

How to automate the changing of the primary group of a user "example" from "example" group to "users" group on a target the Linux with few lines of Ansible playbook code.

December 15, 2021
Access the Complete Video Course and Learn Quick Ansible by 200+ Practical Lessons

How to Change the User Primary Group on Linux with Ansible?

I’m going to show you a live demo with some simple Ansible code. I’m Luca Berton and welcome to today’s episode of Ansible Pilot.

Ansible changes the User Primary Group on Linux

Today we’re talking about Ansible module user. The full name is ansible.builtin.user, which means that is part of the collection of modules “builtin” with ansible and shipped with it. It’s a module pretty stable and out for years, it manages user accounts. It supports a huge variety of Linux distributions, SunOS and macOS, and FreeBSD. For Windows, use the module instead.


This module has many parameters, let me highlight the useful for our use case. The only required is “name”, which is the username. The primary group is specified in the “group” parameter, every user need to be part of only one group. The “groups” parameter specifies the list of additional groups that the user will be added to. This type of group sometimes is called also “secondary”, “additional” or “supplementary”. The parameter “append” is very important. With the “yes” option, the user is going to be added to the specified groups. With the “no” option, all group members are going to be overwritten with the specified groups. So to recap is you specify the “no” option you are going to lose all the previous group associations, please be careful!

The Best Resources For Ansible

Video Course

Printed Book



How to change the User Primary Group on Linux with Ansible Playbook.


- name: user module demo
  hosts: all
  become: true
    myuser: "example"
    mygroup: "users"
    - name: change primary group
        name: "{{ myuser }}"
        group: "{{ mygroup }}"


$ ansible-playbook -i virtualmachines/demo/inventory users_and_groups/user_group_changeprimary.yml
PLAY [user module demo] ***************************************************************************
TASK [Gathering Facts] ****************************************************************************
ok: []
TASK [change primare group] ***********************************************************************
changed: []
PLAY RECAP ****************************************************************************************           : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
ansible-pilot $

before execution

$ ssh [email protected]
Last login: Wed Dec 15 13:00:47 2021 from
[[email protected] ~]$ sudo su
[[email protected] devops]# cat /etc/os-release 
NAME="Red Hat Enterprise Linux"
VERSION="8.5 (Ootpa)"
PRETTY_NAME="Red Hat Enterprise Linux 8.5 (Ootpa)"
REDHAT_BUGZILLA_PRODUCT="Red Hat Enterprise Linux 8"
REDHAT_SUPPORT_PRODUCT="Red Hat Enterprise Linux"
[[email protected] devops]# getent passwd | grep example
example:x:1002:1002:Ansible example:/home/example:/bin/bash
[[email protected] devops]# id example
uid=1002(example) gid=1002(example) groups=1002(example),10(wheel)
[[email protected] devops]# groups example
example : example wheel
[[email protected] devops]#

after execution

$ ssh [email protected]
[[email protected] ~]$ sudo su
[[email protected] devops]# getent passwd | grep example
example:x:1002:100:Ansible example:/home/example:/bin/bash
[[email protected] devops]# id example
uid=1002(example) gid=100(users) groups=100(users),10(wheel)
[[email protected] devops]# groups example
example : users wheel
[[email protected] devops]#

code with ❤️ in GitHub


Now you know how to change the User Primary Group on Linux with Ansible. Subscribe to the YouTube channel, Medium, Website, Twitter, and Substack to not miss the next episode of the Ansible Pilot.


Learn the Ansible automation technology with some real-life examples in my

My book Ansible By Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps

BUY the Complete PDF BOOK to easily Copy and Paste the 250+ Ansible code

Want to keep this project going? Please donate

Access the Complete Video Course and Learn Quick Ansible by 200+ Practical Lessons
Follow me

Subscribe not to miss any new releases

FREE Top 10 Best Practices

Top 10 Best Practices of Ansible Automation: save time, reduce errors and stress