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.85k stars 374 forks source link

Access inventories not currently loaded. #529

Open gnat opened 3 years ago

gnat commented 3 years ago

Is your feature request related to a problem? Please describe. There does not seem to be an obvious way to access inventories other than the one pyinfra is currently operating on.

Would be great to be able to access the host list inside of inventories/production.py while running against inventories/staging.py for example.

Useful case 1: Do fancy auto-configuration (firewalls...) easily using host information inside different inventories. Useful case 2: Can test scripts using @local, etc. easier.

Describe the solution you'd like Be able to reach data in other inventories in some way, maybe just allow import ..inventories.production ? Unsure what would be the best approach.

Thanks!!

gnat commented 3 years ago

For those following this, here's one method that works, although perhaps not the most elegant or API stable:

from pyinfra_cli.util import exec_file
production = exec_file('inventories/production.py')
print(f"{production['hosts']}")
Fizzadar commented 3 years ago

Big fan of this idea, @gnat! Will have a think about how best to implement.