⚙️
DevOps Mastery on AWS
  • 🤓Continuous Integration & Continuous Deployment on AWS Cloud
  • 🤖Jenkins
    • 🛠️Build Jenkins First Job
    • 🦸Launch 2nd EC2 instance
  • 🐳Docker
    • 📁DOCKERFILE
  • 🛺Ansible
    • 📖Ansible Playbook
  • 🏃‍♂️Continuos Integration with GitHub and Jenkins
  • 👷Project Structure & summary.
  • Basics
Powered by GitBook
On this page
  • SSH CONNECTION>
  • Creating Ansible Playbook:
  1. Ansible

Ansible Playbook

PreviousAnsibleNextContinuos Integration with GitHub and Jenkins

Last updated 3 years ago

Before we actually create Playbook there are few tasks we need to accomplish which are necessary for Playbook to work properly.

  • SSH CONNECTION

  • HOST FILES

SSH CONNECTION>

  1. Let us launch a new Linux EC2 instance with the same settings as the previous one.

  2. Connect to the instance using Putty or Instance connect.

  3. We want to establish a connection between this server and our Application Server where we have docker installed. To do that :

    1. run sudo su _ to login as root.

    2. Now edit the sshd_config file using vi /etc/ssh/sshd_config and change PasswordAuthentication no to PasswordAuthentication yes.

    3. service sshd reload

    4. passwd ec2-user (set password we will require it)

    Now we have to connect to the Application server EC2 instance to create SSH keys so we can communicate between servers.

  4. Once Logged in :

    1. run sudo su -

    2. ssh-keygen //press enter

You will see output like this and if you ls -a you can see a .ssh folder with these files

We have successfully created ssh keys as you can see the idrsa.pub is the public key whereas id_rsa is private key which should not be shared to anyone. We will transfer the Public key (.pub) to the newly created server to establish ssh connection between them.

ssh-copy-id ec2-user@<ipaddress>
ssh ec2-user@<ipaddress>  //to login
exit    //to exit

wow!! we are connected to other server using application server. Superb isn't it!

Now let's follow the same steps to copy public key in our localhost

ssh-copy-id ec2-user@localhost

Atlast Now we are all set to create Ansible Playbook, Let's Install Ansible in our application server

  1. Ansible is a simple open-source software automation platform in Python so to install Ansible first we have to install python :

    1. sudo su -

    2. yum install python

    3. pip install ansible // pip is packet manage for python !!!! If you are using EC2 you might have python already installed and pip won't work so run this command: sudo amazon-linux-extras install ansible2

    4. run ansible --version // if you see the version or output like this you have ansible installed in your Application server.

    We have ansible installed and everything is ready but one thing you should always remember is we are in root mode, we should never operate as root when working with other tools like jenkins or ansible. But our ec2-user do not have permission so we will give ec2-user permission by running echo "ec2-user ALL=(ALL)NOPASSWD:ALL">>/etc/sudoers command.

Creating Ansible Playbook:

---
- hosts: all
  tasks: 
  - name:Stop the running container
    command: docker stop tomcatcontainer 
    ignore_errors: True
    
  - name:remove the running container
    command: docker rm tomcatcontainer 
    ignore_errors: True
    
  - name:removing the running image
    command: docker rmi tomcatimage 
    ignore_errors: True
    
  - name: Create the image from DockerFile
    command: docker build -t tomcatimage .
    
  - name: Create and run Tomcat container
    command: docker run -d -p 8080:8080 --name tomcatcontainer tomcatimage

This is YAML code for our playbook but how will ansible know which server to execute playbook? if you see the first line of above YAML Playbook file - hosts: all we have declared to all hosts but where is the list?? so we will create hosts file in home directory by running these commands

vi hosts //(vim editor will open <press i> localhost <esc+wq>)
vi myplaybook.yml  //(vim editor will open <press i> COPY PASTE above CODE <esc+wq>)

Now we can start running command to execute this playbook :

ansible-playbook -i hosts myplaybook.yml

Congratulations!!! We have automated so much but we can do more. Let us order Jenkins to run this command directly rather than we writing in terminal. Interesting isn't it?

Log in to Jenkins and follow these tasks:

  1. In Dashboard, Select your Job

  2. Configure>>Post-build actions (SSH section) paste the above code

  3. APPLY and save.

  4. Now Click on "Build Now" and you will get your playbook run from Jenkin!!!

SUPERBB!! We have come so Far!! Let's Recap what we have achieved till now:

  • Storing our source code on GIT

  • Hosting Jenkins & Docker on AWS EC2 server

  • Passing the code to JENKINS

  • Jenkins creating MAVEN build file

  • Pulling and Building images on DOCKER, running Tomcat server on 8080:8080 port

  • Creating DOCKERFILE to automate Docker tasks

  • Installing ANSIBLE

  • Creating Ansible PLAYBOOK to automate docker tasks and copy pasting Build file on Tomcat server

  • Automating More by running ansible-playbook command in JENKINS. Now if we click on Build Now our application will be live without us interacting with the terminal.

Summary :

Using Ansible we can write playbooks that can run automation scripts with a set of commands on any machine.

You can deploy your code into n number of machines by having IP Address list in hosts file and running Ansible deployment playbook YAML file

Advantage of deploying Apps in Docker Containers is that we need not worry about existing software installations and their version compatibility

We can create a Deployment environment in docker file so that it creates an image out of it and deploy in a container in any machine instantly without worrying about downloading the software manually

Deploying Apps using Ansible + Docker on Linux and automating the Continuous Delivery process with Jenkins is the powerful Process followed in Devops World.

In the Next Section we shall see How to Integrate Git with Jenkins to achieve Continuous Integration.

Enter your public ip address and yes if prompted >> password and you will be connected.

Now lets ssh into the other server from this server by the command as shown in the image

Now delete all the docker containers and images because we will be creating our in file.

To create Ansible we will be using to write Playbook with correct YAML syntax

This command will execute all the instructions in the playbook step by step. Output should be like this

🛺
📖
👆
ANSIBLE playbook
YAML
https://codebeautify.org/yaml-validator