Ansible END TO END PROJECT - 2 Tier | set up two configuration management server groups
In these project we are set up two configuration management server groups,[Apache & Nginx] when Apache configuration is done, we are installing the java on that Apache server using the Ansible role.

1. Directory Structure
ansible_project/
|-- inventory/
| |-- production
|-- roles/
| |-- apache/
| |-- tasks/
| |-- main.yml
| |-- files/
| |-- index_apache.html
| |-- handlers/
| |-- main.yml
| |-- templates/
| |-- httpd.conf.j2
| |-- nginx/
| |-- tasks/
| |-- main.yml
| |-- files/
| |-- index_nginx.html
| |-- handlers/
| |-- main.yml
| |-- java/
| |-- tasks/
| |-- main.yml
|-- playbook.yml
|-- requirements.yml
The Main Project Directory.

This is the root directory for your Ansible project. All other subdirectories and files will be organized under this main directory.
The Inventory Directory.

The inventory directory is where you define your inventory files. The production file is a sample inventory file that lists your server groups.
Create the Roles Directory.
The roles directory is where you organize your roles. Roles are a way to organize your playbooks and make them more modular and reusable.
ansible_project/
|-- inventory/
| |-- production
|-- roles/

Create the Apache Role Directory Structure.

In the Apache Folder , task folder present or create then main.yml we need to put there.
roles/apache/tasks/main.yml:

---
- name: Install Apache #for installation
package:
name: apache2
state: present
- name: Copy Apache config #copying the apache2 Configuration from mentioning the path
template:
src: httpd.conf.j2
dest: /etc/apache2/apache2.conf # Adjust the path if needed
notify: Restart Apache
- name: Push Apache HTML file # Push the html file on the Apache server
copy:
src: index_apache.html
dest: /var/www/html/index.html
- name: Start Apache # start or staying start thr apache2 service
service:
name: apache2
state: started
This file contains the tasks specific to the Apache role.
roles/apache/handlers/main.yml:

---
- name: Restart Apache
service:
name: httpd
state: restarted
Handlers are tasks that only run when notified by other tasks.
roles/apache/files/index_apache.html:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Apache Server</title>
</head>
<body>
<h1>Welcome to Apache Server</h1>
<p>This page is served by Apache.</p>
</body>
</html>
This file Contents that, what content we want to show on the Apache server/target machines.
roles/apache/templates/httpd.conf.j2:

<VirtualHost *:80>
DocumentRoot /var/www/html
</VirtualHost>
This directory contains Jinja2 templates, used for generating the Apache configuration file
Create the NGINX Role Directory Structure.

roles/nginx/tasks/main.yml:

---
- name: Install NGINX
package:
name: nginx
state: present
- name: Push NGINX HTML file
copy:
src: index_nginx.html
dest: /usr/share/nginx/html/index.html
- name: Start NGINX
service:
name: nginx
state: started
This file contains the tasks specific to the NGINX role.
roles/nginx/handlers/main.yml:

--- main.yml ---
- name: Restart NGINX
service:
name: nginx
state: restarted
Handlers are tasks that only run when notified by other tasks.
roles/nginx/files/index_nginx.html:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>NGINX Server</title>
</head>
<body>
<h1>Welcome to NGINX Server</h1>
<p>This page is served by NGINX.</p>
</body>
</html>
This file Contents that, what content we want to show on the Nginx server/target machines.
Create the Java Role Directory Structure
roles/java/tasks/main.yml:

---
- name: Install Java
package:
name: openjdk-17-jdk
state: present
This file contains the tasks specific to the Java role.
Create the Playbook.

---
- hosts: apache_servers
become: yes # Add this line to run tasks with sudo privileges
roles:
- apache
- java
- hosts: nginx_servers
become: yes # Add this line to run tasks with sudo privileges
roles:
- nginx
The playbook.yml file is where you define which roles to apply to which server groups. This file is the main entry point for executing your Ansible configuration.
Create the Requirements File.

---
- name: apache
src: geerlingguy.apache
- name: nginx
src: geerlingguy.nginx
- name: java
src: geerlingguy.java
The requirement.yml file is used to specify external roles that need to be installed using ansible-galaxy.
After running the requirement.yml file , Download the all roles.


Running the playbook.yml



See the Output , which is mention Below.








Last updated