Ansible PlayBook to Launch Web-Server on Docker Container

Ansible

Ansible is an open-source software provisioning, configuration management, and application-deployment tool enabling infrastructure as code. It runs on many Unix-like systems and can configure both Unix-like systems as well as Microsoft Windows. It includes its own declarative language to describe system configuration. Ansible was written by Michael DeHaan and acquired by Red Hat in 2015. Ansible is agentless, temporarily connecting remotely via SSH or Windows Remote Management (allowing remote PowerShell execution) to do its tasks.

Ansible PlayBook

Playbooks are Ansible’s configuration, deployment, and orchestration language. They can describe a policy you want your remote systems to enforce or a set of steps in a general IT process. If Ansible modules are the tools in your workshop, playbooks are your instruction manuals, and your inventory of hosts is your raw material. At a basic level, playbooks can be used to manage configurations of and deployments to remote machines. At a more advanced level, they can sequence multi-tier rollouts involving rolling updates and can delegate actions to other hosts, interacting with monitoring servers and load balancers along the way. While there’s a lot of information here, there’s no need to learn everything at once. You can start small and pick up more features over time as you need them. Playbooks are designed to be human-readable and are developed in a basic text language. There are multiple ways to organize playbooks and the files they include, and we’ll offer up some suggestions on that and making the most out of Ansible.

Ansible Docs - https://docs.ansible.com/

Docker

Docker is a set of the platform as service products that use OS-level virtualization to deliver software in packages called containers. Containers are isolated from one another and bundle their own software, libraries, and configuration files; they can communicate with each other through well-defined channels. Developing apps today requires so much more than writing code. Multiple languages, frameworks, architectures, and discontinuous interfaces between tools for each lifecycle stage create enormous complexity. Docker simplifies and accelerates your workflow while giving developers the freedom to innovate with their choice of tools, application stacks, and deployment environments for each project.

Docker Docs - https://docs.docker.com/

Apache HTTP Server

The Apache HTTP Server, colloquially called Apache, is a free and open-source cross-platform web server software, released under the terms of Apache License 2.0. Apache is developed and maintained by an open community of developers under the auspices of the Apache Software Foundation.

Apache HTTP Docs - https://httpd.apache.org/docs/

Task

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 webserver

  • Install ansible in Controller node and check it using “ansible — version” command.
  • Open an editor to start writing the playbook using the “vim <filename>.yml” command.

Note: Click here for the Github link of code.

- hosts: dockerhost
gather_facts: false
tasks:
- name: Creating Yum Repository for Docker
yum_repository:
name: docker
description: Docker Repo
file: /etc/yum.repos.d/docker
baseurl: https://download.docker.com/linux/centos/7/x86_64/stable
gpgcheck: no
enabled: true
- name: Docker Package Installation
command: "yum install docker-ce --nobest -y"
- name: Install Software Python36
package:
name: python3
state: present
- name: Install Docker Python Library
pip:
name: docker-py
- name: Creating WorkSpace
file:
path: /root/WS
state: directory
- name: Copying Content Into Managed Node
copy:
content: "Hello World!! Task 10 By Karan Agrawal"
dest: "/root/WS/index.html"
ignore_errors: yes
- name: Starting Docker Service
service:
name: docker
state: started
- name: Pull an Image
docker_image:
name: httpd
source: pull
- name: Creating Docker Container
docker_container:
name: webseveros1
image: httpd
state: started
detach: true
interactive: true
ports:
- "1234:80"
volumes:
- /root/WS:/usr/local/apache2/htdocs/
  • Now write IP’s of Managed Nodes in the Inventory file.
  • Also setup ansible.cfg file.
  • Now we are all set to run the playbook so now run the playbook.

We have successfully completed the task by running the playbook. We can verify it by seeing the Managed Node.

Also, we can access the website on the 1234 port of Managed Node’s IP.

--

--

--

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

Recommended from Medium

Create Productivity App in Python from Scratch— Part 1

Woman with a laptop sitting on a chair.

Mediator design pattern in modern applications

Using a reverse proxy with your web application

Hacktober — Recap

13 factors that can ruin your daily team meeting

The code of Digital Barometric Pressure Sensor

Sinatra, Physicians, and Appointments

My First Python and MySQL Database

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
Karan Agrawal

Karan Agrawal

More from Medium

SSH to Red Hat with Docker

VPN Chaining with Wireguard

Developing apps with docker

Automation — let’s sail together. Terraform Hello World on Windows 10