cooshal
cooshal

Reputation: 778

using ansible with docker-compose

I am trying to deploy a docker setup using Ansible playbook. For this, I am using docker_service.

My Playbook looks like:

---
- name: Run Docker compose
hosts: all
gather_facts: no
tasks:
  - debug: msg="Container - {{ inventory_hostname }}"

  - docker_service:
      project_src: "compose"
      state: absent

  - docker_service:
      project_src: "compose"
      state: present

Upon running this simple playbook as:

ansible-playbook -v playbook.yml --ask-sudo-pass

I added --ask-sudo-pass to ensure that it was not a permission issue.

OUTPUT

SUDO password:

PLAY [Run Docker compose] ******************************************************

TASK [debug] *******************************************************************
ok: [prolims-staging] => {
    "msg": "Container - prolims-staging"
}

TASK [docker_service] **********************************************************
fatal: [prolims-staging]: FAILED! => {"changed": false, "msg": "Error connecting: Error while fetching server API version: ('Connection aborted.', error(13, 'Permission denied'))"}
        to retry, use: --limit @/data/prolims-provision/provision-docker.retry

PLAY RECAP *********************************************************************
prolims-staging            : ok=1    changed=0    unreachable=0    failed=1

I did try looking out for this issue on other forums as well ( and similar questions on this StackOverflow too), but those were not helpful.

Note: I am able to run docker-compose successfully in the target machine from its CLI (using sudo).

Also, I tried playing around with docker_container as well. I tried to execute a playbook with contents below:

...
- name: check container status
    command: docker ps
    register: result

  - name: Create a container
    docker_container:
      name: db_pg
      image: "postgres:latest"
      state: present
      recreate: yes
  ...

and running this playbook works perfectly fine.

I assume, posting my docker-compose file might not be relevant here.

I followed this example, but did not work. Maybe, I might be missing some stupid or really important thing here.

Any help on understanding and resolving this issue would be appreciated.

Upvotes: 4

Views: 6388

Answers (1)

techraf
techraf

Reputation: 68439

I am able to run docker-compose successfully in the target machine from its CLI (using sudo).

So you need to use become declaration for the task.

I added --ask-sudo-pass to ensure that it was not a permission issue.

Just adding --ask-sudo-pass to the ansible-playbook parameters doesn't have any effect unless the relevant tasks/plays have become declaration (and become_method is set to sudo, but this is by default).

Reference.

Upvotes: 3

Related Questions