API - adding hostgroups to host not "taking"
Posted: Fri Jul 16, 2021 11:24 am
I'm using Ansible's URI module to make multiple calls to our Nagios server API. Basically the equivalent of multiple cURL calls.
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):
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?
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
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"}