abinitio
abinitio

Reputation: 837

How do you import runbook to azure resource group from the command line

I have a DevOps pipeline for setting up various resources within a resource group in Azure.

As part of the pipeline I would like to import a runbook to the resource group, and set up a schedule for it to run within the portal (not set up a DevOps schedule).

The docs explain how to create a runbook from the azure cli:

az automation runbook create --automation-account-name "myAutomationAccount" --resource-group "rg" --name "myRunbook" --type "PowerShell" --location "East US 2"

but do not explain how import the runbook from a DevOps repo.

How do I import the runbook from a DevOps pipeline using the cli.

Also how can I create a schedule in the portal (using the cli within the DevOps pipeline) on which to run the runbook?

Upvotes: 0

Views: 928

Answers (3)

hvk
hvk

Reputation: 437

For creating the runbook, inserting the content, publishing the runbook, creating a scedule and connecting the runbook to a schedule, we have this Azure CLI code for powershell:

az automation runbook create

az automation runbook replace-content --content @./folder_name/runbook_name.ps1

az automation runbook publish

az automation schedule create 

Register-AzAutomationScheduledRunbook   

But as some of us experienced, the content of runbook_name.ps1 is cut off, after the first CRLF. Why do some of us have this issue and others not?

Apparently, when running in an Azure Devops Pipeline the difference is the agent. Some parts of my pipeline needs a windows-latest agent. But after switching this job (pool/vmImage) to a ubuntu-latest agent, the whole file was imported and not only the first line of code.

Furthermore, I prefer creating the runbook, schedule an job creation via Bicep and only uses the this code for the insertion of je content:

az automation runbook replace-content --content @./folder_name/runbook_name.ps1

This is the only part of the pipeline, which has to run in a linux agent.

The bicep code I use:

resource parent_resource 'Microsoft.Automation/automationAccounts@2022-08-08' existing = {
  name: parentName
}

resource deployRunbook 'Microsoft.Automation/automationAccounts/runbooks@2022-08-08' = {
  name: runbookName
  parent: parent_resource
  location: location
  properties: {
    description: description
    runbookType: 'PowerShell7'
    publishContentLink: {
      //dummy url as I want to use a file from an artifact in the Azure Devops pipeline
      uri: 'https://google.com'
    }
  }
}

resource schedule 'Microsoft.Automation/automationAccounts/schedules@2022-08-08' = {
  name: scheduleName
  parent: parent_resource
  properties: {
    advancedSchedule: {
      weekDays: weekDays
    }
    description: description
    frequency: frequency
    interval: any(1)
    startTime: dateTimeAdd('${todayDate}T${startTime}', dateTimeToEpoch('${todayDate}T${startTime}') > nowTicks + offsetSeconds ? 'P0D' : 'P1D')
  }
}

resource jobschedule 'Microsoft.Automation/automationAccounts/jobSchedules@2022-08-08' = {
  //the name of a jobSchedule must be a guid!
  name: jobscheduleId
  parent: parent_resource
  properties: {
    runbook: {
      name: runbookName
    }
    schedule: {
      name: scheduleName
    }
  }
}

Upvotes: 1

abinitio
abinitio

Reputation: 837

Figured out how to do it using the following commands with the appropriate arguments passed through --automation-account-name --name --resource-group --type Powershell --location for the first call etc:

az automation runbook create

az automation runbook replace-content --content @./Repo_name/runbook_name.ps1

az automation runbook publish

Upvotes: 1

Stefan Beckmann
Stefan Beckmann

Reputation: 1

I import runbooks regularly. But I go a little different way. In Azure DevOps, I call a webhook with parameters and then start a runbook that imports the runbooks from Azure DevOps. In addition, I no longer use the PowerShell modules for the import but do everything via the API, which is much faster.

https://learn.microsoft.com/en-us/rest/api/automation/runbook/create-or-update

First, I create the runbook as a draft, adding the content and publishing the runbook.

It can also be done the other way, directly from Azure DevOps, but Azure Automation is my main tool, so I did it there. Of course, there is still source control in Azure Automation, where you can also have the code imported automatically. But I can do everything the same way with the runbook, and I'm very flexible with the webhook!

Stefan

Upvotes: 0

Related Questions