DevOps 6.2: Grand Finale of Scripts
Check the implementation of Scripts in multi-os web setup
How to run a multi-OS web setup using a single script file?
Steps:
cd /opt/scripts
mkdir remote_websetup
cd remote_websetup
.vim remhosts
Paste the below into the file
web01 web02 web03
Run the following command -
for host in cat remhosts; do ssh devops@$host sudo yum install git -y;done
.Create
vim remoteweb_os.sh
Paste the below code -
#!/bin/bash # Variable Declaration #PACKAGE="httpd wget unzip" #SVC="httpd" URL='https://www.tooplate.com/zip-templates/2098_health.zip' ART_NAME='2098_health' TEMPDIR="/tmp/webfiles" yum --help &> /dev/null if [ $? -eq 0 ] then # Set Variables for CentOS PACKAGE="httpd wget unzip" SVC="httpd" echo "Running Setup on CentOS" # Installing Dependencies echo "########################################" echo "Installing packages." echo "########################################" sudo yum install $PACKAGE -y > /dev/null echo # Start & Enable Service echo "########################################" echo "Start & Enable HTTPD Service" echo "########################################" sudo systemctl start $SVC sudo systemctl enable $SVC echo # Creating Temp Directory echo "########################################" echo "Starting Artifact Deployment" echo "########################################" mkdir -p $TEMPDIR cd $TEMPDIR echo wget $URL > /dev/null unzip $ART_NAME.zip > /dev/null sudo cp -r $ART_NAME/* /var/www/html/ echo # Bounce Service echo "########################################" echo "Restarting HTTPD service" echo "########################################" systemctl restart $SVC echo # Clean Up echo "########################################" echo "Removing Temporary Files" echo "########################################" rm -rf $TEMPDIR echo sudo systemctl status $SVC ls /var/www/html/ else # Set Variables for Ubuntu PACKAGE="apache2 wget unzip" SVC="apache2" echo "Running Setup on CentOS" # Installing Dependencies echo "########################################" echo "Installing packages." echo "########################################" sudo apt update sudo apt install $PACKAGE -y > /dev/null echo # Start & Enable Service echo "########################################" echo "Start & Enable HTTPD Service" echo "########################################" sudo systemctl start $SVC sudo systemctl enable $SVC echo # Creating Temp Directory echo "########################################" echo "Starting Artifact Deployment" echo "########################################" mkdir -p $TEMPDIR cd $TEMPDIR echo wget $URL > /dev/null unzip $ART_NAME.zip > /dev/null sudo cp -r $ART_NAME/* /var/www/html/ echo # Bounce Service echo "########################################" echo "Restarting HTTPD service" echo "########################################" systemctl restart $SVC echo # Clean Up echo "########################################" echo "Removing Temporary Files" echo "########################################" rm -rf $TEMPDIR echo sudo systemctl status $SVC ls /var/www/html/ fi
Run the script file to see it in action.
Push the above script to all the connected VMs using another script -
vim webdeploy.sh
do and paste the below code.#!/bin/bash # Define the username for connecting to remote hosts USR='devops' # Iterate through the list of hosts from the file "remhosts" for host in `cat remhosts` do echo echo "#########################################################" echo "Connecting to $host" echo "Pushing Script to $host" # Copy the script "multios_websetup.sh" to the remote host's /tmp/ directory scp multios_websetup.sh $USR@$host:/tmp/ echo "Executing Script on $host" # Execute the script on the remote host using SSH and sudo ssh $USR@$host sudo /tmp/multios_websetup.sh # Remove the script from the remote host's /tmp/ directory after execution ssh $USR@$host sudo rm -rf /tmp/multios_websetup.sh echo "#########################################################" echo done
And then run the script to see it in action.
You can check the website by accessing the IP address of the VMs from
/etc/host
.
ย