nordvpn

Deploy docker-compose on docker swarm using Ansible

In this article, I will show you how you can deploy docker-compose on docker swarm using Ansible. It will automate the deployment process and we don’t need to do anything manually.

Red Hat® Ansible® Automation is automation software powered by Red Hat Ansible Engine—an execution engine with hundreds of modules that can automate all aspects of IT environments and processes—and Red Hat Ansible Tower—a management interface that can integrate with other services.

Prerequisites:

Read Also: How to install Java 11 on Debian

Deploy docker-compose on docker swarm using Ansible

I have created a playbook to deploy docker-compose on docker swarm cluster on AWS using Ansible. So, here is my deploy.yml.

- name: Deploy on Swarm Cluster
  hosts: masters
  gather_facts: False
  remote_user: ec2-user
  tasks:
    - name: Create a directory
      file:
        path: /home/ec2-user/deploy
        state: directory

    - name: Copy docker-compose-test.yml to master
      copy:
        src: docker-compose-test.yml
        dest: /home/ec2-user/deploy

    - name: Deploy on Swarm
      shell: cd infra-problem && docker stack deploy -c docker-compose-test.yml testapp
      register: stack_deploy

    - debug: var={{item}}
      with_items: stack_deploy.stdout_lines

    - name: Check list of services
      command: docker service ls
      register: service_list

    - debug: var={{item}}
      with_items: service_list.stdout_lines

    - name: Check list of stack
      command: docker stack ps infra
      register: stack_ps

    - debug: var={{item}}
      with_items: stack_ps.stdout_lines

So the playbook will first create a deploy directory in /home/ec2-user/.  After creating a deploy directory it will copy the docker-compose file into this directory. I have a file called docker-compose-test.yml and I will use this file to deploy micro-services on the swarm. Then the next step is to deploy the docker-compose file using docker stack command.

In the next step, it will list all the docker services and also it will list the stack. So we can see the list of the stack that is deployed.

Deploy docker-compose

Now let’s deploy it on docker swarm.

ansible-playbook -i hosts deploy.yml

After executing in few seconds it will deploy and update stack on docker swarm.

If you have an issue using this tutorial please feel free to comment below.

 

Leave a Reply

Your email address will not be published.


*



The reCAPTCHA verification period has expired. Please reload the page.

48-Hour Flash Sale! Courses from just $10.99