Nagios XI downtime, use different list of servers every time

Hello all!

I’m trying to automate servers being put into downtime mode in Nagios XI (2024R1.1.3). I want to use an inventory file that changes, using a file that just has a list of servers generated the day before the patching event. I don’t want to have to update the /etc/ansible/hosts file every time, there will be 100’s of servers in each region.

For example:
dev_file:
server1
server 2

prod_file:
server3
server4

I’m using ansible 2.9.27 on RedHat 8.10

Thanks!

If you have control over the file generation, you can format the files like this

# assume this is called dev_file
all:
  hosts:
    server1:
    server2:

then run ansible-playbook -i dev_file ......

If you dont have control over the file, you can use a quick and dirty bash command:
ansible-playbook -i "$(cat dev_file | tr '\n' ', ')" ......

You can also write a custom inventory plugin. As an example, use the python script below. The name my_plugin can be whatever makes sense for you, just make sure the filename matches.
The file should be placed somewhere on the search path, as described here
https://docs.ansible.com/ansible/latest/reference_appendices/config.html#default-inventory-plugin-path

# my_plugin.py
from ansible.plugins.inventory import BaseInventoryPlugin

class InventoryModule(BaseInventoryPlugin):

    NAME = 'my_plugin'

    def parse(self, inventory, loader, path, cache=True):
        super(InventoryModule, self).parse(inventory, loader, path)
        with open(path, 'r') as f:
            for line in f:
                self.inventory.add_host(f)

Then you can run ansible-playbook -i dev_file .... and it will read the file automatically.

To be clear, this is assuming the files look like this

server1
server2
server3

If the format is different then my examples wont quite work.