You are on page 1of 3

Lab Manual: Creating an Ansible Playbook for a Single Web Application

Infrastructure

Objective:

The objective of this lab is to guide the process of creating an Ansible playbook
to provision and configure a single web application infrastructure. The
playbook will automate the setup of web server, database server, and any
other required components for hosting the web application.

Prerequisites:

• Ansible installed on your local machine or a control node


• Basic understanding of Ansible concepts such as playbooks, roles, and
tasks
• Access to target servers (virtual machines or cloud instances) where the
infrastructure will be provisioned

Lab Steps:

1. Setup Ansible:

• Ensure Ansible is installed on your local machine or a control node.


• If Ansible is not installed, you can follow the official Ansible
documentation for installation instructions.

2. Define Inventory:

• Create an inventory file (inventory.ini) listing the target servers where


you want to provision the infrastructure.
• Example inventory.ini:
csharpCopy code
[ web_servers ]
web1 ansible_host= 192.168.1.101
web2 ansible_host= 192.168.1.102

[ db_servers ]
db1 ansible_host= 192.168.1.103

3. Create Ansible Playbook:

• Create a playbook file (webapp_infrastructure.yml) to define the tasks


for provisioning the infrastructure.
• Define tasks for installing required packages, configuring web server,
setting up database server, etc.
• Example webapp_infrastructure.yml:
yamlCopy code
- name: Provision Web Application Infrastructure
hosts: web_servers
become: true
tasks:
- name: Install Apache web server
yum:
name: httpd
state: present
tags:
- apache

- name: Start Apache service


service:
name: httpd
state: started
tags:
- apache

- name: Provision Database Server


hosts: db_servers
become: true
tasks:
- name: Install MySQL server
yum:
name: mysql-server
state: present
tags:
- mysql

- name: Start MySQL service


service:
name: mysqld
state: started
tags:
- mysql

4. Run Ansible Playbook:

• Open a terminal or command prompt.


• Navigate to the directory where your playbook and inventory files are
located.
• Run the Ansible playbook using the following command:
cssCopy code
ansible-playbook - i inventory .ini webapp_infrastructure .yml

5. Verify Infrastructure:

• Once the playbook execution is complete, verify that the infrastructure


has been provisioned as expected.
• Access the web servers' IP addresses in a web browser to verify that
Apache is serving web pages.
• Connect to the database server to verify that MySQL service is running.

6. Customize and Expand:

• Customize the playbook according to your specific requirements.


• Expand the playbook to include additional tasks such as deploying the
web application code, configuring load balancers, setting up
monitoring, etc.

Conclusion:
In this lab, successfully created an Ansible playbook to provision and
configure a single web application infrastructure. Ansible allows you to
automate the setup of servers and services, enabling consistent and reliable
deployments of your web applications. With further customization and
expansion, you can adapt the playbook to meet the needs of your specific
infrastructure and application requirements.

You might also like