Unable to find file with docker_container exec module

Bonjour :wave:

My task:

- name: "MongoDB Operate | Create user - {{ mongo_metrics_exporter_user }}"
  community.docker.docker_container_exec:
    container: "{{ app_name }}-{{ allocation_id.stdout }}"
    command: "mongosh admin -u {{ mongo_root_username }} -p {{ mongo_root_password }} < metrics_exporter_user.js"
    chdir: "/data/db"

Result:

FAILED - RETRYING: [pc09.quanticware.com]: MongoDB Operate | Create user - exporter (9 retries left).Result was: changed=true 
  attempts: 2
  invocation:
    module_args:
      api_version: auto
      argv: null
      ca_path: null
      chdir: /data/db
      client_cert: null
      client_key: null
      command: mongosh admin -u <user> -p <password> metrics_exporter_user.js
      container: mongo-63aab8de-4a87-75ee-84b0-690f93909cd8
      debug: false
      detach: false
      docker_host: unix:///var/run/docker.sock
      env: null
      ssl_version: null
      stdin: null
      stdin_add_newline: true
      strip_empty_ends: true
      timeout: 60
      tls: false
      tls_hostname: null
      tty: false
      use_ssh_client: false
      user: null
      validate_certs: false
  rc: 1
  retries: 11
  stderr: 'Error: ENOENT: no such file or directory, open ''/data/db/<'''
  stderr_lines: <omitted>
  stdout: ''
  stdout_lines: <omitted>

But when I’m connected to container:

root@f17f90a1de26:~# stat /data/db/metrics_exporter_user.js 
  File: /data/db/metrics_exporter_user.js
  Size: 147             Blocks: 8          IO Block: 4096   regular file
Device: 801h/2049d      Inode: 850160      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2024-08-19 15:54:16.744183518 +0000
Modify: 2024-08-19 15:31:47.960334327 +0000
Change: 2024-08-19 15:54:12.880228729 +0000
 Birth: 2024-08-19 15:54:10.912251755 +0000

And if I try:

cd /data/db

mongosh admin -u <user> -p <password> metrics_exporter_user.js

It works!

Has anyone had this problem before? And solution? :sweat_smile:

Thanks!

Some of the output doesn’t seem to be consistent with your task. Sometimes the < is there, sometimes it is not. I’m assuming it is present in command.

The module doesn’t pass the command to a shell, but direclty executes it. The mongosh command is executed with the following parameter list:

  1. admin
  2. -u
  3. <user>
  4. -p
  5. <password>
  6. <
  7. metrics_expoerter_user.js

Now mongosh tries to open the file <, which doesn’t exist.

If you wrap the command as the argument of /bin/sh -c "put command here" then it should work.

Thanks a lot! :partying_face:

- name: "MongoDB Operate | Create user - {{ mongo_metrics_exporter_user }}"
  community.docker.docker_container_exec:
    container: "{{ app_name }}-{{ allocation_id.stdout }}"
    command: /bin/sh -c "mongosh admin -u {{ mongo_root_username }} -p {{ mongo_root_password }} < metrics_exporter_user.js"
    chdir: "/data/db"
1 Like