XI and automate downtime via ansible

This support forum board is for support questions relating to Nagios XI, our flagship commercial network monitoring solution.
Post Reply
kbauma01
Posts: 43
Joined: Wed May 25, 2022 6:39 am

XI and automate downtime via ansible

Post by kbauma01 »

Hello all,

I'm trying to automate downtime using an ansible playbook and I am getting errors. I'm using the nagios user, which has the rights and keys to access the servers, and I can only get it to work on one server which is the nagios server itself. I tried copying in the nagios.cfg file but that doesn't seem to help.

Any ideas?


TASK [Gathering Facts] *********************************************************
ok: [server02]
ok: [server01]
ok: [nagserver]

TASK [Schedule an hour of host downtime] ***************************************
fatal: [server02]: FAILED! => {"changed": false, "cmdfile": "/usr/local/nagios/var/rw/nagios.cmd", "msg": "nagios command file does not exist"}
changed: [nagserver] => {"changed": true, "nagios_commands": ["[1718712031] SCHEDULE_HOST_DOWNTIME;bnagp03vr;1718712031;1718715631;1;0;3600;Ansible;Scheduling downtime"]}
fatal: [server01]: FAILED! => {"changed": false, "msg": "unable to locate nagios.cfg"}

PLAY RECAP *********************************************************************
nagserver : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
server01 : ok=1 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
server02. : ok=1 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
User avatar
swolf
Developer
Posts: 361
Joined: Tue Jun 06, 2017 9:48 am

Re: XI and automate downtime via ansible

Post by swolf »

Hi @kbauma01, thanks for reaching out!

From the errors you posted, it looks like the playbook in question is set up specifically to run on the Nagios server - even if you copy over the configuration, it's expecting to submit a command to nagios.cmd, which is local to the Nagios Core filesystem. If you copy that file over, Nagios Core won't have the ability to read that command file.

I think you want the job to be set up such that running the playbook to schedule downtime for a server causes the job to run on the Nagios server, using the other server's name or address as an argument/as data. Does that make sense? Let me know what you think.

-Sebastian
Developer @ Nagios 2017-05-15 thru 2024-08-06
kbauma01
Posts: 43
Joined: Wed May 25, 2022 6:39 am

Re: XI and automate downtime via ansible

Post by kbauma01 »

Here is my playbook. And yes, it's basic. I can make it pretty after :lol:

I'm running this on a server that has access to all my servers. Should I be running that as root or as nagios?

---
##########################
# Used to downtime a host#
##########################

- name: Test downtime
hosts: nagios_downtime

tasks:
- name: Schedule an hour of host downtime
community.general.nagios:
action: downtime
minutes: 60
service: host
host: '{{ inventory_hostname }}'
miajackson111
Posts: 1
Joined: Wed Jun 19, 2024 10:33 pm

Re: XI and automate downtime via ansible

Post by miajackson111 »

The error message "nagios command file does not exist" on server02 suggests the Ansible user might not have read access to the /usr/local/nagios/var/rw/nagios.cmd file. love tester
Check the permissions of these files on both servers using the ls -l command on the Nagios server and ensuring the Ansible user has the necessary read permissions. You might need to adjust permissions with chmod if necessary.
You can use the delegate_to option in your Ansible playbook to run the nagios module on the Nagios server, which should already have the necessary configuration and permissions. Here's an example:
YAML
- name: Schedule downtime for servers
nagios:
action: downtime
host: "{{ item }}"
minutes: 60 # Change to desired downtime duration
delegate_to: nagserver
with_items:
- server01
- server02
kbauma01
Posts: 43
Joined: Wed May 25, 2022 6:39 am

Re: XI and automate downtime via ansible

Post by kbauma01 »

That helped a lot, thanks @miajackson11!

Now I have to figure out how to have ansible use different hosts file for each job. I'm grateful for the assistance everyone!
Mikasa23
Posts: 13
Joined: Thu Jul 06, 2023 3:12 am

Re: XI and automate downtime via ansible

Post by Mikasa23 »

Before running the Ansible playbook, manually execute the Nagios command on the Nagios server using the nagios command-line tool. This will help isolate any issues with the command itself. Block Blast

Code: Select all

- name: Schedule Host Downtime
  hosts: all
  become: true

  tasks:
    - name: Schedule downtime for 1 hour
      nagios:
        state: present
        command: SCHEDULE_HOST_DOWNTIME
        host: bnagp03vr
        duration: 3600
        author: Ansible
        comment: Scheduling downtime
ZaneTapia
Posts: 4
Joined: Fri Aug 30, 2024 1:45 am

Re: XI and automate downtime via ansible

Post by ZaneTapia »

This error indicates that the playbook cannot locate the nagios.cfg file on server01.idle breakout This file is essential because it specifies the Nagios configuration, including paths to critical resources like the command file.
Post Reply