← Back to list

Automating User Management, Permissions, Partitioning, YUM, and Server Setup with Ansible

Automation has become an essential part of modern IT practices. Ansible, a powerful automation tool, allows system administrators to…

P Hemanth Kumar Reddy · 2024-10-03 06:38 · 0 claps · 2.9 min read
#ansible #user-management #disk-partitioning #server-setup #configuration-management
Open on Medium ↗
Wiki topics: BIZ · Business Strategy ☁️ · DevOps & Cloud

Automating User Management, Permissions, Partitioning, YUM, and Server Setup with Ansible

Automation has become an essential part of modern IT practices. Ansible, a powerful automation tool, allows system administrators to automate tasks such as user management, permissions, disk partitioning, package management (YUM), and server setup. This article will explore how to leverage Ansible to automate these critical tasks effectively.

Prerequisites

Before we start, ensure you have the following:

  • Ansible installed on your control machine.
  • SSH access to the target servers.
  • Proper permissions to execute Ansible playbooks on the target servers.

You can install Ansible using pip:

pip install ansible

Alternatively, you can install it via your package manager. For example, on CentOS/RHEL:

sudo yum install ansible

1. Automating User Management

User management is crucial for maintaining system security. Ansible can create, modify, and remove users easily using the user module.

Example Playbook for User Management

---
- name: User Management Automation
  hosts: target_servers
  become: yes
  tasks:
    - name: Create a new user
      user:
        name: johndoe
        password: "{{ 'secure_password' | password_hash('sha512') }}"
        state: present
        shell: /bin/bash

    - name: Ensure the user is in the wheel group
      user:
        name: johndoe
        groups: wheel
        append: yes

    - name: Remove an old user
      user:
        name: olduser
        state: absent

Explanation

  • The user module is used to create a new user johndoe, set a password, and assign it to the wheel group.
  • The playbook also removes an old user named olduser.

2. Automating Permissions

Managing file and directory permissions is vital for security. The file module in Ansible can help manage permissions.

Example Playbook for Permission Management

---
- name: Permission Management Automation
  hosts: target_servers
  become: yes
  tasks:
    - name: Set permissions on a directory
      file:
        path: /var/www/html
        owner: apache
        group: apache
        mode: '0755'
        state: directory

Explanation

  • This playbook sets the owner, group, and permissions of the /var/www/html directory.

3. Automating Disk Partitioning

Partitioning can be automated using the parted module in Ansible. This allows you to create or modify disk partitions.

Example Playbook for Disk Partitioning

---
- name: Disk Partitioning Automation
  hosts: target_servers
  become: yes
  tasks:
    - name: Create a new partition
      parted:
        device: /dev/sdb
        number: 1
        state: present
        fs_type: ext4
        size: 10GB

Explanation

  • This playbook creates a new partition on /dev/sdb with a size of 10GB and formats it as ext4.

4. Automating YUM Package Management

Managing packages with YUM can be automated using the yum module. You can install, update, or remove packages.

Example Playbook for YUM Management

---
- name: YUM Package Management Automation
  hosts: target_servers
  become: yes
  tasks:
    - name: Install httpd package
      yum:
        name: httpd
        state: present

    - name: Remove a package
      yum:
        name: oldpackage
        state: absent

    - name: Update all packages
      yum:
        name: '*'
        state: latest

Explanation

  • This playbook installs the httpd package, removes an old package, and updates all installed packages to the latest version.

5. Automating Server Setup

Setting up a server with required configurations and services can be time-consuming. Ansible simplifies this with the systemd module to manage services.

Example Playbook for Server Setup

---
- name: Server Setup Automation
  hosts: target_servers
  become: yes
  tasks:
    - name: Start and enable httpd service
      systemd:
        name: httpd
        state: started
        enabled: yes

    - name: Create a custom firewall rule
      firewalld:
        service: http
        permanent: yes
        state: enabled

Explanation

  • This playbook starts the httpd service and ensures it is enabled at boot time. It also creates a firewall rule to allow HTTP traffic.

6. Putting It All Together

You can combine all the above tasks into a single playbook. Here’s how that might look:

Combined Playbook Example

---
- name: Complete Automation with Ansible
  hosts: target_servers
  become: yes
  tasks:
    - name: Create a new user
      user:
        name: johndoe
        password: "{{ 'secure_password' | password_hash('sha512') }}"
        state: present
        shell: /bin/bash

    - name: Set permissions on a directory
      file:
        path: /var/www/html
        owner: apache
        group: apache
        mode: '0755'
        state: directory

    - name: Create a new partition
      parted:
        device: /dev/sdb
        number: 1
        state: present
        fs_type: ext4
        size: 10GB

    - name: Install httpd package
      yum:
        name: httpd
        state: present

    - name: Start and enable httpd service
      systemd:
        name: httpd
        state: started
        enabled: yes

Conclusion

Ansible is a versatile tool that can greatly simplify the management of users, permissions, disk partitions, YUM packages, and server setups. By automating these tasks, organizations can improve efficiency, reduce human error, and ensure consistency across environments.

By using playbooks, you can ensure that your server configurations are reproducible and maintainable. With the increasing complexity of IT infrastructures, tools like Ansible have become essential for modern system administration.


메타데이터
post_id
82ed039cd004
slug
automating-user-management-permissions-partitioning-yum-and-server-setup-with-ansible-82ed039cd004
url
https://medium.com/@hemanthreddy.p739/automating-user-management-permissions-partitioning-yum-and-server-setup-with-ansible-82ed039cd004
canonical_url
https://medium.com/@hemanthreddy.p739/automating-user-management-permissions-partitioning-yum-and-server-setup-with-ansible-82ed039cd004
author_url
https://medium.com/@hemanthreddy.p739
status
ok
fetched_at
2026-08-04 21:03:15