Integrating Ansible with Docker

What is Ansible ?

Ansible’s Features and Capabilities

CONTROLLER NODE & MANAGED NODE ARCHIETECTURE :-

Task Description :-

Steps :-

pip3 install ansible

2. Check whether ansible is installed or not using command : -

ansible --version

3. Now we have to write an ansible playbook for configuration of Docker.

- hosts: all
tasks:
- name: Add docker repo
yum_repository:
name: docker
description: docker repo
baseurl:
https://download.docker.com/linux/centos/7/x86_64/stable/
gpgcheck: no
- name: Installing Docker packages
package:
name: "docker-ce-18.09.1*"
state: present
register: a- name: Docker installation status
debug:
var: a
- name: Installing Docker SDK
pip:
name: "docker"
register: b- name: Checking SDK install status
debug:
var: b
- name: Starting Docker services
service:
name: "docker"
state: started
enabled: yes- command: "systemctl status docker"
register: c- name: Checking the status of Docker service
debug:
var: c
- name: Creating directory for copying HTML file
file:
path: "/ansible_task1"
state: directory
- name: Copying local file into the directory
copy:
src: "home.html"
dest: "/ansible_task1/"
register: d
- name: Status of the local file
debug:
var: d

4. Our final step is to run the container using HTTPD image. The image will be automatically downloaded from hub.docker.com. We have also exposed the port for our container to run our website at the port mentioned & mounted the webpage directory to the /usr/local/apache2/htdocs/ folder.

- name: Creating a container using HTTPD Image
docker_container:
name: myweb
image: httpd
state: started
exposed_ports:
- "80"
ports:
- "8081:80"
volumes:
- /ansible_task1:/usr/local/apache2/htdocs/
tty: true
detach: true - command: "docker ps"
register: e- name: Status of the Container
debug:
var: e

Ansible Playbook execution :-

Managed Node after Docker Configuration :

Thanks, hope you guys will like my article. if you have any suggestion or query will free to ask.

#Happylearning #keepsharing #ansible #automation

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store