0

I'm sorry I just don't plain get iteration through JSON. I need help please. I am doing simple shell module commands to get processor, memory, and disk space.

I create a set_fact: and add that information in. This is what the running playbook spits out. It is a fact called 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"
}

}

How to I pull out a single key:value or even a value. I cannot use json_query because it expects something to be installed so that is not an option for me. Using Ansible version 2.2.1.

Thanks in advance for clearing this up so I can learn.

RedBloodMage
  • 77
  • 1
  • 1
  • 5
  • Please specify your objective. You already pulled the values all at once, but that doesn't satisfy you. No one but you knows what you want. – techraf May 11 '17 at 23:03
  • I have the values but how do I select individual values of a specific key. So for the CPU.output I want to have a statement or evaluation of that value. So it is an idle number that Cpu.output. I want to know how I would call it out or single the value out to see if it is less than say 95.00 – RedBloodMage May 12 '17 at 01:58
  • "*to see*" - you can use your eyes. However, if you wanted to use the value in some code, please publish that code. – techraf May 12 '17 at 02:01
  • If this is what you want, it's a duplicate of [yesterday's question](http://stackoverflow.com/q/43902117/2947502). – techraf May 12 '17 at 04:23
  • Something to this effect. Printing Name and Output to File That code is not entirely functional but something similar to this: https://gist.github.com/anonymous/bfe7ed5ed1109cf5a524beeefdf5fca5 – RedBloodMage May 12 '17 at 04:34

1 Answers1

0

To fetch individual objects from a list, you can use 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
Konstantin Suvorov
  • 65,183
  • 9
  • 162
  • 193
  • Thank you very much for the help I'll be working on this shortly. I'll let you know what happens with it. – RedBloodMage May 12 '17 at 14:36
  • When I attempt a run on that book as is above I get some errors so I'll try and work through it but it does look like at least on Ansible 2.2.1.0 I am getting issues. Errors are too numerous to post here so I placed them here. https://gist.github.com/anonymous/0edb94fc23f7d09652c592396324fd2b HOWEVER: Testing it LOCAL on my Development Station it works fine so there is an issue with the Ansible setup on the server. That I may not have the authority to get around. Are they any other ways that you are aware of to single out the data without selectattr? In not that's fine. – RedBloodMage May 12 '17 at 15:13
  • The below works. https://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' ` – RedBloodMage May 12 '17 at 16:18