Azure Automation runbook with custom Python classes in multiple files

Antti-Jussi Korjonen 25 Reputation points
2024-09-03T07:24:18.7166667+00:00

I am developing multiple Python classes (in separate files) to be used in a runbook.
Azure DevOps (git) is used as version control with runbooks automatically syncing to Azure Automation.

I know there is a way to include the classes as modules in the environment but I don't want to do that.
Not yet, because I'm still developing those classes.

I could also pull the class files from DevOps separately but I do not want to that either.

Is there any way to include all the files the runbook needs in the runbook run?
My workaround currently is to concatenate all the files in one file, which works but is a bit nasty way of doing this.

Azure Automation
Azure Automation
An Azure service that is used to automate, configure, and install updates across hybrid environments.
1,366 questions
0 comments No comments
{count} vote

1 answer

Sort by: Most helpful
  1. Amira Bedhiafi 33,071 Reputation points Volunteer Moderator
    2025-06-09T19:54:24.0366667+00:00

    Hello !

    Thank you for posting on Microsoft Learn.

    You're are right about the limitations compared to a typical python dev environment when dealing with multiple files or modules.

    But you can zip them up as a module and upload them as a shared asset, or concatenate them into a single script, as you've done.

    I am thinking about using Python import semantics within a single script where you wrap your modules as classes/functions within the same file, like this:

    Structure in a single runbook:

    # --- module_a.py ---
    class HelperA:
        def do_something(self):
            print("Doing something from HelperA")
    # --- module_b.py ---
    class HelperB:
        def do_another_thing(self):
            print("Doing something from HelperB")
    # --- main.py ---
    def main():
        a = HelperA()
        a.do_something()
        b = HelperB()
        b.do_another_thing()
    if __name__ == "__main__":
        main()
    

    So, you write each module as a standalone .py file in your dev environment and u se a CI step or local script to auto-concatenate them in the right order into one file (runbook.py), before pushing to Azure Automation.

    You can use a simple Python or shell script like:

    cat module_a.py module_b.py main.py > runbook.py
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.