pyinfra icon indicating copy to clipboard operation
pyinfra copied to clipboard

Easier api initialization of inventory/hosts

Open fliepeltje opened this issue 1 year ago • 0 comments

Problem

I am interested in using pyinfra api, but I am having a hard time initializing a host to perform operations on. When I initialize a host/inventory combination with the classes provided in the API, I run into the issue that I cannot connect, because I am lacking a state. If I attach a default state object, it still does not allow me to connect. My current workaround has been to use the make_inventory_from_func that is included in the latest cli version, so something like:

from pyinfra_cli.inventory import make_inventory_from_func

inv = make_inventory_from_func(lambda: {"example": ["example.com"]})
host = inv.get_host("example.com")
...

from this point onward I can do most things I would like.

Proposed solution

Ideally I would like to be able to manage a single host without inventory, something like:

from pyinfra.api.host import Host

host = Host("example.com", ...)

# Or to not clash with the current api:

host = Host.create("example.com", ...)
...

Similarly so, I would like to be able to setup an inventory programmatically:


host_a = Host(...)
host_b = Host(...)
inv = Inventory(hosts=[host_a, host_b])

# Or to match the current api more

inv = Inventory()
host = Host(inventory=inv, ...)

Basically I want to avoid having to figure out all the less-visible things that are associated with the initialization of a Host.

I think adding classmethods for Inventory and Host to abstract initialization of the less-visible objects would be the way to go. I would love to write an implementation for it too, but would like some feedback from you first. Specifically I would like to know:

  • Is it reasonable to init hosts without an inventory or would that be introducing problems with the rest of the api?
  • Are there any (non-)obvious gotchas to consider when developing this?
  • Could you very briefly describe the requirements for a host to connect? I will figure out through the code as well, but would be nice to have a sanity check.

Please let me know if you think this might be useful and if I can help with it :)

fliepeltje avatar Apr 06 '23 08:04 fliepeltje