Page 1 of 2

API not working as expected in Ansible

Posted: Thu Jul 11, 2019 9:50 am
by dfmco
Nagios 5.6.2 on CentOS 7 using RPM install.

This command seems to work fine and adds the user with all settings:
"http://<REDACTED>/nagiosxi/api/v1/config/contact?apikey=<REDACTED>&pretty=1" -d "contact_name=pagerduty&host_notifications_enabled=1&service_notifications_enabled=1&host_notification_period=24x7&service_notification_period=24x7&host_notification_options=d,r&service_notification_options=w,u,c,f,r&host_notification_commands=notify-host-by-pagerduty&service_notification_commands=notify-service-by-pagerduty&applyconfig=1"

I tried to send this from Ansible and the contact is created but set to disabled so the rest of the configuration is not applied. I tried splitting the command into 2 parts to create the user first then configure but the user is still not enabled so the settings task does not work.

- name: Create pagerduty contact in Nagios XI
uri:
url: http://{{ ansible_host }}/nagiosxi/api/v1/config/command?apikey={{ API_Key }}&pretty=1
body: 'contact_name=pagerduty&applyconfig=1'
method: POST
validate_certs: no
timeout: 30

- name: Assign configurations to pagerduty contact in Nagios XI
uri:
url: http://{{ ansible_host }}/nagiosxi/api/v1/config/command?apikey={{ API_Key }}&pretty=1
body: 'contact_name=pagerduty&host_notifications_enabled=1&service_notifications_enabled=1&host_notification_period=24x7&service_notification_period=24x7&host_notification_options=d,r&service_notification_options=w,u,c,f,r&host_notification_commands=notify-host-by-pagerduty&service_notification_commands=notify-service-by-pagerduty&applyconfig=1'
method: POST
validate_certs: no
timeout: 30

How can I create and ENABLE a user from Ansible using the API?

Re: API not working as expected in Ansible

Posted: Thu Jul 11, 2019 11:46 am
by lmiltchev
I tried adding a contact in Nagios XI 5.6.4 (CentOS 7) via Ansible, and didn't have any issues. My contact is enabled, and configuration applied. I didn't use variables for the IP and the API key in my playbook, and used otify-host-by-email and notify-service-by-email commands as I don't have pagerduty commands defined. Anyway, here is my playbook:

Code: Select all

---
- name: Test
  hosts: xicentos
  connection: ssh
  gather_facts: True

  tasks:
  - name: Add contact
    uri:
      url: http://192.168.18.1/nagiosxi/api/v1/config/contact?apikey=xxx&pretty=1
      body: 'contact_name=pagerduty&host_notifications_enabled=1&service_notifications_enabled=1&host_notification_period=24x7&service_notification_period=24x7&host_notification_options=d,r&service_notification_options=w,u,c,f,r&host_notification_commands=notify-host-by-email&service_notification_commands=notify-service-by-email&applyconfig=1'
      method: POST
      validate_certs: no
      timeout: 30
More info:
example01.PNG
example02.PNG
example03.PNG
You may want to upgrade your Nagios XI to 5.6.4 and give it a try. You are entitled to a test server, so try it first in a test environment before applying it to production.

https://support.nagios.com/kb/article/n ... s-145.html

Re: API not working as expected in Ansible

Posted: Thu Jul 11, 2019 4:54 pm
by dflick
I will push out a test instance and see where it goes from there.

Re: API not working as expected in Ansible

Posted: Thu Jul 11, 2019 5:00 pm
by lmiltchev
Sure. You can also try "hard-coding" the IP and the API key, just to rule out an issue with the way variables are used in the playbook. Let us know how it goes.

Re: API not working as expected in Ansible

Posted: Tue Aug 06, 2019 3:07 pm
by dfmco
A fresh RPM build on 5.6.3 is failing to update to 5.6.5 so I have an open ticket for that.

Re: API not working as expected in Ansible

Posted: Tue Aug 06, 2019 4:58 pm
by lmiltchev
Noted. Do you have any further questions on the original (API) issue or it's ok to close this topic?

Re: API not working as expected in Ansible

Posted: Tue Aug 06, 2019 5:33 pm
by dfmco
I want to revisit this as soon as we get updated to the version you say is working. Please hold this open.

Thanks!

Re: API not working as expected in Ansible

Posted: Wed Aug 07, 2019 8:40 am
by lmiltchev
Sounds good. We will keep the topic open for the time being.

Re: API not working as expected in Ansible

Posted: Wed Aug 07, 2019 8:34 pm
by dfmco
Version update is working so I ran the play again and the contact is still showing up as inactive. Here is the play:
- name: Create pagerduty contact in Nagios XI
uri:
url: http://{{ ansible_host }}/nagiosxi/api/v1/config/command?apikey={{ NagiosAPIKey }}&pretty=1
body: 'contact_name=pagerduty&applyconfig=1'
method: POST
validate_certs: no
timeout: 30

The user is created but left inactive.

Re: API not working as expected in Ansible

Posted: Thu Aug 08, 2019 10:32 am
by lmiltchev
First off, I am not sure if this is a typo, but you have this:
url: http://{{ ansible_host }}/nagiosxi/api/v1/config/command?apikey={{ NagiosAPIKey }}&pretty=1
but you should have this:
url: http://{{ ansible_host }}/nagiosxi/api/v1/config/contact?apikey={{ NagiosAPIKey }}&pretty=1
Second, you cannot add a contact successfully, when you are NOT passing ALL of the required options...
In the "body", you have:
body: 'contact_name=pagerduty&applyconfig=1'
but you should have a lot more than that...
example01.PNG
I showed you my playbook, which is working. Can you try using it as I showed you?