pyinfra-dev / pyinfra-dev/pyinfra

Easier api initialization of inventory/hosts

Open
#974 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

API spec
Dominant language
Python
Stars
6k
Forks
548
Avg merge
7d 17h
Merged PRs (30d)
13

Description

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 :)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with pyinfra_cli.inventory.make_inventory_from_func and the API Host and Inventory classes mentioned in the issue. Trace the objects required for a host to connect and compare the CLI-created inventory with direct construction. A suitable implementation would provide documented programmatic host and inventory initialization while preserving connection behavior, with tests covering the proposed examples.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
infrastructure
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.