ansible、命令模块和带管道的 jq [英] ansible, command module and jq with pipe

查看:19
本文介绍了ansible、命令模块和带管道的 jq的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试运行一个 docker 命令,该命令返回容器列表及其大小.

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

命令为docker container ls --format "{\"name\":\"{{.Names}}\", \"size\":\"{{.Size}}\"} " --all |jq --slurp

当我尝试在 Ansible 剧本中运行它时,它会爆炸:

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

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

截断的错误是:

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...]
...

我怀疑这与管道 (|) 的使用有关,因为当我删除它时,以及随后的 jq 命令,剧本成功完成.

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.

.sh 脚本在手动运行时可以正常工作.

The .sh script does work correctly when run manually.

我也尝试过使用 shell 模块以及 command 模块 - 两者都无法运行脚本.

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

如何在 Ansible playbook 中使用 jq 和管道函数?

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

推荐答案

退出代码说:

rc: 2标准错误:|-jq - 命令行 JSON 处理器 [版本 1.5-1-a5b5cbe]用法:jq [选项] [文件...]

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

jq 退出代码 2 表示:

有任何使用问题或系统错误

there was any usage problem or system error

man jq 显示:

jq [选项...] 过滤器 [文件...]

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

缺少过滤器".放一个."例如复制输入

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

jq --slurp .

jq --slurp .

这篇关于ansible、命令模块和带管道的 jq的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆