EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Ansible Add User to Group

By Priya PedamkarPriya Pedamkar

Home » Software Development » Software Development Tutorials » Ansible Tutorial » Ansible Add User to Group

Ansible Add User to Group

Introduction to Ansible Add User to Group

The following article provides an outline for Ansible Add User to Group. In Ansible, you have multiple default modules which comes with its package and you can also create customized modules using a supported scripting language like Python. The use of these module depends upon the requirements and available parameters as well as options. Some of the modules are basic and needed for day to day tasks such as user module, which is used to create, remove, update a user on remotes hosts. This is for Linux based OS on remotes hosts. For Microsoft Windows based remote hosts, there is another similar module named win_user. Here we will see user module and its alternatives to add user to group on remote hosts.

Ansible Add User to Group

In a operating system, user and groups are created to organize the privileges hierarchy. Also, to manage the user as per their roles, groups are created and assigned to those users. This is a general practice which takes place across all those operating systems which supports multiuser environment and login support. Using a configuration management automation tool like Ansible, makes such operational tasks, easy and smooth to execute when you have tens of target hosts.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Few parameters, while adding user to 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 use command module or user module in Ansible, to add user to group. For command module, we can directly pass Linux command as plain text to run on remote hosts. For using user module, we can use either ansible to do this by passing all parameters and options on command line or ansible-playbook by mentioning all parameters and options in a playbook and execute it.

Popular Course in this category
Ansible Training (1 Course, 4 Projects)1 Online Courses | 4 Hands-on Projects | 8+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (3,206 ratings)
Course Price

View Course

Related Courses
Linux Training Program (16 Courses, 3+ Projects)Kali Linux Training (3 Courses, 3+ Projects)Red Hat Linux Training Program (4 Courses, 1+ Projects)

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:

ansible add user to group

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

Code:

id testuser

Output:

user is created in remoted hosts

Code:

id testuser

Output:

ansible add user to group 3JPG

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:

pass all the parameters

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:

ansible add user to group 5JPG

Code:

id testuser

Output:

ansible add user to group 6JPG

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:

write all your requirements in a YAML file

On remote hosts, you can check like below:

Code:

id testuser

Output:

ansible add user to group 8JPG

Code:

id testuser

Output:

ansible add user to group 9JPG

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:

ansible add user to group 10JPG

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:

checking remote hosts,

Code:

id testuser

Output:

ansible add user to group 12JPG

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:

have use append with value
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:

ansible add user to group 14JPG

Code:

id testuser

Output:

ansible add user to group 15JPG

Conclusion

Adding and removing users from a group or a list of groups is a repetitive task. Which is important but confusing sometimes when doing it manually on command line. So, better practice is to maintain a playbook for such tasks and pass the actual values on real time during execution.

Recommended Article

This is a guide to Ansible Add User to Group. Here we discuss the introduction to Ansible Add User to Group with how to add user to group? how to remove user from group? and examples. You may also have a look at the following articles to learn more –

  1. Ansible Loop
  2. Ansible YAML
  3. Ansible Versions
  4. Ansible Vault

Ansible Training (1 Course)

1 Online Courses

4 Hands-on Projects

8+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
Ansible Tutorial
  • Ansible Basics
    • What is Ansible
    • Is Ansible free
    • How to Install Ansible
    • Ansible Versions
    • Ansible Architecture
    • Ansible Commands
    • Ansible ad-hoc Commands
    • Ansible Playbooks
    • Ansible Roles
    • Ansible Tags
    • Ansible Ping
    • Ansible Apt
    • Ansible Facts
    • Ansible Tower
    • Ansible Galaxy
    • Ansible Handlers
    • Ansible Loop
    • Ansible Block
    • Ansible Conditional
    • Ansible YAML
    • Ansible Debug
    • Ansible Synchronize
    • Ansible Template
    • Ansible group_vars
    • Ansible Lookup
    • Ansible File Module
    • Ansible Yum Module
    • Ansible Shell Module
    • Ansible lineinfile
    • Ansible Service Module
    • Ansible User Module
    • Ansible Windows Modules
    • ansible-doc
    • Ansible Filters
    • Ansible Add User to Group
    • Ansible Register
    • Ansible Set Fact
    • Ansible Hosts File
    • Ansible add_host
    • Ansible Collections
    • Ansible with_items
    • Ansible Replace Line in File
    • Ansible inventory_hostname
    • Ansible Dynamic Inventory
    • Ansible local_action
    • Ansible Firewalld
    • Ansible Unarchive
    • Ansible Sudo
    • Ansible Create Directory
    • Ansible Reboot
    • Ansible wait_for
    • Ansible run_once
    • Ansible Authorized_key
    • Ansible Interview Questions

Related Courses

Linux Training Course

Kali Linux Training

Red Hat Training Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - Ansible Training (1 Course) Learn More