Azure Automation - Runbooks

How to provision runbooks using Cloud Maker

Alex Metaxas avatar
Written by Alex Metaxas
Updated over a week ago

Azure Automation provides powerful automation and configuration services that are complementary to Cloud Maker. In this walkthrough, we'll show you how to use Cloud Maker and Azure DevOps to deploy an Azure Automation account and a PowerShell runbook that will run on a daily schedule.

Storing the runbook script

When deploying a runbook to an Automation Account, the runbook source script must be initially hosted in a location that the Azure resource provider can access on the internet. One secure option for this is using an Azure Storage Account.


Note: In a future version we will support source control integration for runbooks which will be an alternative to hosting scripts on a storage account.

Creating the Storage Account

First, we need to create a blueprint that will deploy an Azure Storage Account. As mentioned above, this will initially host the PowerShell runbook script in a secure location that the Automation Account resource provider can access at deploy time.

  1. Drag an Azure Resource Group droplet onto the drawing surface.

  2. Open the properties panel and configure the Name and Location.

  3. Drag an Azure Storage Account droplet into the Resource Group.

  4. Set the Name of the Storage Account.
    Note: Please observe the naming restrictions in the description.

  5. Go to Blob Services and Add new Blob Service.

  6. Add a new Container named runbooks.

  7. Set Public Access to None.

  8. Save the blueprint with the name Runbook Storage.

  9. Publish the blueprint.

  10. Create a new pipeline called Runbook Storage Deployment.

  11. Add the following Azure DevOps outputs:

    1. Name: storageAccountName
      Output: (your storage account name).Name
      Secret: False

    2. Name: storageAccountKey
      Output: (your storage account name).Keys.key1
      Secret: True

Create the Automation Account and Runbook

Next we need to create the blueprint that will deploy the Azure Automation Account and the PowerShell runbook.

  1. Create a new blueprint.

  2. Drag an Azure Resource Group droplet onto the drawing surface.

  3. Set the Name and Location.

  4. Drag an Azure Automation Account droplet into the resource group.

  5. Set the Name of the automation account.

Create a schedule

  1. Go to Schedules and add a new Schedule and name it Daily.

  2. Set Description to Daily at 0900 UTC.

  3. Set Frequency to Day.

  4. Set Interval to 1.

  5. Set Start Time to an ISO 8061 DateTime. For example, if the date is the 31st December 2021 and you want to run the runbook at 09:000 UTC use 2021-12-31T09:00:00Z.
    Note: On first deployment this must be at least 5 mins ahead of the time you will deploy.

Create a runbook

  1. Go to Runbooks and add a new Runbook named HelloWorld.

  2. Set Description to A runbook that prints Hello, World.

  3. Set Log Progress to True.

  4. Set Runbook Type to PowerShell.

  5. Go to Content Link and convert URL to a parameter named helloWorldURL.

  6. Convert Version to a parameter named helloWorldVersion.

Create a Job Schedule

The Job Schedule applies a schedule to a runbook.

  1. Go to Job Schedules and add a new Job Schedule named HelloWorldJobSchedule.

  2. Go to Job Schedule Properties

  3. Set Run Book Name to HelloWorld.

  4. Set Schedule Name to Daily.

  5. Save the blueprint with the name Automation Account.

  6. Publish the blueprint.

  7. Create a new pipeline called Automation Account Deployment.

  8. Set the helloWorldURL parameter to use an Azure DevOps variable called helloWorldURL. This variable will be set in the Azure CLI task below.

  9. Set helloWorldVersion to 1.

Putting it all together in Azure DevOps

  1. In your Azure DevOps organisation, create a new repo called runbooks.

  2. Create a PowerShell file called helloWorld.ps1 in the root of the runbooks repository with the following content:

    Write-Output "Hello, World"

  3. In your Azure DevOps organisation, go to Pipelines, select Releases, then New, New release pipeline:


  4. Set the name to Automation Account - Runbooks:

  5. Add an Azure Repos artifact to the pipeline and select the runbooks repository you created above:

  6. Add a stage called Dev:

  7. Click 1 job, 0 task to view the stage tasks.

  8. Add a Cloud Maker Azure Deployment task to deploy the Runbook Storage blueprint with the following settings:

    Display Name: Deploy Azure Storage Account
    Organisation: (Select your Cloud Maker organisation)
    Pipeline: Runbook Storage Deployment
    Stage: Dev
    Azure subscription: (Select your Azure subscription, e.g. Dev)
    Metadata location: (Select an Azure location)

  9. Add an Azure CLI task to upload the runbook to the storage account with the following inline script:

    Display Name: Upload Runbook to Storage Account
    Azure Resource Manager connection: (Select your Azure subscription, e.g. Dev)
    Script Type: Shell
    Script Location: Inline script
    Inline script:

    blobName=helloWorld.ps1
    az storage blob upload --account-key $(storageAccountKey) --account-name $(storageAccountName) --container-name runbooks --file $(System.DefaultWorkingDirectory)/_runbooks/helloWorld.ps1 --name $blobName;
    end=`date -u -d "30 minutes" '+%Y-%m-%dT%H:%MZ'`
    blobURI=`az storage blob generate-sas --account-key $(storageAccountKey) --account-name $(storageAccountName) --container-name runbooks --expiry $end --name $blobName --permissions r --full-uri`
    #strip double quotes
    blobURI="${blobURI%\"}"
    blobURI="${blobURI#\"}"
    echo "##vso[task.setvariable variable=helloWorldURL;]${blobURI}"

  10. Add a Cloud Maker Azure Deployment task to deploy the Automation Account blueprint with the following settings:

    Display Name: Deploy Automation Account and Runbook
    Organisation: (Select your Cloud Maker organisation)
    Pipeline: Automation Account Deployment
    Stage: Dev
    Azure subscription: (Select your Azure subscription, e.g. Dev)
    Metadata location: (Select an Azure location)

  11. Press Save and then Create release to run the pipeline and deploy everything:

Verifying the runbook

Now let's verify that everything was deployed and that the runbook is functioning correctly:

  1. Go to the Azure Portal.

  2. In the search bar, type the name of the runbook. For example, HelloWorld.

  3. Select the runbook from the search results.

  4. If the runbook has been run already, you will see a list of jobs under Recent Jobs on the Overview page. Otherwise press Start to manually trigger the job.

  5. Click on a job in the list.

  6. Go to Output. You should see Hello, world has been printed:

Updating the runbook

  1. Update the PowerShell script in the runbook repo to:

    Write-Output "Hello Cloud Maker!"

  2. Go to the Automation Account Deployment pipeline in Cloud Maker.

  3. Set the helloWorldVersion parameter to 2.
    Note: This indicates there is a new version and ensures that the script you've just updated will be used as the source for the runbook.

  4. Deploy the pipeline.

  5. Repeat the steps above in the Verifying the runbook section to check that you now see Hello Cloud Maker! as the job output.

Did this answer your question?