Automating Lamp Website Deployment on Web01 and Database on Db01

In this blog, we will discuss the steps to automate the deployment of a Lamp website on Web01 and Database on Db01 servers. We will use virtual machines to deploy these servers and use Puppet and Ansible to automate the configuration and deployment process.

Building server within 15min 

Step 1: Clone the Virtual Machines

First, we need to clone the virtual machines. Login to Kvm02 and create a clone of the existing virtual machine named Temp-vm1. We will create two clones with the names web01 and db01, respectively.

# virt-clone --connect qemu:///system --original temp-vm1 --name web01 -f /kvmstore/web01.img
# virt-clone --connect qemu:///system --original temp-vm1 --name db01 -f /kvmstore/db01.img

Step 2: Configure Web01

Start the web01 virtual machine and change the hostname, IP address, disable SELinux, and firewall. We can use the following commands to do that.

# virsh start web01
# ssh 192.168.2.229
# cat > update-hosts.sh << EOF
hostnamectl set-hostname web01.darole.org
sed -i 's/192.168.2.229/192.168.2.221/g' /etc/sysconfig/network-scripts/ifcfg-enp1s0
sed -i 's/enforcing/disabled/g' /etc/selinux/config
systemctl disable firewalld
reboot
EOF
# sh update-hosts.sh

Next, we need to copy the Puppet agent installation script to Web01 and run it. We can use the following commands to do that.

# ssh-copy-id web01
# scp /root/script/puppet-agent-installation.sh web01:/root/
# ssh web01
# echo "192.168.2.213 pup01.darole.org pup01" >> /etc/hosts
# sh puppet-agent-installation.sh

We also need to accept the certificate on the Puppet server. We can use the following commands to do that.

# puppetserver ca list
# puppetserver ca sign --all

Finally, we need to run the Puppet agent to create the Ansible user and install the required default packages. We can use the following command to do that.

# /opt/puppetlabs/bin/puppet agent --test

Step 3: Configure db01

Start the db01 virtual machine and change the hostname, IP address, disable SELinux, and firewall. We can use the following commands to do that.

# virsh start db01
# ssh 192.168.2.229
# cat > update-hosts.sh << EOF
hostnamectl set-hostname db01.darole.org
sed -i 's/192.168.2.229/192.168.2.222/g' /etc/sysconfig/network-scripts/ifcfg-enp1s0
sed -i 's/enforcing/disabled/g' /etc/selinux/config
systemctl disable firewalld
reboot
EOF
#sh update-hosts.sh

Next, we need to copy the Puppet agent installation script to Db01 and run it. We can use the following commands to do that.
# ssh-copy-id db01
# scp /root/script/puppet-agent-installation.sh db01:/root/
# ssh db01
# echo "192.168.2.213 pup01.darole.org pup01" >> /etc/hosts
# sh puppet-agent-installation.sh

We also need to accept the certificate on the Puppet server. We can use the following commands to do that.
# puppetserver ca list
# puppetserver ca sign --all

Finally, we need to run the Puppet agent to create the Ansible user and install the required default packages. We can use the following command to do that.
# /opt/puppetlabs/bin/puppet agent --test

Step 4: Install and Configure Lamp Components
We will now use Ansible to automate the installation and configuration of Lamp components on the VM. We will create Ansible playbooks for installing webserver, MariaDB, PHP, creating a database, creating a table, copying web pages, and restarting the web service.
$ ansible-playbook webserver-installation.yml
$ ansible-playbook mariadb-installation.yml
$ ansible-playbook php-installation.yml
$ ansible-playbook create-database.yml -e "dbname=jobsearch"
$ ansible-playbook create-table.yml
$ ansible-playbook copy-web-pages.yml

$ansible-playbook copy-db-update.yml
$ ansible-playbook webserver-installation.yml --tags "Restart Webservice"

Step 5: Delete the VM
Once the automation is complete, we can delete the VM using the virsh command. We will also revoke
# virsh shutdown web01
# virsh shutdown db01
# virsh undefine web01 --remove-all-storage
# virsh undefine db01 --remove-all-storage

Step 6: Login to puppet servers and revoke web01 and db01 agents.
# puppetserver ca revoke --certname web01
# puppetserver ca clean --certname web01
# puppetserver ca revoke --certname db01
# puppetserver ca clean --certname db01


Github Link for code : 

https://github.com/vdarole/ansible/tree/main/web-db

No comments:

Post a Comment