Skip to content

Instantly share code, notes, and snippets.

@b-mc
Last active August 6, 2021 12:56
Show Gist options
  • Save b-mc/7ef834c0e51dfa77428dbbd30800fd44 to your computer and use it in GitHub Desktop.
Save b-mc/7ef834c0e51dfa77428dbbd30800fd44 to your computer and use it in GitHub Desktop.
Ansible Playbook: Schedule Nagios Downtime

Ansible Playbook: Schedule Nagios Downtime

About

The main advantage here is you don't have to add the affected hosts to the Ansible inventory — useful when hosting e.g. an independently managed application. The start time is conveniently converted to an epoch since this is what the Nagios module expects.

Playbook

- name: schedule nagios downtime
  hosts: nagios
  gather_facts: no
  vars:
    duration: 240
    start_time: "{{ ('2021-05-01 23:00:00'|to_datetime).strftime('%s') }}"
    comment: "Scheduled maintenance"
    author: "admin"
    downtime_hosts:
      - host1
      - host2
      - host3
      - host4

  tasks:
  - name: schedule downtime for hosts
    community.general.nagios:
      action: downtime
      minutes: "{{ duration }}"
      start: "{{ start_time }}"
      host: "{{ item }}"
      service: host
      comment: "{{ comment }}"
      author: "{{ author }}"
    delegate_to: "{{ inventory_hostname }}"
    loop: "{{ downtime_hosts }}"
  - name: schedule downtime for services
    community.general.nagios:
      action: downtime
      minutes: "{{ duration }}"
      start: "{{ start_time }}"
      host: "{{ item }}"
      service: all
      comment: "{{ comment }}"
      author: "{{ author }}"
    delegate_to: "{{ inventory_hostname }}"
    loop: "{{ downtime_hosts }}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment