DinoDD
DinoDD

Reputation: 91

Execute docker-compose inside Ansible playbook

I have an ansible-playbook.yml file and from inside I need to execute docker-compose:

- name: copy sql schema
  hosts: test-mysql
  gather_facts: no
  tasks:        
    - name: Docker compose
      command: docker-compose -f {{ name }}_compose.yml up -d

But I always get the error:

fatal: [test-mysql]: FAILED! => {"changed": false, "cmd": "docker-compose Mick_compose.yml up -d", "failed": true, "msg": "[Errno 2] No such file or directory", "rc": 2}

Any tips?


With command:

- name: Make sure compose service is up
  docker_compose:
    project_src: /path/to/your/compose/project
    files:
      - {{ name }}_compose.yml
    state: present

I get the error:

The offending line appears to be:

    - name: Make sure compose service is up
       docker_compose:
                     ^ here

Upvotes: 5

Views: 5792

Answers (2)

Zeitounator
Zeitounator

Reputation: 44595

[Errno 2] No such file or directory

There is actually no such file or directory as Mick_compose.yml in the default directory where your command is launched. To fix that, I could show you how to change dir from the command module to the correct one holding your file.

But I will let you discover that on your own (if you still really want to do it after this explanation) because you should not run commands directly when there is an existing ansible module already doing what you need

- name: Make sure compose service is up
  docker_compose:
    project_src: /path/to/your/compose/project
    files:
      - "{{ name }}_compose.yml"
    state: present

This is just to put you on track, have a look at the module doc link above for more info on the possible parameters.

Upvotes: 3

rokpoto.com
rokpoto.com

Reputation: 10720

Directory where playbook runs doesn't contain the {{ name }}_compose.yml file.

There's relevant module which can help you. If you don't want to use it, you can add task which will show the directory where playbook runs:

- name: copy sql schema
  hosts: test-mysql
  gather_facts: no
  tasks:
    - debug: 
        msg: "{{ playbook_dir }}"
    - name: Docker compose
      command: docker-compose -f {{ name }}_compose.yml up -d

Then, either move {{ name }}_compose.yml to the directory or provide an absolute path in command: docker-compose -f [abs_path]{{ name }}_compose.yml up -d

Upvotes: 1

Related Questions