Ansible Playbook
Last updated
Last updated
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
Let us launch a new Linux EC2 instance with the same settings as the previous one.
Connect to the instance using Putty or Instance connect.
We want to establish a connection between this server and our Application Server where we have docker installed. To do that :
run sudo su _
to login as root.
Now edit the sshd_config file using vi /etc/ssh/sshd
_config
and change PasswordAuthentication no to PasswordAuthentication yes.
service sshd reload
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.
Once Logged in :
run sudo su -
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.
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
Ansible is a simple open-source software automation platform in Python so to install Ansible first we have to install python :
sudo su -
yum install python
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
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.
Now delete all the docker containers and images because we will be creating our ANSIBLE playbook in YAML file.
To create Ansible we will be using https://codebeautify.org/yaml-validator to write Playbook with correct YAML syntax
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
Now we can start running command to execute this playbook :
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:
In Dashboard, Select your Job
Configure>>Post-build actions (SSH section) paste the above code
APPLY and save.
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.
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
This command will execute all the instructions in the playbook step by step. Output should be like this