Automation using Ansible on Docker
Task description:-
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 httpd container and expose it to the public
?? Copy the html code in /var/www/html directory and start the web server
What is Ansible? :- Ansible is an automation tool that is mainly used for configuration management. We can Configure so many things using Ansible like web-server, Infrastructure, etc. Ansible works on declarative approach which means we don't have to tell how to install just tell what to install.
Why Ansible? :- Ansible is an Agentless tool that means we don't need to install it on the managed node just installed on the controller node and start working with ansible. Ansible is very intelligent tool and its intelligence comes from modules. As an IT Admin, we don't have to remember all os commands just tell which package you wanna install and ansible will do that thing for you!
Docker :- Docker is a container engine which is used to create the container. Container is Just Enough Operating system which includes all the required library,packages which is used for running any specific program.Docker is so fast means we can quickly create our container using the docker.
Due to Ram issue here i am using AWS Cloud for performing these steps
Now lets move towards the task:-
We will have to first install the Ansible:-
pip3 install ansible
Before configuration, we have to create the Inventory file
vim /etc/myec2.txt
After this, we'll tell this location in the configuration file
Now we will have to configure the Ansible
vim /etc/ansible/ansible-cfg
Here we have to tell that where is our Inventory file. The inventory file contains the detail about the managed nodes we can also create group. Here We are passing host_key_checking=false so that we don't have to worry about key acceptance.
As I am Doing this task on AWS Cloud so we will have to enable root login in my machine and for this just log in to the instance and changed the permission in sshd configuration
one more thing we have to do just set the password of root
# passwd root
Now we can login using root account and perform the further steps.
Adding Docker repository in our system:-
to run this file we have to run this command
ansible-playbook repo.yml
Now we have added the Repository, so let's install docker I have done both in a single go.
ansible also needs one library installed (docker-py) so we have to first install the docker-py using pip and if pip is not available so we also have to install pip.
tasks: - package: name: python-pip state: present
Now Pip is installed so we can install the docker-py library
- name: Install command: "pip install docker-py"
Now we can start the docker services
service: name: docker state: started
Docker is installed and started. Next, we have to pull the image
- name: pull httpd image docker_image: name: httpd source: pull
We want to copy our file. so we can first create one directory on the managed node and then copy the file inside this directory and afterward, we can attach this volume to our container while creating the container.
- name: Copy file file: path: /myweb state: directory - copy: src: "index.html" dest: "/myweb/"
- name: create the container docker_container: name: myapplication image: httpd state: started restart: yes exposed_ports: 80 ports: "1234:80" volumes: "/myweb/:/usr/local/apache2/htdocs"
By default httpd render the content of /usr/local/apache2/htdocs/ folder so we will mount our volume here. And we want to see our site so we can expose our container. Now if we go to our site we can see our container is launched and the website is running well.
Thanks for visiting my article!!
Code link:- Ansible-task-1