pyinfra-dev / pyinfra

pyinfra turns Python code into shell commands and runs them on your servers. Execute ad-hoc commands and write declarative operations. Target SSH servers, local machine and Docker containers. Fast and scales from one server to thousands.
https://pyinfra.com
MIT License
3.84k stars 374 forks source link

Use python function for inventory #959

Open Fizzadar opened 1 year ago

Fizzadar commented 1 year ago

We can already do this with deploys/operations:

pyinfra inventory.py my_python_module.my_op

So why not inventory?

mechanarchy commented 1 month ago

This was added in ed8196de7387dd1e5a8d3ab85edc7090bb31158f; there are no docs (fair enough, it's experimental) but I feel like this simplifying use-case of inventory_module.host_group would be beneficial. Consider the following inventory.py contents:

proxmox_hosts = ["pve01", "pve02", "pve03"]
access_points = ["unifi01", "unifi02", "unifi03"]

It would be nice to use pyinfra inventory.proxmox_hosts deploy.py instead of the more verbose and potentially more error prone pyinfra inventory.py deploy.py --limit proxmox_hosts.

Currently the code assumes that this argv syntax always refers to a callable, I think checking if the user parameter is an iterable would be a reasonably simple addition and would enhance UX.

https://github.com/pyinfra-dev/pyinfra/blob/511e7c3a26af1d83bc4e6ef5113c1d06827dd0cd/pyinfra_cli/inventory.py#L134-L143

mechanarchy commented 1 month ago

Ah, I was wondering where I originally saw this documented. It's here: https://github.com/pyinfra-dev/pyinfra-examples/tree/main/inventory-functions