Page 1 of 1

[SOLVED]NXI 5.5.2 - Working with API and Ansible

Posted: Mon Apr 08, 2019 9:57 am
by Aezox
Hi everyone !

I'm working on a project to automatically deploy Nagios agent and declare new hosts to Nagios using Ansible.

For the declaration part I'm working with APIs using POST method (as defined in the "Help" part of Nagios XI)
I've found one blocking point that someone may have the answer.
When assigning hostgroups to my host I want to automatically set inherit option ("+" ticked in the Manage hostgroups) but I cannot handle myself.
I've try hardcoding "+" on the POST method but nothing works with.
Anyone has a clue please ?

Code: Select all

---
- name: Sending host configuration into Nagios without restarting
  uri:
    url: https://MyNagiosAddress/nagiosxi/api/v1/config/host?apikey=MyAPIKey&pretty=1
    body: 'host_name={{ ansible_hostname }}&alias={{ ansible_fqdn }}&address={{ ansible_default_ipv4.address }}&use={{ nagios_host_template.azure[ansible_local.vdc.location] }}&hostgroups={{ nagios_hostgroup.azure[ansible_local.vdc.location] }}&display_name={{ nagios_host_dp.azure.other }}&max_check_attempts=2&check_period={{ nagios_host_chk_period.azure[ansible_local.vdc.location] }}&contacts=contact_info&notification_interval=1440&notification_period={{ nagios_host_not_period.azure[ansible_local.vdc.location] }}&applyconfig=0'
    method: POST
    validate_certs: no
    timeout: 30
It's not a real blocking point but I would like to find a solution to it

Thank you for your help in avance !

Re: NXI 5.5.2 - Working with API and Ansible

Posted: Mon Apr 08, 2019 1:42 pm
by swolf
Hi @Aezox,

How exactly did you try to encode the '+'? Following the object inheritance document ("Additive Inheritance of String Values"), you should be able to do what you describe by adding the '+' onto the front of the "hostgroups" specification. I'm not sure if this would work using the config/host endpoint, but it should work using config/import.

Re: NXI 5.5.2 - Working with API and Ansible

Posted: Tue Apr 09, 2019 2:28 am
by Aezox
Hi @swolf,

I did try to put '+' in both below ways :
set '+' after '=' -> &hostgroups=+{{ nagios_hostgroup.azure[ansible_local.vdc.location] }}
set '+' in my "nagios_hostgroup.azure" variable -> nagios_hostgroup.azure = +NameOfMyHostgroup

In both cases it didn't work as expect

Re: NXI 5.5.2 - Working with API and Ansible

Posted: Tue Apr 09, 2019 4:45 pm
by ssax
Try using %2B (url encoded) instead of the +.

https://meyerweb.com/eric/tools/dencoder/

Re: NXI 5.5.2 - Working with API and Ansible

Posted: Wed Apr 10, 2019 2:43 am
by Aezox
Hi @ssax !
ssax wrote:Try using %2B (url encoded) instead of the +.

https://meyerweb.com/eric/tools/dencoder/
It works ! Thank you !

Re: NXI 5.5.2 - Working with API and Ansible

Posted: Wed Apr 10, 2019 7:36 am
by scottwilkerson
Aezox wrote:Hi @ssax !
ssax wrote:Try using %2B (url encoded) instead of the +.

https://meyerweb.com/eric/tools/dencoder/
It works ! Thank you !
Great!

Locking thread