run python-script
  • 29 Oct 2024
  • Dark
    Light
  • PDF

run python-script

  • Dark
    Light
  • PDF

Article summary

iagctl run python-script

Execute a Python script service

Synopsis

This command will execute a Python script service and display the resulting stdout, stderr, return code, as well as some additional execution time information.

When running a Python script service, one can specify CLI arguments the Python script accepts by using the Python argparse.ArgumentParser module using the --set flag.
The --set flag takes in a key=value syntax. Any variables passed in using --set will be validated against the decorator if one was defined during service creation, and then passed to the python script that was created.

To view helpful information about what inputs are accepted by a particular service, run this command with the service name and the --use flag.

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

Stub Code to Include Arguments

When running a Python script, it is important to understand how gateway will send values defined by the --set flag to the script. On execution, gateway will send arguments in the format of --key=value. For example, when running:

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

The command will essentially cause gateway to run the following command within your virtual environment:

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

The example stub code to parse these inputs is shown below.

main.py

import argparse
import json

def main():
    parser = argparse.ArgumentParser(description="Run commands on a network device.")
    parser.add_argument('--device', required=True, help="Device IP address or hostname")
    parser.add_argument('--commands', required=True, help="Commands to run.")
    args = parser.parse_args()

    device = args.device
    commands_input = args.commands

    try:
         Try to parse commands as JSON. Used when the value would be JSON.
        commands = json.loads(commands_input)
        print("Debug: Commands parsed as JSON")
    except json.JSONDecodeError:
         If it's not valid JSON, treat it as a single command string
        commands = [commands_input]
        print("Debug: Commands parsed as single string command")

    print(device)
    print(commands)

     YOUR EXECUTION CODE GOES BELOW THIS LINE    

if __name__ == "__main__":
    main()

Examples

Python Script Service

Run a simple Python script service called my-python-service.

iagctl run python-script my-python-service

Setting Arguments

Run a Python script service called my-python-service that takes in arguments of device and commands. To understand how arguments will be fed into the script, see the Stub Code to take in Arguments above.

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

Options

-h, --help                 Help for python-script
--set stringArray          Sets an input argument to be passed into the script via CLI when executed. Arguments are sent in the order in which they are defined and are appended to the arguments already defined on the python script service. They must follow the key=value syntax and the service must support the inputs.
--state string             The state file to utilize while running the plan. If a file path is specified with '@' the resulting state file will be saved there as well (unless state-out is specified).
--state-out string         Path to write the resulting state file that is different than --state. This can be used to preserve the old state file.
--use                      Display usage of the plan.

Options Inherited from Parent Commands

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

CLI References

See related run commands:

For all CLI commands see → Command References Index


Was this article helpful?

What's Next
Changing your password will log you out immediately. Use the new password to log back in.
First name must have atleast 2 characters. Numbers and special characters are not allowed.
Last name must have atleast 1 characters. Numbers and special characters are not allowed.
Enter a valid email
Enter a valid password
Your profile has been successfully updated.