iagctl run service python-script

Run a Python script service.

The iagctl run service python-script command executes a Python script service and displays the resulting stdout, stderr, return code, and execution time information.

Syntax

$iagctl run service python-script <service-name> [flags]

Usage notes

Runtime arguments

Use the --set flag to pass CLI arguments to the Python script using key=value syntax. If you defined a decorator during service creation, IAG validates all --set values against it before passing them to the script.

View accepted inputs

Use the --use flag with the service name to display information about the inputs a service accepts:

$iagctl run service python-script <service-name> --use

How IAG passes arguments to scripts

When you use the --set flag, IAG passes each value to the script in --key=value format. For example, this command:

$iagctl run service python-script my-script \
>--set device=10.0.0.1 \
>--set commands='["show ver"]'

Causes IAG to run the following command in the virtual environment:

$python main.py --device=10.0.0.1 --commands='["show ver"]'

Your script needs to parse these arguments using Python’s argparse.ArgumentParser module. The following example shows how to handle both simple string values and JSON inputs:

1import argparse
2import json
3
4def main():
5 parser = argparse.ArgumentParser(description="Run commands on a network device.")
6 parser.add_argument('--device', required=True, help="Device IP address or hostname")
7 parser.add_argument('--commands', required=True, help="Commands to run.")
8 args = parser.parse_args()
9
10 device = args.device
11 commands_input = args.commands
12
13 try:
14 # Try to parse commands as JSON
15 commands = json.loads(commands_input)
16 print("Debug: Commands parsed as JSON")
17 except json.JSONDecodeError:
18 # If it's not valid JSON, treat it as a single command string
19 commands = [commands_input]
20 print("Debug: Commands parsed as single string command")
21
22 print(device)
23 print(commands)
24
25 # YOUR EXECUTION CODE GOES BELOW THIS LINE
26
27if __name__ == "__main__":
28 main()

Examples

Run a Python script service

$iagctl run service python-script my-python-service

Run a Python script service with arguments

The following example passes device and commands arguments to the script.

$iagctl run service python-script my-python-service \
>--set device=10.0.0.1 \
>--set commands='["show ver"]'

Options

$ -h, --help Help for python-script
$ --set stringArray Runtime input arguments as key=value pairs. Values are validated against the decorator, if one is defined, then passed to the script.
$ --use Display usage of the service.

Options inherited from parent commands

$ --config string Path to the configuration file
$ --raw Display the result of the command in raw format
$ --verbose Enable verbose output