API - adding hostgroups to host not "taking"

This support forum board is for support questions relating to Nagios XI, our flagship commercial network monitoring solution.
travisjburrell
Posts: 14
Joined: Thu Sep 24, 2020 12:36 pm

API - adding hostgroups to host not "taking"

Post by travisjburrell »

I'm using Ansible's URI module to make multiple calls to our Nagios server API. Basically the equivalent of multiple cURL calls.

Code: Select all

- name: Register Host & Assign Hostgroups
  hosts: localhost
  connection: local
  gather_facts: no
  vars_files:
    - vars/defaults.yml
    - vars/services.yml
  tasks:
    - name: Register the host and Add to Hostgroups
      block:
        - name: Register Host with Nagios
          uri:
            url: https://{{ xi_ip }}/nagiosxi/api/v1/config/host?apikey={{ xi_api_key }}
            body_format: form-urlencoded
            body: "{{ register_host | combine({
              'host_name':  host_name,
              'address': host_name,
              'contact_groups': 'IT'
              }) }}"
            method: POST
            validate_certs: no
          register: registration
          failed_when: registration.json.error is defined
  
        - name: Put the New Host Into its Hostgroups - Devops
          uri:
            url: https://{{ xi_ip }}/nagiosxi/api/v1/config/host/{{ host_name }}?apikey={{ xi_api_key }}
            body_format: form-urlencoded
            body:
              hostgroups: "{{ item }}"
            method: PUT
            validate_certs: no
          loop: "{{ critical_devops | flatten }}"
          register: registration
          failed_when: registration.json.error is defined

        - name: Apply Configuration
          uri:
            url: https://{{ xi_ip }}/nagiosxi/api/v1/system/applyconfig?apikey={{ xi_api_key }}&applyconfig=1
            method: POST
            validate_certs: no
          register: apply
This playbook completes successfully, no errors. As you can see, I'm hitting the "apply config" at the end. The host is created, but the hostgroups, even though the output says it's updating the host, are not added. Here's a sample of the successful output (verbose mode, so you can see the response/codes):

Code: Select all

TASK [Put the New Host Into its Hostgroups - DCO Linux] *********************************************************************************************************************************************
ok: [localhost] => (item=monitor_cpu_n) => {"access_control_allow_methods": "POST, GET, OPTIONS, DELETE, PUT", "access_control_allow_origin": "*", "ansible_loop_var": "item", "changed": false, "connection": "close", "content_length": "104", "content_type": "application/json", "cookies": {}, "cookies_string": "", "date": "Fri, 16 Jul 2021 15:25:40 GMT", "elapsed": 0, "failed_when_result": false, "item": "monitor_cpu_n", "json": {"success": "Updated testignore2.example.com in the system. Config imported but not yet applied."}, "msg": "OK (104 bytes)", "redirected": false, "server": "Apache/2.4.6 (CentOS) OpenSSL/1.0.2k-fips PHP/5.4.16", "status": 200, "url": "https://nagios.example.com/nagiosxi/api/v1/config/host/testignore2.example.com?apikey=<ourAPIKey>", "x_powered_by": "PHP/5.4.16"}
ok: [localhost] => (item=monitor_ram_n) => {"access_control_allow_methods": "POST, GET, OPTIONS, DELETE, PUT", "access_control_allow_origin": "*", "ansible_loop_var": "item", "changed": false, "connection": "close", "content_length": "104", "content_type": "application/json", "cookies": {}, "cookies_string": "", "date": "Fri, 16 Jul 2021 15:25:41 GMT", "elapsed": 0, "failed_when_result": false, "item": "monitor_ram_n", "json": {"success": "Updated testignore2.example.com in the system. Config imported but not yet applied."}, "msg": "OK (104 bytes)", "redirected": false, "server": "Apache/2.4.6 (CentOS) OpenSSL/1.0.2k-fips PHP/5.4.16", "status": 200, "url": "https://nagios.example.com/nagiosxi/api/v1/config/host/testignore2.example.com?apikey=<ourAPIKey>", "x_powered_by": "PHP/5.4.16"}
ok: [localhost] => (item=monitor_disk_linux_n) => {"access_control_allow_methods": "POST, GET, OPTIONS, DELETE, PUT", "access_control_allow_origin": "*", "ansible_loop_var": "item", "changed": false, "connection": "close", "content_length": "104", "content_type": "application/json", "cookies": {}, "cookies_string": "", "date": "Fri, 16 Jul 2021 15:25:42 GMT", "elapsed": 0, "failed_when_result": false, "item": "monitor_disk_linux_n", "json": {"success": "Updated testignore2.example.com in the system. Config imported but not yet applied."}, "msg": "OK (104 bytes)", "redirected": false, "server": "Apache/2.4.6 (CentOS) OpenSSL/1.0.2k-fips PHP/5.4.16", "status": 200, "url": "https://nagios.example.com/nagiosxi/api/v1/config/host/testignore2.example.com?apikey=<ourAPIKey>", "x_powered_by": "PHP/5.4.16"}
ok: [localhost] => (item=monitor_swap_n) => {"access_control_allow_methods": "POST, GET, OPTIONS, DELETE, PUT", "access_control_allow_origin": "*", "ansible_loop_var": "item", "changed": false, "connection": "close", "content_length": "104", "content_type": "application/json", "cookies": {}, "cookies_string": "", "date": "Fri, 16 Jul 2021 15:25:42 GMT", "elapsed": 0, "failed_when_result": false, "item": "monitor_swap_n", "json": {"success": "Updated testignore2.example.com in the system. Config imported but not yet applied."}, "msg": "OK (104 bytes)", "redirected": false, "server": "Apache/2.4.6 (CentOS) OpenSSL/1.0.2k-fips PHP/5.4.16", "status": 200, "url": "https://nagios.example.com/nagiosxi/api/v1/config/host/testignore2.example.com?apikey=<ourAPIKey>", "x_powered_by": "PHP/5.4.16"}
ok: [localhost] => (item=operations_critical_linux) => {"access_control_allow_methods": "POST, GET, OPTIONS, DELETE, PUT", "access_control_allow_origin": "*", "ansible_loop_var": "item", "changed": false, "connection": "close", "content_length": "104", "content_type": "application/json", "cookies": {}, "cookies_string": "", "date": "Fri, 16 Jul 2021 15:25:43 GMT", "elapsed": 0, "failed_when_result": false, "item": "operations_critical_linux", "json": {"success": "Updated testignore2.example.com in the system. Config imported but not yet applied."}, "msg": "OK (104 bytes)", "redirected": false, "server": "Apache/2.4.6 (CentOS) OpenSSL/1.0.2k-fips PHP/5.4.16", "status": 200, "url": "https://nagios.example.com/nagiosxi/api/v1/config/host/testignore2.example.com?apikey=<ourAPIKey>", "x_powered_by": "PHP/5.4.16"}

TASK [Apply Configuration] **************************************************************************************************************************************************************************
ok: [localhost] => {"access_control_allow_methods": "POST, GET, OPTIONS, DELETE, PUT", "access_control_allow_origin": "*", "changed": false, "connection": "close", "content_length": "82", "content_type": "application/json", "cookies": {}, "cookies_string": "", "date": "Fri, 16 Jul 2021 15:25:44 GMT", "elapsed": 0, "json": {"command_id": 392, "success": "Apply config command has been sent to the backend."}, "msg": "OK (82 bytes)", "redirected": false, "server": "Apache/2.4.6 (CentOS) OpenSSL/1.0.2k-fips PHP/5.4.16", "status": 200, "url": "https://nagios.example.com/nagiosxi/api/v1/system/applyconfig?apikey=<ourAPIKey>&applyconfig=1", "x_powered_by": "PHP/5.4.16"}
As you can see, the API responds as if it's made the changes, but when I go to check the host, it has zero associated hostgroups. Is there a different method I should be using here?
gsmith
Posts: 1253
Joined: Tue Mar 02, 2021 11:15 am

Re: API - adding hostgroups to host not "taking"

Post by gsmith »

Hi

You are going to need to apply the config twice, once after host configuration, and again
after hostgroup assignment.

Let us know how this works for you.

Thanks
travisjburrell
Posts: 14
Joined: Thu Sep 24, 2020 12:36 pm

Re: API - adding hostgroups to host not "taking"

Post by travisjburrell »

No change in behavior. Everything still completes successfully, but the host still isn't added to any of the hostgroups. I even tried it 2 different ways:
  • adding &applyconfig=1 to the host creation call
  • adding a second call to applyconfig after the host creation call
travisjburrell
Posts: 14
Joined: Thu Sep 24, 2020 12:36 pm

Re: API - adding hostgroups to host not "taking"

Post by travisjburrell »

Here's the output w/the second, separate applyconfig call:

Code: Select all

TASK [Register DCO Host with Nagios] ****************************************************************************************************************************************************************
ok: [localhost] => {"access_control_allow_methods": "POST, GET, OPTIONS, DELETE, PUT", "access_control_allow_origin": "*", "changed": false, "connection": "close", "content_length": "102", "content_type": "application/json", "cookies": {}, "cookies_string": "", "date": "Fri, 16 Jul 2021 17:29:35 GMT", "elapsed": 0, "failed_when_result": false, "json": {"success": "Added testignore2.example.com to the system. Config imported but not yet applied."}, "msg": "OK (102 bytes)", "redirected": false, "server": "Apache/2.4.6 (CentOS) OpenSSL/1.0.2k-fips PHP/5.4.16", "status": 200, "url": "https://nagios.example.com/nagiosxi/api/v1/config/host?apikey=<ourAPIKey>", "x_powered_by": "PHP/5.4.16"}

TASK [Apply Configuration] **************************************************************************************************************************************************************************
ok: [localhost] => {"access_control_allow_methods": "POST, GET, OPTIONS, DELETE, PUT", "access_control_allow_origin": "*", "changed": false, "connection": "close", "content_length": "82", "content_type": "application/json", "cookies": {}, "cookies_string": "", "date": "Fri, 16 Jul 2021 17:29:36 GMT", "elapsed": 0, "json": {"command_id": 440, "success": "Apply config command has been sent to the backend."}, "msg": "OK (82 bytes)", "redirected": false, "server": "Apache/2.4.6 (CentOS) OpenSSL/1.0.2k-fips PHP/5.4.16", "status": 200, "url": "https://nagios.example.com/nagiosxi/api/v1/system/applyconfig?apikey=<ourAPIKey>&applyconfig=1", "x_powered_by": "PHP/5.4.16"}

TASK [Put the New Host Into its Hostgroups - DCO Linux] *********************************************************************************************************************************************
ok: [localhost] => (item=monitor_cpu_n) => {"access_control_allow_methods": "POST, GET, OPTIONS, DELETE, PUT", "access_control_allow_origin": "*", "ansible_loop_var": "item", "changed": false, "connection": "close", "content_length": "104", "content_type": "application/json", "cookies": {}, "cookies_string": "", "date": "Fri, 16 Jul 2021 17:29:37 GMT", "elapsed": 0, "failed_when_result": false, "item": "monitor_cpu_n", "json": {"success": "Updated testignore2.example.com in the system. Config imported but not yet applied."}, "msg": "OK (104 bytes)", "redirected": false, "server": "Apache/2.4.6 (CentOS) OpenSSL/1.0.2k-fips PHP/5.4.16", "status": 200, "url": "https://nagios.example.com/nagiosxi/api/v1/config/host/testignore2.example.com?apikey=<ourAPIKey>", "x_powered_by": "PHP/5.4.16"}
ok: [localhost] => (item=monitor_ram_n) => {"access_control_allow_methods": "POST, GET, OPTIONS, DELETE, PUT", "access_control_allow_origin": "*", "ansible_loop_var": "item", "changed": false, "connection": "close", "content_length": "104", "content_type": "application/json", "cookies": {}, "cookies_string": "", "date": "Fri, 16 Jul 2021 17:29:37 GMT", "elapsed": 0, "failed_when_result": false, "item": "monitor_ram_n", "json": {"success": "Updated testignore2.example.com in the system. Config imported but not yet applied."}, "msg": "OK (104 bytes)", "redirected": false, "server": "Apache/2.4.6 (CentOS) OpenSSL/1.0.2k-fips PHP/5.4.16", "status": 200, "url": "https://nagios.example.com/nagiosxi/api/v1/config/host/testignore2.example.com?apikey=<ourAPIKey>", "x_powered_by": "PHP/5.4.16"}
ok: [localhost] => (item=monitor_disk_linux_n) => {"access_control_allow_methods": "POST, GET, OPTIONS, DELETE, PUT", "access_control_allow_origin": "*", "ansible_loop_var": "item", "changed": false, "connection": "close", "content_length": "104", "content_type": "application/json", "cookies": {}, "cookies_string": "", "date": "Fri, 16 Jul 2021 17:29:38 GMT", "elapsed": 0, "failed_when_result": false, "item": "monitor_disk_linux_n", "json": {"success": "Updated testignore2.example.com in the system. Config imported but not yet applied."}, "msg": "OK (104 bytes)", "redirected": false, "server": "Apache/2.4.6 (CentOS) OpenSSL/1.0.2k-fips PHP/5.4.16", "status": 200, "url": "https://nagios.example.com/nagiosxi/api/v1/config/host/testignore2.example.com?apikey=<ourAPIKey>", "x_powered_by": "PHP/5.4.16"}
ok: [localhost] => (item=monitor_swap_n) => {"access_control_allow_methods": "POST, GET, OPTIONS, DELETE, PUT", "access_control_allow_origin": "*", "ansible_loop_var": "item", "changed": false, "connection": "close", "content_length": "104", "content_type": "application/json", "cookies": {}, "cookies_string": "", "date": "Fri, 16 Jul 2021 17:29:39 GMT", "elapsed": 0, "failed_when_result": false, "item": "monitor_swap_n", "json": {"success": "Updated testignore2.example.com in the system. Config imported but not yet applied."}, "msg": "OK (104 bytes)", "redirected": false, "server": "Apache/2.4.6 (CentOS) OpenSSL/1.0.2k-fips PHP/5.4.16", "status": 200, "url": "https://nagios.example.com/nagiosxi/api/v1/config/host/testignore2.example.com?apikey=<ourAPIKey>", "x_powered_by": "PHP/5.4.16"}
ok: [localhost] => (item=operations_critical_linux) => {"access_control_allow_methods": "POST, GET, OPTIONS, DELETE, PUT", "access_control_allow_origin": "*", "ansible_loop_var": "item", "changed": false, "connection": "close", "content_length": "104", "content_type": "application/json", "cookies": {}, "cookies_string": "", "date": "Fri, 16 Jul 2021 17:29:39 GMT", "elapsed": 0, "failed_when_result": false, "item": "operations_critical_linux", "json": {"success": "Updated testignore2.example.com in the system. Config imported but not yet applied."}, "msg": "OK (104 bytes)", "redirected": false, "server": "Apache/2.4.6 (CentOS) OpenSSL/1.0.2k-fips PHP/5.4.16", "status": 200, "url": "https://nagios.example.com/nagiosxi/api/v1/config/host/testignore2.example.com?apikey=<ourAPIKey>", "x_powered_by": "PHP/5.4.16"}

TASK [Apply Configuration] **************************************************************************************************************************************************************************
ok: [localhost] => {"access_control_allow_methods": "POST, GET, OPTIONS, DELETE, PUT", "access_control_allow_origin": "*", "changed": false, "connection": "close", "content_length": "82", "content_type": "application/json", "cookies": {}, "cookies_string": "", "date": "Fri, 16 Jul 2021 17:29:40 GMT", "elapsed": 0, "json": {"command_id": 443, "success": "Apply config command has been sent to the backend."}, "msg": "OK (82 bytes)", "redirected": false, "server": "Apache/2.4.6 (CentOS) OpenSSL/1.0.2k-fips PHP/5.4.16", "status": 200, "url": "https://nagios.example.com/nagiosxi/api/v1/system/applyconfig?apikey=<ourAPIKey>&applyconfig=1", "x_powered_by": "PHP/5.4.16"}
Forgot to mention that we're on 5.8.3, if that makes a difference.
gsmith
Posts: 1253
Joined: Tue Mar 02, 2021 11:15 am

Re: API - adding hostgroups to host not "taking"

Post by gsmith »

hi,

Thinking out loud here ... I am guessing Ansible is pretty quick at issuing those commands, Is there
a way you can can change things so it resembles:
1. add new host
2. apply configuration

3. sleep for 60 seconds
- or better yet -
3. check for existence of host until host is found

4. assign host to hostgroup
5. apply config
For testing purposes you could manually simulate step 3 if
you can split the ansible code into two distinct pieces that you can fire at will.

Let me know your thoughts - and/or results.

Have a good weekend.
travisjburrell
Posts: 14
Joined: Thu Sep 24, 2020 12:36 pm

Re: API - adding hostgroups to host not "taking"

Post by travisjburrell »

Ok, so yes, the execution is quick, but the system is also registering the instance just as quickly. I put in a 2sec delay, followed by a host check (GET config/host w/a host_name filter), & the check returns successfully every time, yet the host group assignments still fail.

Even if I introduce a 60sec delay, I get the same results. So, for the record, here's the current code:

Code: Select all

- name: Register Host with Nagios
  ansible.builtin.uri:
    url: https://{{ xi_ip }}/nagiosxi/api/v1/config/host?apikey={{ xi_api_key }}
    body_format: form-urlencoded
    body: "{{ register_host | combine({
      'host_name':  host_name,
      'address': host_name,
      'contact_groups': 'IT'
      }) }}"
    method: POST
    validate_certs: no
  register: registration
  failed_when: registration.json.error is defined

- name: Apply Configuration
  ansible.builtin.uri:
    url: https://{{ xi_ip }}/nagiosxi/api/v1/system/applyconfig?apikey={{ xi_api_key }}&applyconfig=1
    method: POST
    validate_certs: no
  register: apply

- name: Wait for Host Registration
  ansible.builtin.pause:
    seconds: 60

- name: Check for Host Registration
  ansible.builtin.uri:
    url: https://{{ xi_ip }}/nagiosxi/api/v1/config/host?apikey={{ xi_api_key }}&host_name={{ host_name }}
    validate_certs: no
  register: host_check
  until: host_check.json[0].register == '1'
  retries: 5
  delay: 5

- name: Put the New Host Into its Hostgroups - DCO Linux
  ansible.builtin.uri:
    url: https://{{ xi_ip }}/nagiosxi/api/v1/config/host/{{ host_name }}?apikey={{ xi_api_key }}
    body_format: form-urlencoded
    body:
      hostgroups: "{{ item }}"
    method: PUT
    validate_certs: no
  loop: "{{ critical_operations_linux | flatten }}"
  register: registration
  failed_when: registration.json.error is defined
  when: windows_host == 'no'

- name: Apply Configuration
  ansible.builtin.uri:
    url: https://{{ xi_ip }}/nagiosxi/api/v1/system/applyconfig?apikey={{ xi_api_key }}&applyconfig=1
    method: POST
    validate_certs: no
Matter of fact, after I formatted the above code to paste in to this message (took a couple of mins), I ran that same playbook again (host had been in the system the entire time), which, again, completed without errors, and the host still wasn't placed into its host groups.
gsmith
Posts: 1253
Joined: Tue Mar 02, 2021 11:15 am

Re: API - adding hostgroups to host not "taking"

Post by gsmith »

Hi

I just got Ansible running and am having some issues getting your playbook to work, but I can work those
details out later. Iam a noob when it comes to Ansible

Can you please do the following:
1. delete the host (that Ansible added) from Nagios via Configure, Core Config Manager, Hosts
2. run your playbook again
3. let me know if it adds the host
4. go to Admin, under System Administration on the left pick Audit Log
5. in the upper right corner there's a download button. Please download the log in csv format and send it to me.

Thanks
travisjburrell
Posts: 14
Joined: Thu Sep 24, 2020 12:36 pm

Re: API - adding hostgroups to host not "taking"

Post by travisjburrell »

Hey again - it did add it, but apparently we're on 5.8.3 standard, so no audit log :|
gsmith
Posts: 1253
Joined: Tue Mar 02, 2021 11:15 am

Re: API - adding hostgroups to host not "taking"

Post by gsmith »

Hi

5.8.3 does have the Audit Log:
Image2.jpg
go to Admin, under System Administration on the left pick Audit Log (see above)
In the upper right corner there's a download button. Please download the log in csv format and send it to me.

Thanks
You do not have the required permissions to view the files attached to this post.
travisjburrell
Posts: 14
Joined: Thu Sep 24, 2020 12:36 pm

Re: API - adding hostgroups to host not "taking"

Post by travisjburrell »

Unfortunately not. I'm on 5.8.3 standard. I get a pop-up that says, "UPGRADE TO ENTERPRISE EDITION TO UNLOCK THIS FEATURE."

Regardless, I think I figured it out. I was adding the host groups individually, looping through a list, using the "form-urlencode" body type of the URI module. What I did instead was to add the entire list at once, and instead of using form-urlencode, I manually coded the URL to pass it. For some reason, it seems the Nagios API doesn't take too kindly to that module method.

The play ended up being:

Code: Select all

- name: Put the New Host Into its Hostgroups - DCO Linux
  ansible.builtin.uri:
    url: "https://{{ xi_ip }}/nagiosxi/api/v1/config/\
      host/{{ host_name }}&hostgroups=\
      {{ critical_operations_linux | flatten | join (',') }}\
      ?apikey={{ xi_api_key }}"
    method: PUT
    validate_certs: no
  register: registration
  failed_when: registration.json.error is defined
I used the flatten filter to flatten a nested list, then the join filter to push the format that the API wanted (e.g. hostgroups=group1,group2,group3, etc).

Oh, also, no pause necessary :)
Locked