Docker and Ansible
Introduction
In this article i am going to write ansible playbook that do the following task
Task 1:-
- Installing Docker
- Start and enable Docker services
- Pulling the httpd server image from the Docker Hub
- Run the docker container and expose it to the public
Task 2:-
- Copy the html code in /var/www/html directory
- Starting the web server
Before going any further let me explain you my setup:-
- Red Hat Enterprise Linux(RHEL) 8 as a main OS
- Another RHEL 8 as a target node
Task 1
Step 1:- Configuring YUM Repository
Before install docker in the system our first task is to configuring yum repository so that all the dependency that require to install the docker is satisfied
so now open terminal and type vim docker_ex.yml
command to create an file (you can give any name you like but it’s always good to give meaningful name and file extension as yml)
Before proceeding any further ansible use yml(yaml) as a language to create playbook (playbook is a file that content all the task that ansible going to perform on target node)
let write following lines
- hosts: all # its tell ansible to do the task on all ip present in inventory file
tasks: #list of tasks
- name: 'Creating directory to mount dvd' # name of the task this is optional you may delete it
file: #module used to operate on file and folders
path: '/cdrom' #name of the directory
state: directory #tell whether its file or folder
save the file and run ansible-playbook docker_ex.yml -v
command
We successfully created directory now time to mount the dvd
open docker_ex.yml file and write
- name: 'Mounting dvd'
ansible.posix.mount: #module used to perform mount related operation
path: '/cdrom' # path where we want to mount our dvd
src: '/dev/cdrom' # name of the device we want to mount
fstype: 'iso9660' # format type of mount 1so9660 is the most common type of format used in disk
state: mounted # weather we want to mount or unmount didn't write in quote this is predefine keyword
save the file and run ansible-playbook docker_ex.yml -v
command
now dvd is successfully mounted its time create three yum repository 2 form dvd and 1 from following url
https://download.docker.com/linux/centos/7/x86_64/stable/
- name: 'AppStrem Folder'
yum_repository: # module used to manage yum repos
file: 'dvdrepo' # used to specific file name
name: 'dvd1' # used as unique id for each repo
baseurl: 'file:///cdrom/AppStream' # path of the repo
description: 'Appstream' # description of the repo
gpgcheck: no # disabling gpg check
- name: 'BaseOS Folder'
yum_repository:
file: 'dvdrepo' # used to specific file name
name: 'dvd2' # used as unique id for each repo
baseurl: 'file:///cdrom/BaseOS' # path of the repo
description: 'BaseOS' # description of the repo
gpgcheck: no # disabling gpg check
- name: 'Docker Repo'
yum_repository:
name: 'dockerrepo' # if file name is missing use as a name of the file
baseurl: 'https://download.docker.com/linux/centos/7/x86_64/stable' # path of the repo
description: 'Docker Repo' # description of the repo
gpgcheck: no # disabling gpg check
save the file and run ansible-playbook docker_ex.yml -v
command
now we finally configure yum repository let move to next step
Step 2:- Installing Docker
Now it’s time to install the latest version of docker but there is a problem we can’t directly use package module because redhat didn’t support latest version of docker at the time of writing this file so we have to use command module for this task
- name: 'Installing docker'
command: #module used to run command on target node
cmd: 'yum install docker-ce --nobest -y' # command to execute
warn: no # sometimes ansible throw waring at the time of running command to this option off that waring
save the file and run ansible-playbook docker_ex.yml -v
command
Step 3:- Installing docker api
Before we do further operation on docker we have to install docker api in target node that helps us to ansible to operate on docker this can be download with the help of pip for we need python installed in the system
- name: 'Installing Python 3'
package: # module used to install and remove package
name: 'python3' # name of the package to install
state: present # predefine keyword used to install package
- name: 'Installing Docker API'
pip: # used to interact with pip
name: docker #name of the package to install
state: present
save the file and run ansible-playbook docker_ex.yml -v
command
Step 4: Starting docker services
Now it’s time to start docker services or also known as docker engine
- name: 'Starting Docker engine'
service: # module used to manage services
name: 'docker'
state: started
save the file and run ansible-playbook docker_ex.yml -v
command
Step 5:- Installing docker image
Now it’s time to install httpd image from docker hub
- name: 'Installing image'
community.general.docker_image: # module used to manage docker image
name: 'httpd'
state: present
source: pull # tell that pull image for registry
save the file and run ansible-playbook docker_ex.yml -v
command
now finally its time to start docker conatiner
- name: 'Starting docker httpd server'
community.general.docker_container: # module to manage docker container
name: 'ansible'
image: 'httpd' # image name form which container is launching
detach: yes # same as -d
interactive: no # same -it
published_ports: '8080:80' #port binding
container_default_behavior: "no_defaults" # required to tell that didn't use any default options
the above code is same as
docker run -itd --name ansible -p 80:8080 httpd
save the file and run ansible-playbook docker_ex.yml -v
command
we can confirm by using target node ip and port number 8080 by curl 192.168.0.107:8080
command
Task 1 is completed
Task 2
Step 1:- Install httpd
Now lets create a new yml file name as httpd_ex.yml and add following lines to install httpd server in target node
- hosts: all
tasks: - package:
name: 'httpd'
state: present
save the file and run ansible-playbook httpd_ex.yml -v
command
Step 2:- Copy file to target node
First create a html file in current dir name as page.html use vim page.html
command to create a file and add the following lines
<h1 style=”color: red”> Hello </h1>
now open httpd_ex.yml and add
- name: 'Coping File to target node'
copy: # module used to copy files and folder to target node
src: '/root/ansible_workspace/page.html' # files and folder to copy
dest: '/var/www/html/' # where to copy
save the file and run ansible-playbook httpd_ex.yml -v
command
Step 3:- Starting httpd
Now it’s time to start httpd server in target node
- name: 'Starting httpd'
service:
name: 'httpd'
state: started
save the file and run ansible-playbook httpd_ex.yml -v
command
Step 4:- Firewall
If we try to access our page now we can’t do this due to firewall so let configure port 80 using ansible
- name: 'Enabling port number'
ansible.posix.firewalld: #module used to manage firewall ports
port: 80/tcp #port number that we want to use port/protocol
state: enabled
save the file and run ansible-playbook httpd_ex.yml -v
command
Now go to browser and visit target_node_ip:80/page.html in my case its (http://192.168.0.107/page.html)
Task 2 Completed
Conclusion
Find all the here ➡️ https://github.com/suyash222/ansible_eg
Thank for everyone to reading my article till end if you have any doubt please comment if you have any suggestion please mail all comment both positive and negative is more than welcomed
Contact Detail
LinkeDin [https://www.linkedin.com/in/suyash-garg-50245b1b7]
Additional Tags
#python #vimaldaga #righteducation #educationredefine #rightmentor #worldrecordholder #linuxworld #makingindiafutureready #righeudcation #arthbylw #ansible #docker