Change the User Primary Group on Linux with Ansible
By Luca Berton · Published 2024-01-01 · Category: troubleshooting
Learn how to use Ansible to change a user's primary group on Linux systems with the user module.

How to Change the User Primary Group on Linux with Ansible?
See also: Add Secondary Groups to Linux Users with Ansible Playbook
Ansible changes the User Primary Group on Linux
> ansible.builtin.user Manage user accounts
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 ansible.windows.win_user module instead.
Parameters
- name _string_ - username
- group _string_ - user's primary group (only one)
- groups _list / elements=string_ - list of groups the user will be added to
- append _boolean_ - no/yes - If yes, add the user to the groups specified in groups. If no, replace.
See also: Ansible Linux Users and Groups: Complete Management Guide (Examples)
Links
- https://docs.ansible.com/ansible/latest/collections/ansible/builtin/user_module.html
## Playbook
How to change the User Primary Group on Linux with Ansible Playbook.
code
- user_group_changeprimary.yml
---
- name: user module Playbook
hosts: all
become: true
vars:
myuser: "example"
mygroup: "users"
tasks:
- name: change primary group
ansible.builtin.user:
name: "{{ myuser }}"
group: "{{ mygroup }}"
execution
$ ansible-playbook -i virtualmachines/demo/inventory users_and_groups/user_group_changeprimary.yml
PLAY [user module Playbook] ***************************************************************************
TASK [Gathering Facts] ****************************************************************************
ok: [demo.example.com]
TASK [change primare group] ***********************************************************************
changed: [demo.example.com]
PLAY RECAP ****************************************************************************************
demo.example.com : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
ansible-pilot $
before execution
$ ssh devops@demo.example.com
Last login: Wed Dec 15 13:00:47 2021 from 192.168.0.101
[devops@demo ~]$ sudo su
[root@demo devops]# cat /etc/os-release
NAME="Red Hat Enterprise Linux"
VERSION="8.5 (Ootpa)"
ID="rhel"
ID_LIKE="fedora"
VERSION_ID="8.5"
PLATFORM_ID="platform:el8"
PRETTY_NAME="Red Hat Enterprise Linux 8.5 (Ootpa)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:redhat:enterprise_linux:8::baseos"
HOME_URL="https://www.redhat.com/"
DOCUMENTATION_URL="https://access.redhat.com/documentation/red_hat_enterprise_linux/8/"
BUG_REPORT_URL="https://bugzilla.redhat.com/"
REDHAT_BUGZILLA_PRODUCT="Red Hat Enterprise Linux 8"
REDHAT_BUGZILLA_PRODUCT_VERSION=8.5
REDHAT_SUPPORT_PRODUCT="Red Hat Enterprise Linux"
REDHAT_SUPPORT_PRODUCT_VERSION="8.5"
[root@demo devops]# getent passwd | grep example
example:x:1002:1002:Ansible example:/home/example:/bin/bash
[root@demo devops]# id example
uid=1002(example) gid=1002(example) groups=1002(example),10(wheel)
[root@demo devops]# groups example
example : example wheel
[root@demo devops]#after execution
$ ssh devops@demo.example.com
[devops@demo ~]$ sudo su
[root@demo devops]# getent passwd | grep example
example:x:1002:100:Ansible example:/home/example:/bin/bash
[root@demo devops]# id example
uid=1002(example) gid=100(users) groups=100(users),10(wheel)
[root@demo devops]# groups example
example : users wheel
[root@demo devops]#
Conclusion
Now you know how to change the User Primary Group on Linux with Ansible.
See also: ansible.builtin.user: Change User Password with Ansible (Secure Guide)
Related Articles
Category: troubleshooting
Watch the video: Change the User Primary Group on Linux with Ansible — Video Tutorial