Config Apache webserver in container using ansible.

Shobhit Singh Pal
3 min readDec 23, 2020

--

Write an Ansible PlayBook that does the following operations in the managed nodes:

  • Configure Docker
  • Start and enable Docker services
  • Pull the httpd server image from the Docker Hub
  • Run the docker container and expose it to the public
  • Copy the html code in /var/www/html directory and start the web server

With these operations our apache webserver container will set up.

So, let’s go step by step:

Step 1: Configure docker

Configure docker simply means install the docker. In ansible playbook to install any package/software the module is package.

On ami instance the package module works and install the docker community edition.

But in local redhat8 VM , sometimes the yum is not configured for docker. So need to install the repo for the docker and then install it. When we install the docker we get error with package module. To resolve error need to pass the “no best” option in following way:

yum install docker-ce --nobest -y

But there is not way to pass the “no best” option in package so it can not be used. To run the above box command, command module is there:

command: "yum install docker-ce --nobest -y"

Code

Step 2: Start and enable Docker services

  • start docker services with service module

Docker need docker-py libary to work with ansible otherwise error will occur for docker-py.

Step 3: Pull the httpd server image from the Docker Hub

  • docker_image module to pull image.

Step 4:Run the docker container and expose it to the public

Here exposed_ports will expose the 80 port of docker container and state parameter with started value will start the docker container.

Step 5: Copy the html code in /var/www/html directory and start the web server.

or git module can be used as

- git:
repo: GIT_REPO
dest: /var/www/html/index.html

Playbook:

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Shobhit Singh Pal
Shobhit Singh Pal

No responses yet

Write a response