Skip to main content
Redhat Developers  Logo
  • Products

    Featured

    • Red Hat Enterprise Linux
      Red Hat Enterprise Linux Icon
    • Red Hat OpenShift AI
      Red Hat OpenShift AI
    • Red Hat Enterprise Linux AI
      Linux icon inside of a brain
    • Image mode for Red Hat Enterprise Linux
      RHEL image mode
    • Red Hat OpenShift
      Openshift icon
    • Red Hat Ansible Automation Platform
      Ansible icon
    • Red Hat Developer Hub
      Developer Hub
    • View All Red Hat Products
    • Linux

      • Red Hat Enterprise Linux
      • Image mode for Red Hat Enterprise Linux
      • Red Hat Universal Base Images (UBI)
    • Java runtimes & frameworks

      • JBoss Enterprise Application Platform
      • Red Hat build of OpenJDK
    • Kubernetes

      • Red Hat OpenShift
      • Microsoft Azure Red Hat OpenShift
      • Red Hat OpenShift Virtualization
      • Red Hat OpenShift Lightspeed
    • Integration & App Connectivity

      • Red Hat Build of Apache Camel
      • Red Hat Service Interconnect
      • Red Hat Connectivity Link
    • AI/ML

      • Red Hat OpenShift AI
      • Red Hat Enterprise Linux AI
    • Automation

      • Red Hat Ansible Automation Platform
      • Red Hat Ansible Lightspeed
    • Developer tools

      • Red Hat Trusted Software Supply Chain
      • Podman Desktop
      • Red Hat OpenShift Dev Spaces
    • Developer Sandbox

      Developer Sandbox
      Try Red Hat products and technologies without setup or configuration fees for 30 days with this shared Openshift and Kubernetes cluster.
    • Try at no cost
  • Technologies

    Featured

    • AI/ML
      AI/ML Icon
    • Linux
      Linux Icon
    • Kubernetes
      Cloud icon
    • Automation
      Automation Icon showing arrows moving in a circle around a gear
    • View All Technologies
    • Programming Languages & Frameworks

      • Java
      • Python
      • JavaScript
    • System Design & Architecture

      • Red Hat architecture and design patterns
      • Microservices
      • Event-Driven Architecture
      • Databases
    • Developer Productivity

      • Developer productivity
      • Developer Tools
      • GitOps
    • Secure Development & Architectures

      • Security
      • Secure coding
    • Platform Engineering

      • DevOps
      • DevSecOps
      • Ansible automation for applications and services
    • Automated Data Processing

      • AI/ML
      • Data Science
      • Apache Kafka on Kubernetes
      • View All Technologies
    • Start exploring in the Developer Sandbox for free

      sandbox graphic
      Try Red Hat's products and technologies without setup or configuration.
    • Try at no cost
  • Learn

    Featured

    • Kubernetes & Cloud Native
      Openshift icon
    • Linux
      Rhel icon
    • Automation
      Ansible cloud icon
    • Java
      Java icon
    • AI/ML
      AI/ML Icon
    • View All Learning Resources

    E-Books

    • GitOps Cookbook
    • Podman in Action
    • Kubernetes Operators
    • The Path to GitOps
    • View All E-books

    Cheat Sheets

    • Linux Commands
    • Bash Commands
    • Git
    • systemd Commands
    • View All Cheat Sheets

    Documentation

    • API Catalog
    • Product Documentation
    • Legacy Documentation
    • Red Hat Learning

      Learning image
      Boost your technical skills to expert-level with the help of interactive lessons offered by various Red Hat Learning programs.
    • Explore Red Hat Learning
  • Developer Sandbox

    Developer Sandbox

    • Access Red Hat’s products and technologies without setup or configuration, and start developing quicker than ever before with our new, no-cost sandbox environments.
    • Explore Developer Sandbox

    Featured Developer Sandbox activities

    • Get started with your Developer Sandbox
    • OpenShift virtualization and application modernization using the Developer Sandbox
    • Explore all Developer Sandbox activities

    Ready to start developing apps?

    • Try at no cost
  • Blog
  • Events
  • Videos

How Ansible automates Linux server user and group management

October 11, 2023
Himanshu Yadav
Related topics:
Automation and managementLinux
Related products:
Red Hat Ansible Automation Platform

Share:

    Due to the day-to-day activities of developers, system administrators need to automate the management of users and groups. Red Hat Ansible Automation Platform simplifies this process with the help of Ansible Playbooks. In this article, we will demonstrate user and group management on managed node instances using Ansible Automation Platform.

    How to automate user/group management

    We will create a playbook to run the automation task. First, we will create three different groups that identify three kinds of users with access to our Linux system:

    1. Admins
    2. Developers
    3. Quality Assurance

    We will add groups to our system. Let's define our groups in our playbook file. Create an playbook.yml and include the following:

    ---
    - hosts: localhost
      become: true
      tasks:
        - name: Create admin group
          group:
            name: admin
            state: present
        
        - name: Create developers group
          group:
            name: developers
            state: present
        
        - name: Create quality assurance group
          group:
            name: quality_assurance
            state: present
    

    Next, we will add a few users to these groups. Add users ad1 and ad2 to the admin group, users dev1 and dev2 to the developer group, and users qa1 and qa2 to the quality assurance group. Add the following content to the previously created YAML file:

    - name: Add ad1 and ad2 to admin group
      user:
        name: "{{ item }}"
        groups: admin
        password: "{{ 'admin' | password_hash('sha512') }}"
      with_items:
        - ad1
        - ad2
        
    - name: Add dev1 and dev2 to developers group
      user:
        name: "{{ item }}"
        groups: developers
        password: "{{ 'dev' | password_hash('sha512') }}"
      with_items:
        - dev1
        - dev2
        
    - name: Add qa1 and qa2 to quality assurance group
      user:
        name: "{{ item }}"
        groups: quality_assurance
        password: "{{ 'test' | password_hash('sha512') }}"
      with_items:
        - qa1
        - qa2

    Finally, we will grant certain privileges to these groups which will enable the users of these groups with those same privileges:

    - name: Grant admin group sudo privileges
      lineinfile:
        path: /etc/sudoers
        line: "%admin ALL=(ALL) NOPASSWD: ALL"
        state: present
        
    - name: Grant developers group access to /opt/myapp
      file:
        path: /opt/myapp
        owner: root
        group: developers
        mode: "0755"
        state: directory
        
    - name: Grant quality assurance group access to /var/log/myapp
      file:
        path: /var/log/myapp
        owner: root
        group: quality_assurance
        mode: "0740"
        state: directory
    

    The final YAML will look like this:

    ---
    #######  creating groups ######
    - hosts: localhost
      become: true
      tasks:
        - name: Create admin group
          group:
            name: admin
            state: present
        
        - name: Create developers group
          group:
            name: developers
            state: present
        
        - name: Create quality assurance group
          group:
            name: quality_assurance
            state: present
    
    #######  Adding Users in Group ######
        
        - name: Add ad1 and ad2 to admin group
          user:
            name: "{{ item }}"
            groups: admin
    	  password: "{{ 'admin' | password_hash('sha512') }}"
          with_items:
            - ad1
            - ad2
        
        - name: Add dev1 and dev2 to developers group
          user:
            name: "{{ item }}"
            groups: developers
            password: "{{ 'dev' | password_hash('sha512') }}"
          with_items:
            - dev1
            - dev2
        
        - name: Add qa1 and qa2 to quality assurance group
          user:
            name: "{{ item }}"
            groups: quality_assurance
            password: "{{ 'test' | password_hash('sha512') }}"
          with_items:
            - qa1
            - qa2
    
    #######  Setting up permissions to Groups ######
        
        - name: Grant admin group sudo privileges
          lineinfile:
            path: /etc/sudoers
            line: "%admin ALL=(ALL) NOPASSWD: ALL"
            state: present
        
        - name: Grant developers group access to /opt/myapp
          file:
            path: /opt/myapp
            owner: root
            group: developers
            mode: "0755"
            state: directory
        
        - name: Grant quality assurance group access to /var/log/myapp
          file:
            path: /var/log/myapp
            owner: root
            group: quality_assurance
            mode: "0740"
            state: directory
    

    Run Ansible Playbooks

    Run the playbook using the following command:

    ansible-playbook playbook.yml

    Now head over to your managed node and log into the dev user from the terminal using the following command:

    su - dev1

    On the prompt, enter the password: dev. Check the permissions on the /opt/myapp folder as follows: drwxr-xr-x (read and execute for developers and others, write permission reserved for user). Change the permissions to your needs accordingly in the playbook file.

    Now, we will use Ansible Automation Platform to run these playbooks. Install the latest version of Ansible Automation Platform. You can also refer to the Ansible Automation Platform 2.3 installation guide for a RHEL 9 system. After the installation is complete, navigate to the dashboard on https://localhost.

    Create new project

    A project is a collection of Ansible Playbooks which perform a certain set of automated tasks. Here, our project will contain our single playbook.

    Steps to create a new project:

    • Head over to the Projects tab (Figure 1).
    • Click on the Add option and select a name for the project and select the default organization.
    • Add the GitHub URL to the Source Control URL field: https://github.com/4molybdenum2/ansible-automation-demos/tree/main/user_mgmt.
    • Save the project.
    Projects
    Figure 1: Project Page

    Steps to create an inventory

    • Create an inventory from the Inventories tab and add a group named webstack (Figure 2).
    Inventory
    Figure 2: Inventory Page
    • Head back to the inventory edit page and navigate to the Hosts tab. 
    • Add a host with the name as the IP address of the node on which the playbook will run (Figure 3).
    Hosts
    Figure 3: Host Page

    Steps to add a credential

    • Navigate to the Credential tab and add a credential with type Machine, username and password as that of our managed node, and private key as copied over from control node file: ~/.ssh/id_rsa (Figure 4).
    • Fill the Privilege Escalation password with the password for our managed node which will run the commands as a sudo user.
    Credentials
    Figure 4: Credentials Page

    Steps to create a template

    • Head over to the Templates tab and create a new template.
    • Add the project that we created and select the playbook from our GitHub or any other source control repository where we uploaded the playbook.
    • Add the Inventory, Credentials, and the Execution Environment as Default execution environment (Figure 5). 
    Templates
    Figure 5: Templates Page
    • After the template has been created, you can launch it, and the job will be visible under the Jobs tab, as shown in Figure 6.

    Jobs
    Figure 6: Jobs Page

    Continue your automation journey with Ansible Automation Platform

    You can get started with the Ansible Automation Platform by downloading at no cost and exploring interactive labs at Red Hat Developer.

    Related Posts

    • What's new in Ansible Automation Platform 2.4

    • The benefits of deploying Ansible Automation Platform on AWS

    • Automate workshop setup with Ansible playbooks and CodeReady Workspaces

    • 6 steps to install Ansible Automation Platform 2.3 on RHEL

    Recent Posts

    • AI meets containers: My first step into Podman AI Lab

    • Live migrating VMs with OpenShift Virtualization

    • Storage considerations for OpenShift Virtualization

    • Upgrade from OpenShift Service Mesh 2.6 to 3.0 with Kiali

    • EE Builder with Ansible Automation Platform on OpenShift

    What’s up next?

    Why automate? Which tool do I use? Choosing an automation tool dives into various automation options, how they work, what to automate, and the benefits of each tool.

    Get the e-book
    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Products

    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform

    Build

    • Developer Sandbox
    • Developer Tools
    • Interactive Tutorials
    • API Catalog

    Quicklinks

    • Learning Resources
    • E-books
    • Cheat Sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site Status Dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit
    © 2025 Red Hat

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Report a website issue