ansible, command module and jq with pipe

2019-08-18 15:13发布

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?

标签: ansible
1条回答
啃猪蹄的小仙女
2楼-- · 2019-08-18 15:52

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 .

查看更多
登录 后发表回答