Using ansible’s docker_compose module fails

  ansible, devops, docker, docker-compose

Trying to use docker-compose within an ansible playbook using the docker_compose module. Note that I can successfully use the docker module in ansible but running into problems with docker_compose module. I’m using plaintext vars for testing locally, I use ansible-vault for storing secrets.

On the ansible documentation it explains that their docker-compose module only supports version 1 and 2 of docker-compose.

Here is my docker-compose.yml:

---
version: "2.4"
services:
   my_demo:
       build: .
       networks:
          demo_net:
             ipv4_address: 172.0.1.2
       ports:
          - 8080:8080
       image: demo_image

networks:
    demo_net:
       driver: bridge
       ipam:
         driver: default
         config:
           - subnet: 172.0.1.0/30
             gateway: 172.0.1.1

Here is my main.yml:

---
- hosts: localhost
  connection: local
  become: true

  vars:
    - ansible_sudo_pass: password
    - ansible_python_interpreter: /usr/bin/python3

  tasks:
    - name: Docker compose
      docker_compose:
         project_name: docker_fhe_ansible
         project_src: .
         build: yes
      register: output

    - debug:
        var: output

Output:

PLAY [localhost] **********************************************************************************************************************************************************************************

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

TASK [Docker compose] *****************************************************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"changed": false, "msg": "Failed to import the required Python library (Docker SDK for Python: docker (Python >= 2.7) or docker-py (Python 2.6)) on ubuntu's Python /usr/bin/python3. Please read module documentation and install in the appropriate location. If the required library is installed, but Ansible is using the wrong Python interpreter, please consult the documentation on ansible_python_interpreter, for example via `pip install docker` or `pip install docker-py` (Python 2.6). The error was: No module named 'docker'"}

PLAY RECAP ****************************************************************************************************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0   

The error is "no module named docker", I can verify python3 has the module:

[email protected]:~/Documents/docker-ansible$ python3
Python 3.8.10 (default, Jun  2 2021, 10:49:15) 
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import docker
>>> import ansible

Source: Docker Questions

LEAVE A COMMENT