Add a User to a Second Group on Linux - Ansible module user
How to automate the adding of a user "example" to a system or user-defined groups list on a target the Linux with few lines of Ansible playbook code.


How to add a user to a second 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 adds a user to second a group
ansible.builtin.user
- Manage user accounts
Today we’re talking about the 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 - user’s primary group (only one)
- groups list / elements=string - list of groups 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 use for our use-case. The only required is “name”, which is the username. The primary group is specified in the “group” parameter, every user needs 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!
Links
The Best Resources For Ansible
Video Course
Printed Book
eBooks
- Ansible by Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps
- Ansible For Windows By Examples: 50+ Automation Examples For Windows System Administrator And DevOps
- Ansible For Linux by Examples: 100+ Automation Examples For Linux System Administrator and DevOps
- Ansible Linux Filesystem By Examples: 40+ Automation Examples on Linux File and Directory Operation for Modern IT Infrastructure
- Ansible For Containers and Kubernetes By Examples: 20+ Automation Examples To Automate Containers, Kubernetes and OpenShift
- Ansible For Security by Examples: 100+ Automation Examples to Automate Security and Verify Compliance for IT Modern Infrastructure
- Ansible Tips and Tricks: 10+ Ansible Examples to Save Time and Automate More Tasks
- Ansible Linux Users & Groups By Examples: 20+ Automation Examples on Linux Users and Groups Operation for Modern IT Infrastructure
- Ansible For PostgreSQL by Examples: 10+ Examples To Automate Your PostgreSQL database
- Ansible For Amazon Web Services AWS By Examples: 10+ Examples To Automate Your AWS Modern Infrastructure
demo
Add a user to second a group with Ansible Playbook.
code
- user_group_addsecondary.yml
---
- name: user module demo
hosts: all
become: true
vars:
myuser: "example"
mygroups:
- adm
- sys
tasks:
- name: adding secondary group(s)
ansible.builtin.user:
name: "{{ myuser }}"
groups: "{{ mygroups }}"
append: true
execution
$ ansible-playbook -i virtualmachines/demo/inventory users_and_groups/user_group_addsecondary.yml
PLAY [user module demo] ***************************************************************************
TASK [Gathering Facts] ****************************************************************************
ok: [demo.example.com]
TASK [adding secondary groups] ********************************************************************
changed: [demo.example.com]
PLAY RECAP ****************************************************************************************
demo.example.com : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
before execution
$ ssh [email protected]
[[email protected] ~]$ sudo su
[[email protected] 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"
[[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]# grep example /etc/group
wheel:x:10:example
example:x:1002:
[[email protected] devops]#
after execution
$ ssh [email protected]
[[email protected] ~]$ sudo su
[[email protected] devops]# id example
uid=1002(example) gid=1002(example) groups=1002(example),3(sys),4(adm),10(wheel)
[[email protected] devops]# groups example
example : example sys adm wheel
[[email protected] devops]# getent passwd | grep example
example:x:1002:1002:Ansible example:/home/example:/bin/bash
[[email protected] devops]# grep example /etc/group
sys:x:3:example
adm:x:4:example
wheel:x:10:example
example:x:1002:
[[email protected] devops]#
Recap
Now you know how to add a user to second a group with Ansible.
Subscribe to the YouTube channel, Medium, Website, Twitter, and Substack to not miss the next episode of the Ansible Pilot.Academy
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
Donate
Want to keep this project going? Please donate