Write an Ansible PlayBook that does the following operations in the managed nodes: 🔹 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 the/var/www/html directory and start the web server

 ---

- name: Configure and Deploy Docker Container

  hosts: managed_nodes

  become: yes

  tasks:

    - name: Update the package cache

      apt:

        update_cache: yes

      when: ansible_os_family == 'Debian'


    - name: Install required packages

      apt:

        name:

          - docker.io

          - docker-compose

        state: present

      when: ansible_os_family == 'Debian'


    - name: Start and enable Docker service

      service:

        name: docker

        state: started

        enabled: yes

      when: ansible_os_family == 'Debian'


    - name: Pull httpd server image

      docker_image:

        name: httpd

      when: ansible_os_family == 'Debian'


    - name: Run the Docker container

      docker_container:

        name: my_httpd

        image: httpd

        ports:

          - "80:80"

        volumes:

          - "/var/www/html:/usr/local/apache2/htdocs"

      when: ansible_os_family == 'Debian'


    - name: Copy HTML code to /var/www/html

      copy:

        src: /path/to/your/html/code/index.html

        dest: /var/www/html/index.html

      when: ansible_os_family == 'Debian'


    - name: Restart the Docker container

      docker_container:

        name: my_httpd

        state: restarted

      when: ansible_os_family == 'Debian'


Comments

Popular posts from this blog

Revolutionizing Industries: Solving Challenges with Ansible

How to Install & run docker inside docker?

Creating a Python Script to Capture and Process Face Photos