What you'll learn
- How to add a user to a second group on Linux with Ansible?
- Ansible adds a user to second a group
- Parameters
- Links
- Playbook
- code
- execution
- before execution
- after execution
- Conclusion
How to add a user to a second group on Linux with Ansible?
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 Conclusion is you specify the "`no`" option you are going to lose all the previous group associations, please be careful!
Links
- [Ansible user module](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/user_module.html)
Playbook
Add a user to second a group with Ansible Playbook.
code
- user_group_addsecondary.yml
```yaml
---
- name: user module Playbook
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
```bash
$ ansible-playbook -i virtualmachines/demo/inventory users_and_groups/user_group_addsecondary.yml
PLAY [user module Playbook] ***************************************************************************
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
```bash
$ ssh devops@demo.example.com
[devops@demo ~]$ sudo su
[root@demo devops]# cat /etc/os-release
NAME="Red Hat Enterprise Linux"
VERSION="8.5 (Ootpa)"
ID="rhel"
ID_LIKE