Looking for advice: json returned from API with and without brackets

16 views
Skip to first unread message

J-F Gaudreault

unread,
Aug 16, 2018, 2:57:40 PM8/16/18
to Ansible Project
Hi! I getting a json from an API request. I've registered the json in a var.

I'm trying to find a way to get the hostname in the json.
I need to find it even if it has brackets or not.

I only got this far ....


with brackets:

- name: test1
  debug:
    msg: "{{ nagios_xi_hosts | json_query('json.host[0].host_name') }}"

no brackets:

- name: test2
  debug:
    var: nagios_xi_hosts.json.host.host_name




Jonathan Lozada De La Matta

unread,
Aug 16, 2018, 3:02:03 PM8/16/18
to ansible...@googlegroups.com
a copy of the json would be helpful.

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/7091e4ea-4272-4697-8558-1fc82bff2562%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--

Jonathan lozada de la matta

AUTOMATION CONSULTANT - AUTOMATION PRACTICE

Red Hat Consulting Services

jloz...@redhat.com   



 

J-F Gaudreault

unread,
Aug 16, 2018, 3:12:49 PM8/16/18
to Ansible Project
The json... returning a list of 2+ objects.
When the API returns only one object, the brackets are gone.

{
    "nagios_xi_hosts.json.host": [
        {
            "@attributes": {
                "id": "26128"
            },
            "action_url": {},
            "active_checks_enabled": "1",
            "address": "1.1.1.1",
            "alias": "sldops0725",
            "check_interval": "5",
            "config_type": "1",
            "display_name": "sldops0725",
            "first_notification_delay": "0",
            "host_name": "sldops0725",
            "icon_image": "redhat.png",
            "icon_image_alt": {},
            "instance_id": "1",
            "is_active": "1",
            "max_check_attempts": "5",
            "notes": {},
            "notes_url": {},
            "notification_interval": "60",
            "notifications_enabled": "1",
            "passive_checks_enabled": "1",
            "retry_interval": "1",
            "statusmap_image": "redhat.png"
        },
        {
            "@attributes": {
                "id": "14039"
            },
            "action_url": {},
            "active_checks_enabled": "1",
            "address": "2.2.2.2",
            "alias": "sldops0196",
            "check_interval": "5",
            "config_type": "1",
            "display_name": "sldops0196",
            "first_notification_delay": "0",
            "host_name": "sldops0196",
            "icon_image": "redhat.png",
            "icon_image_alt": {},
            "instance_id": "1",
            "is_active": "1",
            "max_check_attempts": "5",
            "notes": {},
            "notes_url": {},
            "notification_interval": "60",
            "notifications_enabled": "1",
            "passive_checks_enabled": "1",
            "retry_interval": "1",
            "statusmap_image": "redhat.png"
        }
    ]

Kai Stian Olstad

unread,
Aug 16, 2018, 3:41:29 PM8/16/18
to ansible...@googlegroups.com
On Thursday, 16 August 2018 21.12.49 CEST J-F Gaudreault wrote:
> The json... returning a list of 2+ objects.
> When the API returns only one object, the brackets are gone.

But when it return multiple entries, which one of them will you have?
If it's the fist element in the list this should work

{{ nagios_xi_hosts.json.host.host_name | default(nagios_xi_hosts.json.host.0.host_name }}

If nagios_xi_hosts.json.host.host_name doesn't exist it will return the host_name in the first list element.

--
Kai Stian Olstad


Reply all
Reply to author
Forward
0 new messages