Integration of Docker with Ansible!
In our day-to-day life Automation is playing a crucial role!In this article, we are going to do a small task with the ANSIBLE Automation tool.
Task Details:
- Ansible playbook for following operations in the managed node
- > 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 webserver
Before stepping into task lets discuss few points about Ansible
ANSIBLE
If we want to deploy any servers in an os or any other program to run in a os, we need to login to the os manually and do our tasks.
Ansible is an automation tool that does this work on behalf of us!
In Ansible, there are two categories of computers: the control node and managed nodes. The control node is a computer that runs Ansible.
A managed node is any device being managed by the control node.
Ansible works by connecting to nodes (clients, servers, or whatever you’re configuring) on a network, and then sending a small program called an Ansible module to that node. Ansible executes these modules over SSH and removes them when finished. The only requirement for this interaction is that your Ansible control node has login access to the managed nodes.
Prerequisites:
- We need two vms one is Controller and other is Mansged Node
- We have to install ansible in controller node and should give managed node in and credentials
Ansible Playbook
- Ansible playbook is an organized unit of scripts that defines work for a server configuration managed by the automation tool Ansible
If we run the playbook it will perform the task which we have written in the playbook
1:
Configure yum for docker
- hosts: all
tasks:
- name: docker repository
yum_repository:
name: docker
description: this is my docker repo
baseurl: https://download.docker.com/linux/centos/7/x86_64/stable/
gpgcheck: no
2:
Installing Python
- name: installing python3
package:
name: "python3"
state: present
3:
Installing Docker
- name: installing docker
package:
name: "docker-ce-18.03.0.*"
state: present
4:
Starting the docker service
- name: docker service
service:
name: "docker"
state: started
enable: yes
5:
creating a directory (folder) and copying file to that folder
- file:
path: /web
state: directory
- copy:
src: "httpd.html"
dest: "/web/"
6:
pulling docker image and starting our webserver
- docker_container:
name: "webserver"
image: "httpd"
state: yes
detach: yes
ports:
- "234:80"
volumes:
- /web:/usr/local/apache2/htdocs/
Now finally lets run the playbook
asnible-playbook web.yml
Final step: To check our web server configured or not!
Thanks for reading the article!
Thanks,vimal sir for your teaching!