mdraevich
mdraevich

Reputation: 408

How to pass custom arguments into Config file (for generating client-side assets)?

Often projects generate some client-side artifacts before running automations. So, in our case we'd like to generate some build files, then push them to remote servers by Pyinfra automation.

In order to achieve that we use:

The thing is that we'd like to generate a build artifact according to the argument provided in CLI:

pyinfra --data build-version=v1.0 --config prepare.py inventory.py deploy.py

I didn't find any example how to access build-version in prepare.py script. Is it possible to access build-version in prepare.py config script somehow?

What have you tried?

I've tried to from pyinfra import host, but host object lacks of data field (seems like inventory is not initialized).

Upvotes: 0

Views: 69

Answers (1)

Staccato
Staccato

Reputation: 658

Is it possible to access build-version in prepare.py config script somehow?

Yes. To obtain your argument from CLI, the argument must be declared in the prepare.py script(or possibly anywhere data/group_data exists) and accessed as host.data.argument_name. See sample below:

from pyinfra import host
from pyinfra.api import deploy

# the argument you want
build_version: str = ""

# more arguments with defaults
DEFAULTS = {"foo": None, "bar": ""}

@deploy("Foobar", data_defaults=DEFAULTS)
def samplex():
    print(
      f"\r\n\t{'='*9} RECEIVED ARGUMENTS FROM TERMINAL -> {host.data.build_version} {'='*9}\n")

    print(
    f"\r\n\t{'='*9} RECEIVED EXTRAS FROM TERMINAL ->{host.data.foo} :: {host.data.bar} {'='*9}\n")

From CLI you can call the function as:

pyinfra inventory.py testbed.samplex --data build_version=v1.0 --data foo=baz --data bar=barfoo -y

Upvotes: 1

Related Questions