API not working as expected in Ansible

This support forum board is for support questions relating to Nagios XI, our flagship commercial network monitoring solution.
dfmco
Posts: 257
Joined: Wed Dec 04, 2013 11:05 am

API not working as expected in Ansible

Post 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?
User avatar
lmiltchev
Bugs find me
Posts: 13589
Joined: Mon May 23, 2011 12:15 pm

Re: API not working as expected in Ansible

Post 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
You do not have the required permissions to view the files attached to this post.
Be sure to check out our Knowledgebase for helpful articles and solutions!
dflick
Posts: 72
Joined: Tue Nov 12, 2013 3:16 pm

Re: API not working as expected in Ansible

Post by dflick »

I will push out a test instance and see where it goes from there.
User avatar
lmiltchev
Bugs find me
Posts: 13589
Joined: Mon May 23, 2011 12:15 pm

Re: API not working as expected in Ansible

Post 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.
Be sure to check out our Knowledgebase for helpful articles and solutions!
dfmco
Posts: 257
Joined: Wed Dec 04, 2013 11:05 am

Re: API not working as expected in Ansible

Post 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.
User avatar
lmiltchev
Bugs find me
Posts: 13589
Joined: Mon May 23, 2011 12:15 pm

Re: API not working as expected in Ansible

Post by lmiltchev »

Noted. Do you have any further questions on the original (API) issue or it's ok to close this topic?
Be sure to check out our Knowledgebase for helpful articles and solutions!
dfmco
Posts: 257
Joined: Wed Dec 04, 2013 11:05 am

Re: API not working as expected in Ansible

Post 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!
User avatar
lmiltchev
Bugs find me
Posts: 13589
Joined: Mon May 23, 2011 12:15 pm

Re: API not working as expected in Ansible

Post by lmiltchev »

Sounds good. We will keep the topic open for the time being.
Be sure to check out our Knowledgebase for helpful articles and solutions!
dfmco
Posts: 257
Joined: Wed Dec 04, 2013 11:05 am

Re: API not working as expected in Ansible

Post 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.
User avatar
lmiltchev
Bugs find me
Posts: 13589
Joined: Mon May 23, 2011 12:15 pm

Re: API not working as expected in Ansible

Post 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?
You do not have the required permissions to view the files attached to this post.
Be sure to check out our Knowledgebase for helpful articles and solutions!
Locked