【发布时间】:2017-05-11 22:20:42
【问题描述】:
对不起,我只是没有通过 JSON 进行迭代。我需要帮助。我正在执行简单的 shell 模块命令来获取处理器、内存和磁盘空间。
我创建了一个 set_fact: 并将该信息添加到其中。这就是正在运行的剧本吐出的内容。这是一个名为 payload_list 的事实。
"ansible_facts": {
"payload_list": [
{
"name": "Hostname",
"output": "test_server",
"rc": "0",
"threshold": "0"
},
{
"name": "Uname",
"output": "Linux 2.6.32-431.29.2.el6.x86_64",
"rc": "0",
"threshold": "0"
},
{
"name": "Uptime",
"output": "22:09:17 up 91 days 3:44",
"rc": "0",
"threshold": "0"
},
{
"name": "CPU",
"output": "99.05",
"rc": "0",
"threshold": "1"
},
{
"name": "Memory",
"output": "4GB",
"rc": "0",
"threshold": "1"
},
{
"name": "Disk Usage",
"output": "40",
"rc": "0",
"threshold": "1"
}
]
},
"changed": false,
"invocation": {
"module_args": {
"payload_list": [
{
"name": "Hostname",
"output": "test_server",
"rc": "0",
"threshold": "0"
},
{
"name": "Uname",
"output": "Linux 2.6.32-431.29.2.el6.x86_64",
"rc": "0",
"threshold": "0"
},
{
"name": "Uptime",
"output": "22:09:17 up 91 days 3:44",
"rc": "0",
"threshold": "0"
},
{
"name": "CPU",
"output": "99.05",
"rc": "0",
"threshold": "1"
},
{
"name": "Memory",
"output": "",
"rc": "0",
"threshold": "1"
},
{
"name": "Disk Usage",
"output": "",
"rc": "0",
"threshold": "1"
}
]
},
"module_name": "set_fact"
}
}
如何提取单个 key:value 甚至一个 value。 我不能使用 json_query 因为它需要安装一些东西,所以这不是我的选择。使用 Ansible 2.2.1 版。
提前感谢您解决此问题,以便我学习。
【问题讨论】:
-
请说明您的目标。您已经一次提取了所有值,但这并不满足您。没有人知道你想要什么。
-
我有值,但如何选择特定键的单个值。因此,对于 CPU.output,我想要对该值进行声明或评估。所以Cpu.output是一个空闲的数字。我想知道我将如何调用它或将值单独列出来查看它是否小于 95.00
-
"看" - 你可以用你的眼睛。但是,如果您想在某些代码中使用该值,请发布该代码。
-
如果这是你想要的,它是 yesterday's question 的副本。
-
有这个效果。将名称和输出打印到文件该代码并不完全正常,但类似于:gist.github.com/anonymous/bfe7ed5ed1109cf5a524beeefdf5fca5
标签: json ansible ansible-2.x