Ansible projects:Hands-on with essential automation skills

RMAG news

1. Automated Web Server Deployment

Overview

This project automates the setup of an Nginx or Apache web server on multiple hosts. It’s commonly used for creating web server clusters and ensuring consistent configurations across servers.

Project Structure

webserver-deployment/
├── playbook.yml
├── inventory.ini
└── roles/
└── webserver/
├── tasks/
│ └── main.yml
├── handlers/
│ └── main.yml
└── vars/
└── main.yml

Steps

Define the Inventory File (inventory.ini):

[webservers]
server1 ansible_host=192.168.1.101
server2 ansible_host=192.168.1.102

Create Playbook (playbook.yml):

name: Deploy Web Server
hosts: webservers
roles:
webserver

Define the Role (roles/webserver):

Tasks (tasks/main.yml):

name: Install Nginx
ansible.builtin.yum:
name: nginx
state: present

name: Start and Enable Nginx
ansible.builtin.service:
name: nginx
state: started
enabled: yes

Handlers (handlers/main.yml):

name: Restart Nginx
ansible.builtin.service:
name: nginx
state: restarted

Variables (vars/main.yml):

nginx_port: 80

Run the Playbook:

ansible-playbook -i inventory.ini playbook.yml

2. User Management System

Overview

This project manages users and groups across multiple servers, which is helpful in environments where you need centralized user management.

Project Structure

user-management/
├── playbook.yml
├── inventory.ini
└── roles/
└── user/
├── tasks/
│ └── main.yml
└── vars/
└── main.yml

Steps

Define the Inventory (inventory.ini):

[userservers]
server1 ansible_host=192.168.1.101

Playbook (playbook.yml):

name: User Management
hosts: userservers
roles:
user

Role Structure (roles/user):

Tasks (tasks/main.yml):

name: Create a group
ansible.builtin.group:
name: {{ group_name }}”
state: present

name: Create user
ansible.builtin.user:
name: {{ user_name }}”
group: {{ group_name }}”
state: present

Variables (vars/main.yml):

user_name: john
group_name: developers

Run the Playbook:

ansible-playbook -i inventory.ini playbook.yml

3. Database Server Setup (MySQL)

Overview

This project configures a MySQL server, sets up databases, and defines users with necessary permissions.

Project Structure

mysql-setup/
├── playbook.yml
├── inventory.ini
└── roles/
└── mysql/
├── tasks/
│ └── main.yml
├── handlers/
│ └── main.yml
└── vars/
└── main.yml

Steps

Inventory File (inventory.ini):

[dbservers]
server1 ansible_host=192.168.1.103

Playbook (playbook.yml):

name: MySQL Server Setup
hosts: dbservers
roles:
mysql

Role Structure (roles/mysql):

Tasks (tasks/main.yml):

name: Install MySQL
ansible.builtin.yum:
name: mysql-server
state: present

name: Start MySQL Service
ansible.builtin.service:
name: mysqld
state: started
enabled: yes

name: Create MySQL Database
ansible.builtin.mysql_db:
name: {{ db_name }}”
state: present

name: Create MySQL User
ansible.builtin.mysql_user:
name: {{ db_user }}”
password: {{ db_pass }}”
priv: *.*:ALL”
state: present

Variables (vars/main.yml):

db_name: my_database
db_user: db_user
db_pass: db_pass

Run the Playbook:

ansible-playbook -i inventory.ini playbook.yml

4. Application Deployment with Docker

Overview

This project deploys a web application using Docker, making it easier to manage dependencies and scale applications.

Project Structure

docker-deployment/
├── playbook.yml
├── inventory.ini
└── roles/
└── app/
├── tasks/
│ └── main.yml
├── files/
│ └── Dockerfile
└── vars/
└── main.yml

Steps

Inventory (inventory.ini):

[appservers]
server1 ansible_host=192.168.1.104

Playbook (playbook.yml):

name: Deploy App with Docker
hosts: appservers
roles:
app

Role Structure (roles/app):

Tasks (tasks/main.yml):

name: Copy Dockerfile
ansible.builtin.copy:
src: Dockerfile
dest: /tmp/Dockerfile

name: Build Docker Image
ansible.builtin.command:
cmd: docker build -t myapp /tmp

name: Run Docker Container
ansible.builtin.docker_container:
name: myapp
image: myapp
state: started
restart_policy: always

Dockerfile (files/Dockerfile):

FROM python:3.8
WORKDIR /app
COPY . .
RUN pip install -r requirements.txt
CMD [“python”, “app.py”]

Run the Playbook:

ansible-playbook -i inventory.ini playbook.yml

5. System Update and Patch Management

Overview

This project automates the system update process across multiple servers, ensuring that all servers are up to date with the latest security patches.

Project Structure

system-update/
├── playbook.yml
└── inventory.ini

Steps

Inventory (inventory.ini):

[all]
server1 ansible_host=192.168.1.105
server2 ansible_host=192.168.1.106

Playbook (playbook.yml):

name: System Update and Patching
hosts: all
become: yes
tasks:
name: Update all packages
ansible.builtin.yum:
name: *”
state: latest

name: Reboot if Kernel Updated
ansible.builtin.reboot:
msg: Reboot initiated by Ansible for kernel updates”
connect_timeout: 5
reboot_timeout: 600
pre_reboot_delay: 5
post_reboot_delay: 30

Run the Playbook:

ansible-playbook -i inventory.ini playbook.yml

These Ansible projects cover essential skills and scenarios. Let me know if you’d like further details on any project or additional configurations!

Please follow and like us:
Pin Share