如何确保在创建时发布带参数的新 Azure runbook?

huangapple go评论52阅读模式
英文:

How do I ensure a new Azure runbook with parameters is published on creation?

问题

我有一个 bicep 模板,用于创建一个自动化帐户,其中包含一个运行簿和计划任务。新的运行簿具有参数,用于引用它所影响的存储帐户并将这些参数传递给 PowerShell 脚本。目前,这个运行簿没有被创建为 "已发布",这意味着它无法使用,也无法与计划任务关联。

我如何确保运行簿已发布(并且已设置好 PowerShell 输入参数),以便运行簿和计划任务之间的关联工作,以便不需要在 Azure 门户中手动发布和关联它们?

以下是脚本的 bicep 模板:

param automationAccountName string
param location string

param runbookName string
param runbookContentUrl string
param scheduleName string
param storageAccountName string
param resourceGroupName string
param jobScheduleLinkName string

resource automationAccount 'Microsoft.Automation/automationAccounts@2022-08-08' = {
  name: automationAccountName
  location: location
  identity: {
    type: 'SystemAssigned'
  }
  properties: {
    publicNetworkAccess: true
    disableLocalAuth: false
    sku: {
      name: 'Basic'
    }
    encryption: {
      keySource: 'Microsoft.Automation'
      identity: {}
    }
  }
}

resource runbook 'Microsoft.Automation/automationAccounts/runbooks@2022-08-08' = {
  parent: automationAccount
  name: runbookName
  location: location
  properties: {
    runbookType: 'PowerShell'
    logVerbose: true
    logProgress: true
    logActivityTrace: 1
    draft: {
      inEdit: false
      parameters: {
        storageAccountName: {
          type: 'string'
          defaultValue: storageAccountName
        }
        resourceGroupName: {
          type: 'string'
          defaultValue: resourceGroupName
        }
      }
    }
    publishContentLink: {
      uri: runbookContentUrl
      version: '1.0.0.0'
    }
  }
}

resource runbookSchedule 'Microsoft.Automation/automationAccounts/schedules@2022-08-08' = {
  parent: automationAccount
  name: scheduleName
  properties: {
    frequency: 'Day'
    interval: 1
    startTime: '2023-06-08T00:00:00Z'
    expiryTime: '9999-12-31T23:59:59Z'
    timeZone: 'Europe/London'
  }
}

resource jobScheduleLink 'Microsoft.Automation/automationAccounts/jobSchedules@2022-08-08' = {
  parent: automationAccount
  name: jobScheduleLinkName
  properties: {
    schedule: {
      name: scheduleName
    }
    runbook: {
      name: runbookName
    }
  }
  dependsOn: [
    runbook
    runbookSchedule
  ]
}

output automationAccountId string = automationAccount.identity.principalId

我尝试单独添加 publishContentLink,但它不允许 parameters。我还尝试创建一个新的部署资源以部署/发布运行簿,但也无法使其工作。

英文:

I have a bicep template being used to create an automation account with a runbook and schedule inside it. The new runbook has parameters to reference the storage account it affects and pass these to the PowerShell script. Currently this runbook is not created as 'published' which means it can't be used nor can it be linked to the schedule.

How do I ensure the runbook is published (with the PowerShell input parameters in place) so that the linking between runbook and schedule works so that it doesn't need manually publishing and linking afterwards in Azure Portal?

Here is the bicep template for the script:

param automationAccountName string
param location string

param runbookName string
param runbookContentUrl string
param scheduleName string
param storageAccountName string
param resourceGroupName string
param jobScheduleLinkName string

resource automationAccount 'Microsoft.Automation/automationAccounts@2022-08-08' = {
  name: automationAccountName
  location: location
  identity: {
    type: 'SystemAssigned'
  }
  properties: {
    publicNetworkAccess: true
    disableLocalAuth: false
    sku: {
      name: 'Basic'
    }
    encryption: {
      keySource: 'Microsoft.Automation'
      identity: {}
    }
  }
}

resource runbook 'Microsoft.Automation/automationAccounts/runbooks@2022-08-08' = {
  parent: automationAccount
  name: runbookName
  location: location
  properties: {
    runbookType: 'PowerShell'
    logVerbose: true
    logProgress: true
    logActivityTrace: 1
    draft: {
      inEdit: false
      parameters: {
        storageAccountName: {
          type: 'string'
          defaultValue: storageAccountName
        }
        resourceGroupName: {
          type: 'string'
          defaultValue: resourceGroupName
        }
      }
    }
    publishContentLink: {
      uri: runbookContentUrl
      version: '1.0.0.0'
    }
  }
}

resource runbookSchedule 'Microsoft.Automation/automationAccounts/schedules@2022-08-08' = {
  parent: automationAccount
  name: scheduleName
  properties: {
    frequency: 'Day'
    interval: 1
    startTime: '2023-06-08T00:00:00Z'
    expiryTime: '9999-12-31T23:59:59Z'
    timeZone: 'Europe/London'
  }
}

resource jobScheduleLink 'Microsoft.Automation/automationAccounts/jobSchedules@2022-08-08' = {
  parent: automationAccount
  name: jobScheduleLinkName
  properties: {
    schedule: {
      name: scheduleName
    }
    runbook: {
      name: runbookName
    }
  }
  dependsOn: [
    runbook
    runbookSchedule
  ]
}

output automationAccountId string = automationAccount.identity.principalId

I tried adding publishContentLink separately but it doesn't allow for parameters.
I tried to create a new deployment resource to deploy/publish the runbook but couldn't get that to work either.

答案1

得分: 0

你需要传递一个带有键/值的 params 对象。

resource automationJobs 'Microsoft.Automation/automationAccounts/jobSchedules@2022-08-08' = {
  parent: automationAccount
  name: guid(automationAccount.id, runbook.name, job.schedule)
  properties: {
    schedule: {
      name: runbookSchedule.name
    }
    runbook: {
      name: runbook.name
    }
    parameters: {
          ResourceGroupName : 'myRG'
          AksClusterName : 'myVM'
          Operation: 'start'
    }
  }
  dependsOn: [runbookSchedule]
}

要获取更详细的示例,我正在为 Microsoft Bicep Registry 贡献一个自动化帐户模块。 PR

英文:

You'll need to pass a params object with the key/values.

resource automationJobs 'Microsoft.Automation/automationAccounts/jobSchedules@2022-08-08' = {
  parent: automationAccount
  name: guid(automationAccount.id, runbook.name, job.schedule)
  properties: {
    schedule: {
      name: runbookSchedule.name
    }
    runbook: {
      name: runbook.name
    }
    parameters: {
          ResourceGroupName : 'myRG'
          AksClusterName : 'myVM'
          Operation: 'start'
    }
  }
  dependsOn: [runbookSchedule]
}

For a fuller sample, I'm in the middle of contributing a Automation Account module to the Microsoft Bicep Registry. PR

huangapple
  • 本文由 发表于 2023年6月5日 19:15:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76405882.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定