Automation of Lamp on same server.

Introduction:

In this blog, we will discuss the automation of a lamp project on the same server. The lamp project includes the installation of webserver, MariaDB, PHP, creating a database, creating a table, copying web pages, and restarting the web service. We will be using Ansible for automation.

Building server within 15min 

Prerequisites:

Before starting the automation, we need to have a server with Rocky Linux 8 operating system installed on it. We will also need Ansible installed on the control machine.

Step 1: Clone the VM

The first step is to clone the VM using the virt-clone command on kvm01. The command will create a new VM named lamp01 using the original VM temp-vm1.

[root@kvm01 ~]# virt-clone --connect qemu:///system --original temp-vm1 --name lamp01 -f /kvmstore/lamp01.img 
[root@kvm01 ~]# virsh start lamp01

Step 2: Configure the VM

Next, we need to configure the VM. We will change the hostname, IP address, disable SELinux and firewall, and reboot the VM.

[root@backup01 ~]# hostnamectl set-hostname lamp01.darole.org 
 [root@backup01 ~]# sed -i 's/192.168.2.219/192.168.2.211/g' /etc/sysconfig/network-scripts/ifcfg-enp1s0 
[root@backup01 ~]# sed -i 's/enforcing/disabled/g' /etc/selinux/config
 [root@backup01 ~]# systemctl disable firewalld 
[root@backup01 ~]# reboot

Step 3: Install Puppet Agent

After configuring the VM, we will install the Puppet agent on the VM. We will copy the Puppet agent installation script from kvm01 to the lamp01 VM and run it.

[root@kvm01 ~]# ssh-copy-id lamp01 
[root@kvm01 ~ ]# scp /root/script/puppet-agent-installation.sh lamp01:/root/ 

[root@lamp01 ~]# sh puppet-installation.sh

Step 4: Sign the Puppet Certificate

Next, we need to sign the Puppet certificate of the VM on the Puppet server.

[root@pup01 ~]# puppetserver ca list 
[root@pup01 ~]# puppetserver ca sign --all

Step 5: Run the Puppet Agent

After signing the Puppet certificate, we will run the Puppet agent on the VM.

[root@lamp01 ~]# /opt/puppetlabs/bin/puppet agent --test

Step 6: 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@ans01 lamp01]$ ssh lamp01 
[ansible@ans01 ~]$ cd lamp01/ 
[ansible@ans01 lamp01]$ ansible-playbook webserver-installation.yml 
[ansible@ans01 lamp01]$ ansible-playbook mariadb-installation.yml 
[ansible@ans01 lamp01]$ ansible-playbook php-installation.yml 
[ansible@ans01 lamp01]$ ansible-playbook create-database.yml -e "dbname=jobsearch" 
[ansible@ans01 lamp01]$ ansible-playbook create-table.yml 
[ansible@ans01 lamp01]$ ansible-playbook copy-web-pages.yml 
[ansible@ans01 lamp01]$ ansible-playbook webserver-installation.yml --tags "Restart Webservice"

Step 7: Delete the VM

Once the automation is complete, we can delete the VM using the virsh command. We will also revoke

[root@kvm01 ~]# virsh shutdown lamp01
[root@kvm01 ~]# virsh undefine lamp01 --remove-all-storage 

[root@pup01 ~]# puppetserver ca revoke --certname lamp01 
[root@pup01 ~]# puppetserver ca clean --certname lamp01


Github Link for code : 

https://github.com/vdarole/ansible/tree/main/lamp01


No comments:

Post a Comment