Ansible Playbook : Web Server Deployment using docker

Ankit Shukla
4 min readJul 31, 2023

--

Ansible + Docker+Apache webserver

Greetings and best wishes for an insightful read😇 ,

Hope this blog finds you great and healthy !!😊

Lets begin with brief introduction about me…..

Hi, This is Ankit Shukla and In this article we will discuss about Ansible Playbook to deploy web server and leverage docker as tool.

Ansible Playbook:

An Ansible playbook is a configuration management tool used to define, organize, and execute a series of tasks on remote hosts. It allows system administrators and developers to automate the deployment, configuration, and management of systems and applications.

Playbooks are written in YAML (YAML Ain’t Markup Language) format, which makes them human-readable and easy to understand.

A playbook typically consists of one or more “plays,” where each play contains a list of tasks. A task is a single action that needs to be executed on a remote host, such as installing a package, copying a file, or restarting a service.

The playbook describes the desired state of the remote hosts, and Ansible takes care of bringing the hosts to that desired state by executing the tasks in the playbook.

Terminologies:

Inventory: The inventory is a file that defines the list of hosts and groups of hosts on which Ansible will operate. It can be in INI or YAML format and contains information about hostnames or IP addresses, SSH connection details, and host grouping for better organization.

Play: A play is a set of tasks that are executed on a specific set of hosts. It defines what needs to be done on those hosts. Each playbook can have one or more plays, allowing you to target different groups of hosts or perform different tasks on the same group of hosts.

Task: A task is an individual action that needs to be performed on a remote host. It is defined within a play and represents a specific action, such as installing a package, copying files, managing services, etc.

Module: Modules are small, standalone scripts that Ansible uses to perform tasks. They are executed on remote hosts and are responsible for taking desired states defined in the playbook and making the necessary changes. Ansible provides a rich set of built-in modules for various tasks, such as yum, apt, copy, file, service, etc.

Playbook Variables: Playbook variables are used to customize the behavior of tasks within a playbook. They allow you to make the playbook flexible and reusable by changing the variable values based on the target hosts or environments.

Facts: Facts are pieces of information collected from the remote hosts by Ansible. They include details like network interfaces, operating system, available memory, disk space, etc. Facts can be used in playbooks to make decisions based on the remote host’s characteristics.

Handlers: Handlers are tasks that are only executed when triggered by a notification. They are typically used to restart services or perform actions that should occur only after certain tasks have made changes to the system.

Templates: Templates are files that contain variables and other dynamic content. They allow you to create configuration files that are customized based on specific host or environment details.

Roles: Roles are a way to organize playbooks and other files in a structured manner. They provide a reusable and modular approach to managing different aspects of your infrastructure.

Pre-requisite :

Make sure you have Ansible installed on your local system.

Managed nodes with SSH access and Python installed.

Put all the ip addresses of a host into Ansible inventory file (/etc/ansible/hosts)

Step1: Create a Playbook with extension as .yml

touch My_Playbook.yml

Step2 : Setup a docker and start service for uses

- name: Configure and Start Docker Container for httpd
hosts: your_managed_nodes
become: true
tasks:
- name: Install required packages for Docker
apt:
name:
- apt-transport-https
- ca-certificates
- curl
- gnupg
- software-properties-common
state: present

- name: Add Docker GPG key
apt_key:
url: https://download.docker.com/linux/ubuntu/gpg
state: present

- name: Add Docker repository
apt_repository:
repo: deb [arch=amd64] https://download.docker.com/linux/ubuntu {{ ansible_distribution_release }} stable
state: present

- name: Install Docker
apt:
name: docker-ce
state: present

- name: Start and enable Docker service
service:
name: docker
state: started
enabled: yes

- name: Pull httpd server image from Docker Hub
docker_image:
name: httpd
source: pull

- name: Create a directory for html content
file:
path: /var/www/html
state: directory

- name: Copy html code to the /var/www/html directory
copy:
src: path/to/your/html/code/index.html
dest: /var/www/html/index.html
owner: root
group: root
mode: "0644"

- name: Run the httpd container and expose to the public
docker_container:
name: my_httpd_server
image: httpd
ports:
- "80:80"
volumes:
- /var/www/html:/usr/local/apache2/htdocs
state: started

Step3 : Pull httpd server image from Docker Hub

- name: Pull httpd server image from Docker Hub
docker_image:
name: httpd
source: pull

- name: Create a directory for html content
file:
path: /var/www/html
state: directory

Step4: Create a directory for html content

- name: Create a directory for html content
file:
path: /var/www/html
state: directory

- name: Copy html code to the /var/www/html directory
copy:
src: /root/Directory1/index.html
dest: /var/www/html/index.html
owner: root
group: root
mode: "0644"

Step5: Run the httpd container and expose to the public

- name: Run the httpd container and expose to the public
docker_container:
name: my_httpd_server
image: httpd
ports:
- "80:80"
volumes:
- /var/www/html:/usr/local/apache2/htdocs
state: started

Step6 : Run below command to run playbook to deploy

ansible-playbook My_playbook.yml

Thanks for reading !!🙂

--

--

Ankit Shukla
Ankit Shukla

Written by Ankit Shukla

| Software Developer | SDET |

No responses yet