【问题标题】:JSON Document Fields in AnsibleAnsible 中的 JSON 文档字段
【发布时间】: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


【解决方案1】:

要从列表中获取单个对象,您可以使用selectattr

---
- hosts: localhost
  gather_facts: no
  vars:
    payload_list: [ { "name": "CPU",    "output": "99.05", "rc": "0", "threshold": "1" },
                    { "name": "Memory", "output": "4GB",   "rc": "0", "threshold": "1" } ]
  tasks:
    - debug:
        msg: "{{item}} = {{ (payload_list | selectattr('name','equalto',item) | first).output }}"
      with_items:
        - CPU
        - Memory

【讨论】:

  • 非常感谢您的帮助,我很快就会处理这个问题。我会让你知道它会发生什么。
  • 当我尝试按上述方式运行该书时,会遇到一些错误,因此我会尝试解决它,但至少在 Ansible 2.2.1.0 上看起来我遇到了问题。错误太多,无法在此处发布,因此我将它们放在这里。 gist.github.com/anonymous/0edb94fc23f7d09652c592396324fd2b 但是:在我的开发站上进行本地测试它工作正常,因此服务器上的 Ansible 设置存在问题。我可能没有权力四处走动。您是否知道它们有任何其他方法可以在没有 selectattr 的情况下挑选出数据?这不是很好。
  • 以下工作。 bpaste.net/show/7d2e7ac72ab2--- - hosts: localhost gather_facts: no vars: payload_list: [ { "name": "CPU", "output": "99.05", "rc": "0", "threshold": "1" }, { "name": "Memory", "output": "4GB", "rc": "0", "threshold": "1" } ] tasks: - debug: msg: "Name: {{ item.name }}, Value: {{ item.output }}." with_items: "{{ payload_list }}" when: item.name == 'Memory'
猜你喜欢
  • 2021-07-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-22
  • 1970-01-01
  • 2014-04-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多