Ansible PlayBook to Launch Web-Server on Docker Container
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.
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 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/
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
tasks:- name: Creating Yum Repository for Docker
description: Docker Repo
enabled: true- name: Docker Package Installation
command: "yum install docker-ce --nobest -y"- name: Install Software Python36
state: present- name: Install Docker Python Library
name: docker-py- name: Creating WorkSpace
state: directory- name: Copying Content Into Managed Node
content: "Hello World!! Task 10 By Karan Agrawal"
ignore_errors: yes- name: Starting Docker Service
state: started- name: Pull an Image
source: pull- name: Creating Docker Container
- 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.