2

I'm trying to run a docker command that returns a list of containers, and their size.

The command is docker container ls --format "{\"name\":\"{{.Names}}\", \"size\":\"{{.Size}}\"} " --all | jq --slurp

When I try to run this in an Ansible playbook it explodes:

- name: Get cointainer size
      raw: /path/to/script/docker-cointainer-size.sh

The truncated error is:

fatal: [localhost]: FAILED! => changed=true 
  msg: non-zero return code
  rc: 2
  stderr: |-
    jq - commandline JSON processor [version 1.5-1-a5b5cbe]
    Usage: jq [options] <jq filter> [file...]
...

I suspect this has something to do with the use of the pipe (|), as when I remove this, and the subsequent jq command, the playbook completes successfully.

The .sh script does work correctly when run manually.

I have also tried using the shell module, as well as the command module - both are unable to run the script.

How can I use jq and the pipe function in an Ansible playbook?

Magick
  • 4,603
  • 22
  • 66
  • 103
  • 1
    have you checked that your script is returning proper return code when triggered manually? – Marcin Orlowski Apr 09 '19 at 03:15
  • Yes, it is working correctly when run manually. – Magick Apr 09 '19 at 03:16
  • 2
    i am not talking about working but about return code (`echo $?` when your script is done) – Marcin Orlowski Apr 09 '19 at 03:17
  • 1
    FWIW, I cannot reproduce this problem. Your script and playbook seem to run just fine. What version of `jq` are you using? Does the problem go away if you pass `jq` a filter on the command, as indicated in the usage message? I was surprised that it works without this when using `--slurp`. – larsks Apr 09 '19 at 04:23
  • 2
    AFAIK `jq` **mandates** a selector, even if it's just `.` to copy the input to the output – mdaniel Apr 09 '19 at 05:19
  • 2
    Have you considered going the ansible way ? use `command` to get your docker result without piping to jq and parse with `command_result.stdout | from_json` – Zeitounator Apr 09 '19 at 06:54
  • Have you tried encasing the whole statement in quotes? Also the indentation before raw seems to be a bit too much – HermanTheGermanHesse Apr 09 '19 at 07:12
  • @Matthew L Daniel: "rc: 2" supports your explanation. Exit code 2: there was any usage problem or system error. – Vladimir Botka Apr 09 '19 at 07:27
  • 2
    @MatthewLDaniel it doesn't _always_ mandate it [for some time already](https://github.com/stedolan/jq/issues/476), but it can be confusing, as seen here. – Michał Politowski Apr 09 '19 at 07:51

1 Answers1

1

The exit code says:

rc: 2 stderr: |- jq - commandline JSON processor [version 1.5-1-a5b5cbe] Usage: jq [options] [file...]

jq exit code 2 means:

there was any usage problem or system error

man jq shows:

jq [options...] filter [files...]

"filter" is missing. Put a "." for example to copy the input

jq --slurp .

Vladimir Botka
  • 58,131
  • 4
  • 32
  • 63