ansible, command module and jq with pipe

非 Y 不嫁゛ 提交于 2020-01-06 14:33:32

问题


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?


回答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 .



来源:https://stackoverflow.com/questions/55584559/ansible-command-module-and-jq-with-pipe

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!