Webserver configuration on EC2 Instance with Docker and Ansible

  1. AWS Management Console
  2. AWS CLI
  3. API

What are the benefits of storing credentials in ansible-vault?

  • You can secure your credentials with a file and because it is
    encrypted so we can share it with our team.
  • We can pass a reference of variable in the playbook so that we can use it where the authentication is required.
ansible-vault create cred.yml
  • The first play will be executed in localhost where this play is using the cred.yml file for variables. ( This file is encrypted with ansible-vault)
  • The tasks the first task is to launch ec2 instance over AWS cloud for this amazon.aws.ec2 module uses API to connect our system with AWS cloud. As we have to give credentials we are passing variable access_key and secret_key along with some details which are essential to launching instances over the cloud.
  • In the second task, the wait_for module will hold the task till ssh connection with launched instances will establish.
  • ec2_instance_info module used for fetching all instance information along with newly launched instances to store it into variable named x
  • debug module is used to verify whether the information is stored in x or not.
  • The most critical part in the playbook is updating existing inventory so for this we are using blockinfile module along with some conditions.
  • To use this instance for further configuration add_host will add a temporary host with the group name dockerhosts.
  • yum repository will configure yum for docker-ce (community edition of docker)
  • The next stage is to install docker but if we use the package module it will not install some dependencies which will conflict in the future so a better way is to use the command module with — nobest option.
  • Next is starting service with a service module
  • docker_image module requires a request module in python so to solve these dependencies we installed python and with the help of pip, we ensure that this module will be available before we run play to fetch the image.
  • docker_image will pull image named as yash202000/webserver-php
  • copy module will copy the index.html file to the remote server.
  • Once again we used the command module to launch a container with webserver-php image because docker_container will only launch it and after detaching it will stop which means it will not run in the background so to overcome this we are using the command module.
  • Last but not least copy content to docker container so we used command module with docker cp command.


  1. Mail: yashpanchwatkar@gmail.com
  2. LinkedIn: https://www.linkedin.com/in/yash-panchwatkar/




ARTH - Learner

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Building Enterprise Apps with Unity

Thoughts about naming variables and methods

Ruby On Rails Today

How to build an automated holiday tracker with google sheets & forms

One Link for All Forms

[Django] AWS EC2에 서버 배포하기(2)

CS373 Spring 2022: Sanchith Shanmuga

Cordova and APK Tampering

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Yash Panchwatkar

Yash Panchwatkar

ARTH - Learner

More from Medium

Beginners: Docker & Kubernetes

Use a Script to install and launch an Apache Server on CentOS 8

How to install docker on RHEL using Ansible role