Page 1 of 1

Nagios API not using Ansible URI 'body' options?

Posted: Thu Dec 03, 2020 11:37 am
by walterkessler
I have seen posts in the forum where people show using the 'body' parameter of the Ansible URI module. https://support.nagios.com/forum/viewto ... it=ansible

I have been trying to get host info as well as trying to add a test device, but the API call seems to ignore anything passed via 'body'.

For instance, this will just result in EVERY host being returned:

Code: Select all

  - name: get host config
    uri:
      url: https://SERVERNAME/nagiosxi/api/v1/config/host?apikey=APIKEY
      method: GET
      body: 'host_name=blah001'
      validate_certs: no
    register: get_host_results
If I move the body part up into the URL, it works and returns a single host:

Code: Select all

  - name: Get host config
    uri:
      url: https://SERVERNAME/nagiosxi/api/v1/config/host?apikey=APIKEY&host_name=blah001.domain.net
      method: GET
#      body: 'host_name=blah001.domain.net'
      validate_certs: no
    register: get_host_results
Not sure what I am missing.

Re: Nagios API not using Ansible URI 'body' options?

Posted: Fri Dec 04, 2020 3:22 pm
by ssax
It wouldn't be part of the body, it's a part of the request which is why the URL change works.

Does this work?

Code: Select all

  - name: Get host config
    uri:
      url: https://SERVERNAME/nagiosxi/api/v1/config/host?apikey=APIKEY
      method: GET
      validate_certs: no
      deployment:
        host_name: blah001.domain.net
    register: get_host_results

Re: Nagios API not using Ansible URI 'body' options?

Posted: Mon Dec 07, 2020 12:23 pm
by walterkessler
Ah, so only a POST or PUT can use the body parameter?

What is the syntax to use the 'optional parameter' on the GET to filter results?
It works when using the OBJECTS api, but not when using the CONFIG api:

Works:

Code: Select all

url: "https://SERVER/nagiosxi/api/v1/objects/host?apikey=APIKEY&pretty=1&name=lk:{{ inventory_hostname }}"
This returns only "OK.", even when no host was found:

Code: Select all

url: "https://SERVER/nagiosxi/api/v1/config/host?apikey=APIKEY&pretty=1&host_name=lk:{{ inventory_hostname }}""
If i remove the "lk:" and do an exact name match, i will get a result.
Is there a way to do a search in CONFIG without the full/exact host name?

Re: Nagios API not using Ansible URI 'body' options?

Posted: Tue Dec 08, 2020 4:02 pm
by ssax
When using a GET the body comes from the remote host.

Don't use the body for any of them, there's no need. But yes, essentially POST/PUT will only use the body but it's not required.

Code: Select all

If you want to read information, use GET.
If you want to update a host/service (writing information) use a PUT
If you want to create a new host/service use a POST
It doesn't look like the lk: functionality works for the config endpoint as you found, it doesn't work on mine either and I do not see the code in there that does it. Development would need to add it.

I can submit a feature request to development requesting they add that functionality if you'd like?

Re: Nagios API not using Ansible URI 'body' options?

Posted: Tue Dec 08, 2020 6:55 pm
by walterkessler
ssax wrote:It doesn't look like the lk: functionality works for the config endpoint as you found, it doesn't work on mine either and I do not see the code in there that does it. Development would need to add it.

I can submit a feature request to development requesting they add that functionality if you'd like?
I think that it would be useful considering that new hosts being added -- but not yet applied -- do not appear in 'object' API searches.

Re: Nagios API not using Ansible URI 'body' options?

Posted: Wed Dec 09, 2020 5:56 pm
by ssax
That makes sense because the objects endpoints only deal with live objects and the CCM isn't live until after you apply. I've created the feature request for the config endpoint:

Code: Select all

FR: XI - API - Please add lk: (and other query limiting support that the objects endpoint has) to the config endpoint, it does not allow using lk: to build limited queries
Please keep in mind that the decision to implement the enhancement is at the discretion of our development team.

Thank you!