Automating User Management, Permissions, Partitioning, Yum, and Server Configuration Using Ansible

Ayushmaan Srivastav
3 min readOct 26, 2024

--

Automation is a key aspect of modern IT operations, enabling teams to streamline processes, reduce errors, and enhance productivity. Ansible, a powerful automation tool, can manage various system configurations, including user accounts, permissions, partition management, package management with Yum, and server setups. In this blog, we will explore how to automate these tasks using Ansible playbooks.

1. Automating User Management

Managing user accounts is critical for maintaining security and access control in any organization. With Ansible, you can easily create, modify, and delete user accounts across multiple servers.

Playbook Example:

- hosts: all
tasks:
- name: Ensure user exists
user:
name: john_doe
state: present
shell: /bin/bash
groups: wheel
password: "{{ 'mypassword' | password_hash('sha512') }}"

- name: Ensure user is removed
user:
name: temp_user
state: absent

Explanation:

  • The user module creates a user named john_doe, assigns a shell, adds the user to the wheel group, and sets a hashed password.
  • The second task removes a temporary user.

2. Managing Permissions

Permissions control access to files and directories. Ansible can automate the process of setting and modifying permissions, ensuring that only authorized users have access.

Playbook Example:

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

- name: Set permissions on a file
file:
path: /var/www/html/index.html
owner: www-data
group: www-data
mode: '0644'

Explanation:

  • The file module sets permissions for a directory and a file, ensuring the correct ownership and access rights.

3. Partition Management

Ansible can automate partitioning of disks, making it easier to manage storage on servers. This is particularly useful when setting up new servers or when storage needs to be reconfigured.

Playbook Example:

- hosts: all
tasks:
- name: Create a new partition
command: "parted /dev/sdb mkpart primary ext4 1GB 10GB"

- name: Format the partition
filesystem:
fstype: ext4
dev: /dev/sdb1

- name: Mount the partition
mount:
path: /mnt/data
src: /dev/sdb1
fstype: ext4
state: mounted

Explanation:

  • The command module creates a new partition, the filesystem module formats it, and the mount module mounts it to a specified directory.

4. Automating Yum Package Management

Yum is a popular package manager for RPM-based distributions. You can automate the installation, removal, and updating of packages using Ansible.

Playbook Example:

- hosts: all
tasks:
- name: Install httpd package
yum:
name: httpd
state: present

- name: Ensure all packages are up to date
yum:
name: '*'
state: latest

Explanation:

  • The yum module installs the httpd package and ensures that all packages are updated to their latest versions.

5. Server Configuration

Ansible can automate the overall server configuration, including setting up services, configurations, and security settings.

Playbook Example:

- hosts: all
tasks:
- name: Start and enable httpd service
service:
name: httpd
state: started
enabled: true

- name: Copy custom configuration file
copy:
src: /path/to/local/httpd.conf
dest: /etc/httpd/conf/httpd.conf
owner: root
group: root
mode: '0644'

Explanation:

  • The service module ensures that the httpd service is running and enabled on startup.
  • The copy module transfers a custom configuration file from the local machine to the server.

Conclusion

Using Ansible to automate user management, permissions, partition management, package management, and server configuration can significantly enhance efficiency and consistency across your IT environment. By leveraging the power of Ansible playbooks, you can ensure that your infrastructure remains secure, well-organized, and easy to manage.

--

--

No responses yet