pyinfra icon indicating copy to clipboard operation
pyinfra copied to clipboard

Syntactically correct python in inventory file fails if not matching inventory specification

Open themanifold opened this issue 3 years ago • 1 comments

If you specify a syntactically python inventory file, but also one that does not match what pyinfra requires in its inventory spec, you'll see no error which is confusing.

For example, an inventory file (called bad_inventories.py) can look like this:

app_server = [
    'a',
    'b', ({"foo": "bar"})    
]

which is incorrect as the bracket for specifying data associated with the host 'b' is in the wrong place so that there's a third host that is a tuple.

If you run the above snippet with pyinfra as an inventory file, you'll see:

$ pyinfra --debug -vvvv inventories/bad_inventory.py debug-inventory
    [pyinfra_cli.main] Checking potential directory: inventories
    [pyinfra_cli.main] Deploy directory remains as cwd
--> Loading config...
--> Loading inventory...
    [pyinfra_cli.inventory] Creating fake inventory...

Obviously, the inventory file should look like this:

app_server = [
    'a',
    ('b', {"foo": "bar"})    
]

but we should protect the users by having some more validation on the inventory file format. If there's a subset of python we accept, then it needs validation.

themanifold avatar Sep 22 '21 08:09 themanifold

This is super important, a real gotcha without any validation currently.

Fizzadar avatar Sep 25 '21 10:09 Fizzadar