4

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

2 Answers 2

3

[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.

2
  • The offending line appears to be: files: - {{ name }}-compose.yml ^ here
    – DinoDD
    Oct 14, 2019 at 9:58
  • It is mandatory to quote your value when it begins with a jinja2 expansion expression, as this was the case in my above example => - "{{ name }}_compose.yml" Jul 1, 2020 at 8:39
1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.