Ansible Add User to Group

Ansible Add User to Group
Techiio-author
Written by Nilima PaulFebruary 1, 2022
11 min read
Ansible
2 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 2 VIEWS SHARE
Techiio-author
Nilima Paul

Technology Security Analyst

We will know in this article, what's the Ansible Add User to Group.

Introduction to Ansible Add User to Group

The accompanying article gives a layout to Ansible Add User to Group. In Ansible, you have different default modules which accompany its bundle and you can likewise make modified modules utilizing an upheld prearranging language like Python. The utilization of these modules relies on the necessities and accessible boundaries just as choices. A portion of the modules are essential and required for everyday undertakings, for example, the client module, which is utilized to make, eliminate, update a client on controllers has. This is for Linux put-together OS with respect to controllers has. For Microsoft Windows-based remote hosts, there is another comparable module named win_user. Here we will see the client module and its choices to add the client to a bunch of remote hosts.

Ansible Add User to Group

In a working framework, clients and gatherings are made to arrange the honors pecking order. Additionally, to deal with the client according to their jobs, bunches are made and allowed to those clients. This is an overall practice that happens across that large number of working frameworks that uphold multiuser climate and login support. Utilizing a design the executive's computerization apparatus like Ansible makes such functional undertakings, simple and smooth to execute when you have many objective hosts.

Few parameters, while adding a user to the group.

Given below are the few parameters:

1. append: This parameter is useful when you want to append a user to a group or a list of groups.

This accepts two values:

  • yes: To append all the listed groups in groups field, to the user’s mapped group list or you can say, add the user to the groups specified in groups.
  • no: To overwrite the assigned group list of the user with only groups mentioned under groups field, meaning removing the user from all groups except mentioned in groups. This is the default option if nothing is mentioned.

2. group: To set the user’s primary group. Acceptable option is a valid group name.

3. groups: To give the list of secondary groups, to which the user will be added to. When an empty string (‘ ’) is given, user will be removed from all the groups except its primary group.

How to Add User to Group?

To add a user to a group, we can have two scenarios related to user’s existence:

  • User is new and need to be added to existing groups.
  • User is existing and need to be added to existing groups.

In both the cases, we can involve order module or client module in Ansible, to add client to bunch. For order module, we can straightforwardly pass Linux order as plain text to run on remote hosts. For utilizing client module, we can utilize either ansible to do this by passing all boundaries and choices on order line or ansible-playbook by referencing all boundaries and choices in a playbook and execute it.

How to Remove User from Group?

  • To remove a user from a group or list of groups we can either use command module and pass Linux command in plain text or use user module via ansible or in a playbook and execute by ansible-playbook.
  • One point to note that while using user module, there is no direct way to remote a user from a group or list of groups.
  • We should use append field and provide the list of groups to groups section in playbook.

Examples of Ansible Add User to Group

Given below are the examples mentioned:

Here we have an Ansible control server named ansible-controller and two remotes hosts named host-one and host-two. We will create playbooks and run ansible commands on ansible-controller node and manage the users on remote hosts.

Example #1

Add a new user to a list of secondary groups while creating it.

This can be done by below different ways:

a. Use command module and pass Linux command to add as user and assign a list of groups as secondary group to it.

Code:

ansible all -m command -a "useradd testuser -G testgroup1,testgroup2,testgroup3"

Output:

blogpost

You can check the user is created in remoted hosts and assigned groups like below:

Code:

id testuser

Output:

blogpost

Code:

id testuser

Output:

blogpost

b. Use ansible and pass all the parameters on command line like below:

Code:

ansible all -m user -a "name=testuser groups=testgroup1,testgroup2,testgroup3 append=yes"

Output:

blogpost

Upon checking on target systems, you will find that user is created and assigned to the listed groups as secondary groups.

Code:

id testuser

Output:

blogpost

c. Use ansible-playbook and write all your requirements in a YAML file called Playbook which have all the values in key-pair form.

The playbook content will be like below:

Code:

hosts:
all
tasks:
user:
name: testuser
groups:
testgroup1,testgroup2,testgroup3
append: yes

When executes it like below:

Code:

ansible-playbook useradd_assign_secondary_group.yaml

Output:

blogpost

On remote hosts, you can check like below:

Code:

id testuser

Output:

blogpost

Code:

id testuser

Output:

blogpost

Example #2

For adding an existing user to a group. You can use either command module or user like above. But better we practice writing playbooks, as this is more organized. Also, the output is easily readable.

We can write a playbook like below:

Code:

hosts:
all
tasks:
user:
name: testuser
groups:
testgroup4
append: yes

Then execute it like below:

Code:

ansible-playbook adding_user_to_group.yaml

Output:

blogpost

Also, on checking remote hosts, we can see that a new group have been added to the list of secondary groups.

Code:

id testuser

Output:

blogpost

Code:

id testuser

Output:

blogpost

Example #3

For removing a user from group, we have use append with value.

To do this, we can create playbook like below:

Code:

hosts:
all
tasks:
user:
name: testuser
groups:
testgroup4
append: no

Then executes it like below:

Code:

ansible-playbook removing_user_from_groups.yaml

Output:

blogpost

On remote hosts, if you check you will find that, the user testuser have only testuser4 as secondary group and it has been removed from rest of all secondary groups,

Code:

id testuser

Output:

blogpost

Code:

id testuser

Output:

blogpost

Conclusion

Adding and eliminating clients from a gathering or a rundown of gatherings is a dreary assignment. Which is significant however confounding some of the time while doing it physically on order line. Along these lines, better practice is to keep a playbook for such assignments and pass the real qualities on ongoing during execution.

Ansible
yaml
python
2 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 2 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-author
Nilima Paul
Technology Security Analyst
Techiio-followerTechiio-followerTechiio-follower
201 Blog Posts
0 Discussion Threads
Trending Technologies
15
Software40
DevOps46
Frontend Development24
Backend Development20
Server Administration17
Linux Administration26
Data Center24
Sentry24
Terraform23
Ansible83
Docker70
Penetration Testing16
Kubernetes21
NGINX20
JenkinsX17
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance