Friday, 2 October 2020

Automation- How to Automate Awx/Tower installation using bash script in Ubuntu 18

 Goal of Automation

In your role as a Devops Engineer, you may find yourself repeating many processes. Especially when it comes to infrastructure configuration and server provisioning. In the SDLC there may be many repeated actions that drive the process, Automation helps to facilitate efficient process workflow as well as save time, money and resources. Automation is about taking once manual processes and placing technology around them so they're inherently repeatable.

In our previous lab we learnt how to install Awx in Ubuntu 18:

https://violetstreamstechnology.blogspot.com/2020/09/what-is-ansiblehow-to-install-awx.html

This process takes about 15mins to complete and involves entering close to 30 linux commands.

This will be a real pain if we are to use this method to install like 50 servers. To solve this problem we will use bash script. Go to Bash Script to learn how to create one.

Step 1: Launch an Ubuntu instance using Ansible: See Previous Tutorial or from Aws Console

Open Port 80,8000,9000

Step2: Log into the instance using Mobaxterm

Step 3: Create A new file called app.sh



Step 4: Right click on the file and Open with default editor



Step 5: Copy the below Bash Script and paste in the window
#!/bin/bash
##########################System Requirements for AWX Server##########################
##########################2GB of memory###############################################
##########################2 cpu cores#################################################
##########################20GB of space###############################################
sudo apt -y update
sudo apt -y install apt-transport-https ca-certificates curl gnupg-agent software-properties-common git
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
echo "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list
echo "#########################################################################################################1"
sudo apt -y update
echo "#########################################################################################################2"
sudo apt -y install docker-ce docker-ce-cli containerd.io
echo "#########################################################################################################3"
sudo usermod -aG docker $USER
echo "#########################################################################################################4"
newgrp docker << END
echo "#########################################################################################################5"
docker version
curl -s https://api.github.com/repos/docker/compose/releases/latest \
| grep browser_download_url \
| grep docker-compose-Linux-x86_64 \
| cut -d '"' -f 4 \
| wget -qi -;
###Docker compose-Linux-x86_64
chmod +x docker-compose-Linux-x86_64
sudo mv docker-compose-Linux-x86_64 /usr/local/bin/docker-compose
docker-compose version
docker volume create portainer_data 
docker run -d -p 8000:8000 -p 9000:9000 --name=portainer --restart=always -v /var/run/docker.sock:/var/run/docker.sock -v portainer_data:/data portainer/portainer
echo "deb http://ppa.launchpad.net/ansible/ansible/ubuntu bionic main" | sudo tee /etc/apt/sources.list.d/ansible.list
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 93C4A3FD7BB9C367
sudo apt -y update
sudo add-apt-repository ppa:ansible/ansible-2.9
ansible --version
sudo apt install -y nodejs
sudo apt install -y npm 
sudo apt install -y python3-pip git pwgen vim
pip3 install requests==2.14.2
pip3 install docker-compose==1.25.5  
pip3 install --upgrade pip cffi && \
pip3 install ansible && \
pip3 install mitogen ansible-lint && \
pip3 install --upgrade pywinrm 
END
#sudo su -
#sudo su << END
#check for root
UID=$(id -u)
if [ x$UID != x0 ] 
then
    #Beware of how you compose the command
    printf -v cmd_str '%q ' "$0" "$@"
    exec sudo su -c "$cmd_str"
fi

#I am root

git clone --depth 1 --branch 17.1.0 https://github.com/ansible/awx.git
cd awx/installer/
echo "#######################################################################################################################"
echo "################################################################################################################3"
echo "  ¦¦¦¦¦+ ¦¦¦+   ¦¦+¦¦¦¦¦¦¦+¦¦+¦¦¦¦¦¦+ ¦¦+     ¦¦¦¦¦¦¦+    ¦¦¦¦¦¦¦¦+ ¦¦¦¦¦¦+ ¦¦+    ¦¦+¦¦¦¦¦¦¦+¦¦¦¦¦¦+ "
echo " ¦¦+--¦¦+¦¦¦¦+  ¦¦¦¦¦+----+¦¦¦¦¦+--¦¦+¦¦¦     ¦¦+----+    +--¦¦+--+¦¦+---¦¦+¦¦¦    ¦¦¦¦¦+----+¦¦+--¦¦+"
echo " ¦¦¦¦¦¦¦¦¦¦+¦¦+ ¦¦¦¦¦¦¦¦¦¦+¦¦¦¦¦¦¦¦¦++¦¦¦     ¦¦¦¦¦+         ¦¦¦   ¦¦¦   ¦¦¦¦¦¦ ¦+ ¦¦¦¦¦¦¦¦+  ¦¦¦¦¦¦++"
echo " ¦¦+--¦¦¦¦¦¦+¦¦+¦¦¦+----¦¦¦¦¦¦¦¦+--¦¦+¦¦¦     ¦¦+--+         ¦¦¦   ¦¦¦   ¦¦¦¦¦¦¦¦¦+¦¦¦¦¦+--+  ¦¦+--¦¦+"
echo " ¦¦¦  ¦¦¦¦¦¦ +¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦++¦¦¦¦¦¦¦+¦¦¦¦¦¦¦+       ¦¦¦   +¦¦¦¦¦¦+++¦¦¦+¦¦¦++¦¦¦¦¦¦¦+¦¦¦  ¦¦¦"
echo " +-+  +-++-+  +---++------++-++-----+ +------++------+       +-+    +-----+  +--++--+ +------++-+  +-+"
echo "All is setup and done!\
For access Ansible Tower go to: ip address\
Complete manual ----edit the inventory file and install playbook"

Step 6: Save.....Select Yes to All

Step 7: Make the script executable- In the command prompt 
enter: chmod +x app.sh


Step 8: Run the bash script- enter ./app.sh
If you face errors like 
/bin/bash^M: bad interpreter: No such file or directory

Enter
sed -i -e 's/\r$//' app.sh
Then Run the script again
enter ./app.sh


Final step will be to configure: inventory file and run install.yml. Please see https://violetstreamstechnology.blogspot.com/2020/09/what-is-ansiblehow-to-install-awx.html for reference.
The final steps can also be automated . This will be tackled in a later tutorial

No comments:

Post a Comment