Setting up Docker Configuration By Ansible

Why Ansible?

How to set up Ansible in Controlled Node?

  1. Min 2GB RAM
  2. 64-bit support required (kernel and runtime)
pip3 install ansible
$vi /etc/myhosts.txt[<name>]
192.168.1.102 ansible_ssh_user=<username> ansible_ssh_pass=<password>
192.168.1.104 ansible_ssh_user=<username> ansible_ssh_pass=<password>
$ansible all -m ping
  1. So here we first make a repository of docker by the yum_repository package, here we have to provide the name, description, file, baseurl, gpgcheck for more parameters see the official documentation of ansible. Before it, we have to provide the host.
- hosts: <name>
tasks:
- name: docker-repo
yum_repository:
name: 'docker'
description: 'Docker Repo'
file: 'docker-repo'
baseurl: "https://download.docker.com/linux/centos/7/x86_64/stable/"
gpgcheck: no
- name: docker_install
command:
cmd: 'yum install docker-ce --nobest'
register: docker_install_status
- debug:
var: docker_install_status
   - name: docker-service
service:
name: 'docker'
state: started
enabled: yes
register: docker_service_status
- debug:
var: docker_service_status
- name: python for docker
pip:
name: 'docker'
- name: pulling_image
docker_image:
name: 'httpd'
source: pull
register: docker_image_status
- debug:
var: docker_image_status
- name: copy-file
copy:
src: '/root/ansifol/index.html'
dest: '/root/http/index.html'
register: Copy_file_status
- debug:
var: copy_file_status7
- name: start-container
docker_container:
name: 'httpd'
image: 'httpd'
state: started
detach: yes # run in background
ports:
- "8081:80" #linking port 80 to 8081
volumes:
- /root/http/:/usr/local/apache2/htdocs/:ro
register: container_status
- debug:
var: container_status
$ansible-playbook <name of file>.yml

Thank you!

--

--

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