LAUNCHING AND CONFIGURING WEBSERVER IN DOCKER THROUGH ANSIBLE
Hola! Guys seeking for some automation you are at the right place. Here I am going to show you to how to launch docker container and configuring Web Server on the launched docker itself. This is not a new thing …but what if I say that all the things I would do without accessing the system or host computer. The only condition is that the host computer should be switched on and has Internet Connection. I am going to perform all the mentioned operations through a tool by REDHAT popularly known as ANSIBLE.
Before commencing to create the setup. Let me introduce you with some Ansible Basic Terminologies.
1. Ansible
Ansible is a software tool that provides simple but powerful automation for cross-platform computer support. It is primarily intended for IT professionals, who use it for application deployment, updates on workstations and servers, cloud provisioning, configuration management, intra-service orchestration, and nearly anything a systems administrator does on a weekly or daily basis. Ansible doesn’t depend on agent software and has no additional security infrastructure, so it’s easy to deploy.
2. Controller Node
Any machine with Ansible installed. You can run Ansible commands and playbooks by invoking the ansible or ansible- playbook command from any control node. You can use any computer that has a Python installation as a control node - laptops, shared desktops, and servers can all run Ansible. However, you cannot use a Windows machine as a control node. You can have multiple control nodes.
3. Managed Node
The network devices (and/or servers) you manage with Ansible. Managed nodes are also sometimes called “hosts”. Ansible is not installed on managed nodes because Ansible is an agentless tool.
4. Inventory
A list of managed nodes. An inventory file is also sometimes called a “hostfile”. Your inventory can specify information like IP address for each managed node. An inventory can also organize managed nodes, creating and nesting groups for easier scaling.
5. Configuration File
By default Ansible reads its configuration file in /etc/ansible/ansible.cfg, however this behavior can be altered. The recommended practice is either to have an ansible.cfg in your current project working directory or to set it as an environment variable. One way to determine which configuration file ansible is using is to use the ansible --version command, you can also run your ansible commands with the -v option.
6. Module
Modules (also referred to as “task plugins” or “library plugins”) are discrete units of code that can be used from the command line or in a playbook task. Ansible executes each module, usually on the remote managed node, and collects return values.
7. Playbook
Playbooks are the files where Ansible code is written. Playbooks are written in YAML format. YAML stands for Yet Another Markup Language. Playbooks are one of the core features of Ansible and tell Ansible what to execute. They are like a to-do list for Ansible that contains a list of tasks. Playbooks contain the steps which the user wants to execute on a particular machine. Playbooks are the building blocks for all the use cases of Ansible.
8. Ad-hoc
An Ansible ad-hoc command uses the /usr/bin/ansible command-line tool to automate a single task on one or more managed nodes. Ad-hoc commands are quick and easy, but they are not reusable.
9. Resource Abstraction Layer
Resource Abstraction Layer uses ansible resources and this makes it possible to target the specified configuration of the system without worrying about the implementation details and the other internal working. These internal workings are defined in the underlying configuration file.
Note:
I will provide a GitHub link in the last of this blog and there you can find all the codes used in this blog.
Let us now commence to create the setup proposed :
1. Installing Ansible
First we have to install Python as Ansible is written on the Programming Language known as Python.
You can achieve it by the command shown below.
Just Press y to Continue.
Now we will install Ansible by the command shown below.
2. Setting Up Inventory
Make a text file and write the following content as shown below.
After doing this let us move to another part.
3. Configuration File
a) Before making this make a directory on the path : /etc/
b) Make a directory with name ansible by the command shown below
mkdir ansible
c) Now in this folder make a configuration file with name and extension : ansible.cfg
Now write the following content shown below.
After making this let us move to the Coding Part of Ansible which is known as Playbook.
Before moving to code just make a file with the extension .yml by the command shown below.
vi <file-name>.yml
For running the Code run the playbook by the following command:
ansible-playbook <file-name>.yml
We will be doing it step by step which will evolve a better understanding of code of the tool.
1. Hosts and Inputs
We will be mentioning hosts (managed nodes) in the file and we will be taking inputs from the user about the image of docker, name of OS, and the folder to be mounted.
Code:
Output:
2. Create and Mount the Folder
We will be creating a directory by the name given by the user and then will be mounting the same folder with the disk (/dev/cdrom) of the Operating System.
Code :
Output :
3. Setup Yum Repository
We will be setting up yum repositories which means we will making yum able to install any software in the Operating System.
Code:
Output:
4. Installing Docker
We will now install docker in the host computer which will create a repository, download it, install the software and will start the service in the computer.
Code:
Output:
Now to run the container one of prerequisite of Ansible is to download package of Docker from Python package library.
5. Python and Docker Package
We will be installing Python 3 in the host computer and after this we will download and install the docker package through pip which will download and install the package from the Package library of Python.
Code:
Output:
6. Pulling A Docker Image
We will be downloading a docker image from the Docker Hub and will be taking the input from the user about the image of the docker.
Code:
Output:
7. FILE FOR WEBSERVER
We will be creating a file which we want to host and after that we will be copying that file to a directory.
Code:
Output:
Code in Index.html
8. Firewall Settings
The file is copied and we will be disabling the firewall at the port which we are exposing or in simple words getting the host accessed by the outer world.
Code:
9. Launching Container
Now we will deploy the container with the image and name of the container asked from the user. Exposing the ports and copying the file to the document directory (/usr/local/apache2/htdocs/)
Code:
Output:
10. Stopping Firewall
We just need to stop the firewall of the host computer and after that with the IP of the host and the port number exposed one can access the content or host the website.
Code:
Output:
RUNNING THE FILE :
RUNNING THE OUTPUTS :
Hope you learned and enjoyed a lot!!
Here is the GitHub Link which I promised to mention: