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.
Change the User Primary Group on Linux with Ansible — Video Tutorial
Learn how to use Ansible to change a user's primary group on Linux systems with the user module.
What You'll Learn
- How to Change the User Primary Group on Linux with Ansible?
- Ansible changes the User Primary Group on Linux
- Parameters
- Links
- code
- execution
- before execution
- after execution
- Conclusion
- Related Articles
Full Tutorial Content
How to Change the User Primary Group on Linux with Ansible?
I'm going to show you a live Playbook 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
> `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.
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 Conclusion is you specify the "no" option you are going to lose all the previous group associations, please be careful!
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
```yaml
---
- 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
```bash
$ 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
```bash
$ ssh devops@demo.example.com
Last login: Wed Dec 15 13:00:47
About This Tutorial
- Author: Luca Berton
- Difficulty: Beginner
- Read time: 3 min
- Category: troubleshooting
Read the full written article: Change the User Primary Group on Linux with Ansible