Page 1 of 1

API Calls for external commands with Ansible

Posted: Tue Feb 11, 2020 5:34 pm
by dfmco
I am trying to run an external command to shut off notifications before an event on a remote Nagios XI server via Ansible.

Here is the playbook:

Code: Select all

# patch_nagios.yml -I am using the proposed name here. 
- hosts: '{{ target }}'
  vars_prompt:
   - name: "target"
     prompt: "Enter the Nagios hostname to patch"
     private: no
  tasks:
    # Disable Nagios notifications for patching
    - name: Disable Nagios XI Notifications
      uri:
        url: http://{{ target }}/nagiosxi/api/v1/system/corecommand?apikey={{ NagiosAPIKey }}
        method: POST
        body: 'DISABLE_NOTIFICATIONS'
        force_basic_auth: yes
        status_code: 201
...
And here is the response:

Code: Select all

TASK [Disable Nagios XI Notifications] ****************************************************************************************************************************
fatal: [onshore-tim-netmon-dev]: FAILED! => {"access_control_allow_methods": "*", "access_control_allow_orgin": "*", "changed": false, "connection": "close", "content": "{\"error\":\"You must specify an external command.\"}\n", "content_length": "50", "content_type": "application/json", "cookies": {}, "cookies_string": "", "date": "Tue, 11 Feb 2020 22:30:35 GMT", "elapsed": 0, "json": {"error": "You must specify an external command."}, "msg": "Status code was 200 and not [201]: OK (50 bytes)", "redirected": false, "server": "Apache/2.4.6 (CentOS) OpenSSL/1.0.2k-fips PHP/5.4.16", "status": 200, "url": "http://onshore-tim-netmon-dev/nagiosxi/api/v1/system/corecommand?apikey=REDACTED", "x_powered_by": "PHP/5.4.16"}
It seems it does not like the format of the external command. Any ideas on how to do this?

Re: API Calls for external commands with Ansible

Posted: Tue Feb 11, 2020 6:02 pm
by scottwilkerson
Your 'body' would likely need to be

Code: Select all

        body: 'cmd=DISABLE_NOTIFICATIONS'

Re: API Calls for external commands with Ansible

Posted: Tue Feb 11, 2020 7:34 pm
by dfmco
That was what I was missing! Thanks!

Re: API Calls for external commands with Ansible

Posted: Wed Feb 12, 2020 8:00 am
by scottwilkerson
dfmco wrote:That was what I was missing! Thanks!
Great!

Locking thread