How to use ansible's command module chdir parameter

Hello,

I think this is the right place to post this. Here’s the problem: I’ve been taking the Ansible for absolute beginners course while trying out some stuff on my own, but I ran into some problems with the command module during my experiments.

Here’s the problem: I’m trying to start a docker container on a remote machine using ansible, for which purpose I’ve been trying to use the command module and its chdir parameter to no success.

The project files are located in the remote machine, inside the directory /opt/monitoring_client, while the container itself is in /opt/monitoring_client/pushgateway. In order to have ansible run the container, I tried adding the following lines to the playbook:

        -
            name: 'Run pushgateway docker-compose in detached mode'
            command: docker-compose up -d
            args:
                chdir: /opt/monitoring_client/pushgateway

But this produces the following output:

FAILED! => {"changed": true, "cmd": ["docker-compose", "up", "-d"], "delta": "0:00:01.850715", "end": "2020-08-11 02:55:57.004475", "msg": "non-zero return code", "rc": 1, "start": "2020-08-11 02:55:55.153760", "stderr": "\n        Can't find a suitable configuration file in this directory or any\n        parent. Are you in the right directory?\n\n        Supported filenames: docker-compose.yml, docker-compose.yaml\n        ", "stderr_lines": ["", "        Can't find a suitable configuration file in this directory or any", "        parent. Are you in the right directory?", "", "        Supported filenames: docker-compose.yml, docker-compose.yaml", "        "], "stdout": "", "stdout_lines": []}

I took this to mean that maybe chdir wasn’t changing to the right directory, so I tried running the task below on the playbook instead:

-
            name: 'Show PWD in pushgateway directory'
            command: echo $PWD
            args:
                chdir: /opt/monitoring_client/pushgateway
            register: compose
        -
            debug: msg="{{ compose.stdout }}"

Which gave me the output below:

ok: [instance] => {
    "msg": "/home/test_user"
}

Which suggests that chdir may indeed be failing for some reason, since it didn’t switch to /opt/monitoring_client/pushgateway before running the command. Running the task below, however:

        -
            name: 'List files on pushgateway directory'
            command: ls
            args:
                chdir: /opt/monitoring_client/pushgateway
            register: compose
        -
            debug: msg="{{ compose.stdout }}"

Gives the following output:

ok: [instance] => {
    "msg": "docker-compose.yml"
}

Which is the real content of the /opt/monitoring_client/pushgateway directory, which would imply chdir is working in this case. I have no idea what’s going on, and any insight would be greatly appreciated as to what I’m missing when using the chdir parameter.

When i ran this playbook as per testing working fine.

 - name: 'Run pushgateway docker-compose in detached mode'
   command: "docker-compose up -d"
   args:
         chdir: "/root/docker/"

docker-compose.yml file present under docker directory.