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.
- 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 }}"