Ansible Playbook for Launching Docker

Ansible Playbook for Launching Docker

Task:

?? Configure Docker

?? Start and enable Docker services

?? Pull the httpd server image from the Docker Hub

?? Run the docker container and expose it to the public

?? Copy the html code in Root directory and start the web server

Solution:

Check we have Ansible Installed

#ansible --version

We have to first configure ansible.cfg file as below I have configured in current workspace

No alt text provided for this image

Ansible first check config file in current workspace, Now It's time to configure Inventory

No alt text provided for this image

We can have multiple host groups in single inventory according to use cases, here I have enabled authentication with password, I have DockerNode as host group as mentioned above we can use IP or hostname of managed node.

Playbook for configure docker:

There we have entire code on github.

GitHub Link:- https://github.com/yashdwi05/ansible-docker.git

---
- name: Playbook for Docker Config
  hosts: DockerNode
  become: yes
  gather_facts: false
  tasks:
          - name: config yum repo for docker package
            yum_repository:
                   name: docker
                   description: docker repo
                   baseurl: https://download.docker.com/linux/centos/7/x86_64/stable/
                   gpgcheck: no

we have host group which we have in inventory and also we're using privilege escalation so the playbook can run with root power using sudo. the first task is to configure yum repo for docker package.

          - name: Install docker package
            command: yum install -y docker-ce --nobest


          - name: available docker dependancy
            pip:
                    name: docker

This task is to install docker in RHEL8 and also the python dependency for docker, here we have to use command module because there is no option available for --nobest and we have to use it because of docker RHEL8 limitation.

          - name: start docker service
            service:
                    name: docker
                    state: started

This task is to start docker service

          - name: directory for webpage
            file:
                    name: "/etc/docker_page"
                    state: directory


          - name: installing webpage
            copy:
                    content: "This page from docker"
                    dest: "/etc/docker_page/index.html"

These tasks is to create directory for storing webpages which we'll use to mount to containers and there copy module deploy webpages.

          - name: Container present and Exposed
            docker_container:
                    name: byansible
                    state: started
                    image: httpd:latest
                    ports:
                            - "8081:80"
                    volumes:
                            - "/etc/docker_page:/usr/local/apache2/htdocs"

this task ensure to launch container and it will exposed with volume mounted to container's root directory. the container by default launched in detach mode.

          - name: Testing webserver
            uri:
                    url: https://192.168.29.186:8081/index.html
                    return_content: yes
                    status_code: 200
            register: data


          - name: WebApp
            debug:
                  msg: "{{  data['content']  }}"
                                                                              

These Tasks is for Testing purpose of webserver container launched by httpd Image.

Here uri module can also retrieve status code and content of data, there debug module we used to print some of the data retrieved by uri module.

#ansible-playbook docker_config.yml

This command we can use to run playbook and output would be as:

No alt text provided for this image

Here the playbook working great and also we have output of testing tasks below:

No alt text provided for this image

Here we can see the content and server url working fine.

Thank You!!

要查看或添加评论,请登录

Yash Dwivedi的更多文章

社区洞察

其他会员也浏览了