Showing posts with label Bash script. Show all posts
Showing posts with label Bash script. Show all posts

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

How to write Bash Scripts

 







Bash scripting is an extremely useful and powerful part of system administration and development. It might seem extremely scary the first time you do it, but hopefully this guide will help ease the fear.

Bash is a Unix shell, which is a command line interface (CLI) for interacting with an operating system (OS). Any command that you can run from the command line can be used in a bash script. Scripts are used to run a series of commands.

Bash is available by default on Linux and macOS operating systems.

This is not meant to be an extensive guide to bash scripting, but just a straightforward guide to getting started with making your first script, and learning some basic bash syntax.

Note: Newer macOS installations (from Catalina) come installed with zsh (Z shell) as the new default, but everything in this article will still be applicable.Prerequisites

  • A basic command line knowledge is required. Everything you need to know to get started can be found in my How to Use the Command Line article.

This guide was created on macOS, and will be using /Users/you as the default user directory for all examples. However, the concepts here will apply to any Unix-like operating system, including macOS and various Linux distributions.

Goals

In this tutorial, we're going to:

Create Your First Script

Making a bash script is a lot simpler than you might think.

Create a file called hello-world, using the touch command.

touch hello-world

Edit the file with the program of your choice. Within the file, print a string that says "Hello, world!' using echo.

hello-world
echo "Hello, world!"

Now from the command line, run the script using the bash interpreter:

bash hello-world

You'll see the script has run successfully from the output.

Hello, world!

That's it, you've created your first script!

Executable Scripts

So far, you've learned how to run a script from the command line prefixed with the bash interpreter. However, if you want to run the script by name alone, it won't work. Try to run the file simply by typing the name of the file and pressing enter. Note that we're prefixing the file with ./, which means a file in the current directory.

./hello-world
bash: ./hello-world: Permission denied

In order to run a file directly, we'll need to change the permissions to allow the script to be executable for the user. chmod is a command that changes permissions on a file, and +x will add execute rights to the script.

chmod +x hello-world

In order to interpret the file as an executable, you'll also have to add the shebang (#!) at the top of the script. In Unix-like systems, a text file with a shebang is interpreted as an executable file. You can confirm where the bash interpreter is located with which bash.

which bash
/bin/bash

We'll add #!/bin/bash to the top of the script.

#!/bin/bash

echo "Hello, world!"

Note: You may also see #!/usr/bin/env bash instead, which can be used if you don't know the exact path for bash.

Now you can run hello-world directly.

./hello-world
Hello, world!

Note: In order to run a bash script without specifying the directory (using ./, for example) you would have to add the directory of the script to the PATH by running export PATH=$PATH:/path/to/script/directory. However, this is generally not necessary for personal scripts.

 Strings

A simple string in Bash does not require double quotes - you can write it directly.

echo Just a regular string
Just a regular string

A single or double quote will expect a closing match, so in order to use one in such a string, you would need to escape the quote.

echo I\'m a string
I'm a string

However, if you want to use a single or double quote in a string without escaping characters, you can do so by wrapping your string in quotes.

echo 'A single quoted "string"'
echo "A double quoted 'string'"
A single quoted "string"
A double quoted 'string'

With the -e flag, bash will interpret strings with backslash-escaped characters, such as \n for newline. This also requires a quoted string.

echo -e "This string has a \nnew line"
This string has a
new line

Double quoted strings are also important for use with variables, as we'll see in the next section.

Variables

variable is declared without a dollar sign ($), but has one when invoked. Let's edit our hello-world example to use a variable for the entity being greeted, which is World.

hello-world
#!/bin/bash

who="World"

echo "Hello, $who!"
Hello, World!

Note that who = "World" with a space between the assignment is not valid - there must not be a space between variable and value.

Double quoted strings are required for interpolating variables. Within a single quoted string, the dollar sign would be interpreted literally

echo 'Hello, $who!'
Hello, $who!

Another way you might see variables written is surrounded by curly brackets along with the dollar sign, which is known as parameter expansion.

echo "Hello, ${who}!"

This syntax is necessary for anything more complex you might do with a variable, such as getting one item from an array.

Shell Execution

If you would like to use the output of a shell execution within a string, you can do so with a dollar sign followed by parentheses. ($()). For example the whoami command will print out your current user. To use it within a string, wrap whoami in the shell execution syntax.

echo "Hello, $(whoami)!"
Hello, taniarascia!

User Input

We declared a variable in the last example, but we can also have the user set the value of a variable dynamically. For example, instead of just having the script say Hello, World!, we can make it ask for the name of the person calling the script, then output that name. We'll do this using the read command.

hello-world
#!/bin/bash

echo 'Who are you?'

read who

echo "Hello, $who!"
Who are you?
> Tania
Hello, Tania!

Comparison

Operators are slightly different in bash than what you might be used to.

In order to compare numbers, you will use the operators in the number comparison column, such as -lt for less than.

In order to compare strings, you will use the operators in the string comparison column, such as < for less than.

This is the opposite of what you might expect, but it's the way it works in bash.

Number ComparisonString ComparisonDescription
-eq==Equal
-ne!=Not equal
-gt>Greater than
-ge>=Greater than or equal
-lt<Less than
-le<=Less than or equal

You can also use -z to test for emptiness on a string.

Conditions

if statements use the ifthenelse, and fi keywords. The condition goes in square brackets.

check-id
#!/bin/bash

echo 'How old are you?'

read age

if [ $age -gt 20 ]
then
    echo 'You can drink.'
else
    echo 'You are too young to drink.'
fi
How old are you?
> 30
You can drink.

Loops

Bash uses forwhile, and until loops. In this example, I'll use the for...in loop to get all the files in a directory and list them.

list-files
#!/bin/bash

files=/Users/you/dev/*

for file in $files
do
  echo $(basename $file)
done
hello-world check-id list-files

Arrays

An array in bash is defined inside parentheses. There are no commas between the items of the array.

beatles=('John' 'Paul' 'George' 'Ringo')

To access an item from an array, you'll use square brackets ([]). Arrays are 0-indexed in bash. It is also necessary to use the paramter expansion syntax.

echo ${beatles[3]}
Ringo

How to upgrade Maven

  java.lang.IllegalStateException I had installed maven in my ubuntu using command  apt install maven This installed maven in path /usr/shar...