Skip to content

It's just an ansible playbook and that the playbook installs docker and deploys a nodejs container through that playbook, all that plays are using with inbuilt ansible modules.

yousafkhamza/docker-deployment-from-playbook

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 

Repository files navigation

Docker Deployment from an Ansible Playbook

Build


Description

It's just an ansible playbook and that the playbook installs docker and deploys a nodejs container through that playbook, all that plays are using with inbuilt ansible modules.


Feature

  • Easy to deploy a container with the help of an ansible-playbook
  • All these plays are using inbuilt ansible modules
  • Docker installation and container spin up through the same playbook

Pre-Requests

  • Install Ansible on your Master Machine
  • Basic Knowledge of ansible
Installation

Ansible2 (For your reference visit How to install Ansible)

Packages

amazon-linux-extras install -y ansbile2
yum install -y git
Ansible Modules used

How to use

git clone https://github.com/yousafkhamza/docker-deployment-from-playbook.git
cd docker-deployment-from-playbook
ansible -i hosts -m ping all                 <--------- Establish/Check SSH connection via ansible
ansible-playbook -i hosts dockerinstallation.yml           <------------- playbook running script

Output be like

PLAY [Installing docker through ansible] ***********************************************************************************************************

TASK [Gathering Facts] *****************************************************************************************************************************
ok: [172.31.10.48]

TASK [PIP Installation] ****************************************************************************************************************************
changed: [172.31.10.48]

TASK [Depandancy Task Install dokcer-py] ***********************************************************************************************************
changed: [172.31.10.48]

TASK [Docker Installtion] **************************************************************************************************************************
changed: [172.31.10.48]

TASK [Docker Service start and Enable] *************************************************************************************************************
changed: [172.31.10.48]

TASK [Docker image from docker hub] ****************************************************************************************************************
changed: [172.31.10.48]

TASK [Create a container from image] ***************************************************************************************************************
changed: [172.31.10.48]

PLAY RECAP *****************************************************************************************************************************************
172.31.10.48               : ok=7    changed=6    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Point of view on Slave

[root@ip-172-31-10-48]# docker image ls
REPOSITORY          TAG       IMAGE ID       CREATED       SIZE
yousafkhamza/node   latest    39e4935f129b   6 weeks ago   50.8MB
[root@ip-172-31-10-48]# docker container ls
CONTAINER ID   IMAGE               COMMAND         CREATED         STATUS         PORTS                  NAMES
79867e5504e3   yousafkhamza/node   "node app.js"   8 minutes ago   Up 7 minutes   0.0.0.0:80->3000/tcp   ansible_nodejs

Behind the code

vim dockerinstallation.yml

---
- name: 'Installing docker through ansible'
  hosts: all
  become: true
  vars:
    container_name: "ansible_nodejs"
    container_image: "yousafkhamza/node"
    container_port: "80:3000"
  tasks:
  
    - name: "PIP Installation"
      yum:
        name: pip
        state: present
        
    - name: "Depandancy Task Install dokcer-py"
      pip:
        name: docker-py
        state: present

    - name: "Docker Installtion"
      yum:
        name:
          - docker
        state: present

    - name: "Docker Service start and Enable"
      service:
        name: docker
        state: restarted
        enabled: true

    - name: "Docker image from docker hub"
      docker_image:
       name: "{{ container_image }}"
       source: pull

    - name: "Create a container from image"
      docker_container:
       name: "{{ container_name }}"
       image: "{{ container_image }}"
       ports: "{{ container_port }}"
       state: started

vim hosts

[slave]
172.31.10.48 ansible_user="ec2-user" ansible_port=22 ansible_ssh_private_key_file="/root/abcd.pem"

Please note that you may use your slave server's details also please put your key absolute path and avoid unnecessary issues

Output Screenshot

alt_txt

Reference

On this playbook was I used my old nodjs container image so please be check the same. I have already explained how we create an image and container from the docker file.


Conclusion

It's a simple Ansible playbook how we pulled an image from docker hub and then how to spin up a container from that image through ansible. I just uploaded the same for educational purposes and this playbook we used inbuilt modules so it is should more helpful for freshers who started ansible and docker.

⚙️ Connect with Me


About

It's just an ansible playbook and that the playbook installs docker and deploys a nodejs container through that playbook, all that plays are using with inbuilt ansible modules.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published