Continuous Integration and Continuous Deployment Using Ansible , AWS EC2 and Docker


Tools Used:
1. Git
2. Jenkins
3. Ansible
4. Docker
5. AWS EC2

How it works:
Git commit -> Git Push -> Automatic trigger Jenkins Job -> Run Ansible Playbook -> Launch EC2 instance -> Launch Docker in EC2 -> Setup web server inside docker -> Run webserver

On ansible
Yum install epel-release ansible
Yum install python-pip
Pip install –upgrade pip
Pip install boto

Setup GIT server (make ssh password less authentication between Jenkins user (Jenkins) and git user(git). i.e., Jenkins user from Jenkins server should be able to connect git repository server with out any password.
yum install git
su – git
cat /root/.ssh/ >> /home/git/.ssh/authorized_keys

(on git server)
mkdir /git
mkdir /git/project.git

cd /git/project.git
git init –bare
chmod -R 777 /git/project.git/objects
chmod -R 777 /git/project.git/refs

(on git client)
mkdir /project
cd /project
git init
git config –global “git”
git config –global “”

touch sample.txt
git add .
git commit -m ‘some comment’
git remote add origin
git push origin master

git add .
git commit -m ‘my change’
git push origin master

1. Install Ansible, Jenkins. Configure Jenkins to use SSH for trigger and Ansible for build

mkdir /etc/ansible/playbooks
mkdir /etc/ansible/playbooks/vars

2. Use the below url for Ansible script to launch dockers in EC2…r-and-launch-ec2/


cp /etc/ansible/playbooks/ec2_docker.yml /myproject/ (do some change in the script)
cd /myproject/
git add .
git commit -m ‘change’
git push origin master

You can see in Jenkins UI that the job will trigger automatically

Cat /dockerfiles/
#Download centos base image
FROM centos:7

#Update software repository
RUN yum -y install net-tools

ENV container docker
RUN (cd /lib/systemd/system/; for i in *; do [ $i == \
systemd-tmpfiles-setup.service ] || rm -f $i; done); \
rm -f /lib/systemd/system/*;\
rm -f /etc/systemd/system/*.wants/*;\
rm -f /lib/systemd/system/*; \
rm -f /lib/systemd/system/*udev*; \
rm -f /lib/systemd/system/*initctl*; \
rm -f /lib/systemd/system/*;\
rm -f /lib/systemd/system/*;
VOLUME [ “/sys/fs/cgroup” ]
CMD [“/usr/sbin/init”]

#print hello world

RUN mkdir /mydir

COPY jdk.tar.gz /mydir
RUN cd /mydir
RUN tar -xvf /mydir/jdk.tar.gz

ENV JAVA_HOME /jdk1.8.0_77

Cat /dockerfiles/dockerfile.web

FROM rawdocker

RUN yum -y install httpd
RUN rm -rf /etc/httpd/logs
RUN mkdir /etc/httpd/logs
RUN systemctl enable httpd.service
CMD [“/usr/sbin/init”]

For remote ssh, ansible use “execute remote ssh scripts” . so create shell script with ansible-playbook command and run the script
(make sure the key pem file permissions are 0400)

execute a script or command remotely from jenkins.

                                                                                        By Madhu R



One Reply to “Continuous Integration and Continuous Deployment Using Ansible , AWS EC2 and Docker”

Leave a Reply

Your email address will not be published. Required fields are marked *