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

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

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

问题

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

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

以下是脚本的 bicep 模板:

  1. param automationAccountName string
  2. param location string
  3. param runbookName string
  4. param runbookContentUrl string
  5. param scheduleName string
  6. param storageAccountName string
  7. param resourceGroupName string
  8. param jobScheduleLinkName string
  9. resource automationAccount 'Microsoft.Automation/automationAccounts@2022-08-08' = {
  10. name: automationAccountName
  11. location: location
  12. identity: {
  13. type: 'SystemAssigned'
  14. }
  15. properties: {
  16. publicNetworkAccess: true
  17. disableLocalAuth: false
  18. sku: {
  19. name: 'Basic'
  20. }
  21. encryption: {
  22. keySource: 'Microsoft.Automation'
  23. identity: {}
  24. }
  25. }
  26. }
  27. resource runbook 'Microsoft.Automation/automationAccounts/runbooks@2022-08-08' = {
  28. parent: automationAccount
  29. name: runbookName
  30. location: location
  31. properties: {
  32. runbookType: 'PowerShell'
  33. logVerbose: true
  34. logProgress: true
  35. logActivityTrace: 1
  36. draft: {
  37. inEdit: false
  38. parameters: {
  39. storageAccountName: {
  40. type: 'string'
  41. defaultValue: storageAccountName
  42. }
  43. resourceGroupName: {
  44. type: 'string'
  45. defaultValue: resourceGroupName
  46. }
  47. }
  48. }
  49. publishContentLink: {
  50. uri: runbookContentUrl
  51. version: '1.0.0.0'
  52. }
  53. }
  54. }
  55. resource runbookSchedule 'Microsoft.Automation/automationAccounts/schedules@2022-08-08' = {
  56. parent: automationAccount
  57. name: scheduleName
  58. properties: {
  59. frequency: 'Day'
  60. interval: 1
  61. startTime: '2023-06-08T00:00:00Z'
  62. expiryTime: '9999-12-31T23:59:59Z'
  63. timeZone: 'Europe/London'
  64. }
  65. }
  66. resource jobScheduleLink 'Microsoft.Automation/automationAccounts/jobSchedules@2022-08-08' = {
  67. parent: automationAccount
  68. name: jobScheduleLinkName
  69. properties: {
  70. schedule: {
  71. name: scheduleName
  72. }
  73. runbook: {
  74. name: runbookName
  75. }
  76. }
  77. dependsOn: [
  78. runbook
  79. runbookSchedule
  80. ]
  81. }
  82. 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:

  1. param automationAccountName string
  2. param location string
  3. param runbookName string
  4. param runbookContentUrl string
  5. param scheduleName string
  6. param storageAccountName string
  7. param resourceGroupName string
  8. param jobScheduleLinkName string
  9. resource automationAccount 'Microsoft.Automation/automationAccounts@2022-08-08' = {
  10. name: automationAccountName
  11. location: location
  12. identity: {
  13. type: 'SystemAssigned'
  14. }
  15. properties: {
  16. publicNetworkAccess: true
  17. disableLocalAuth: false
  18. sku: {
  19. name: 'Basic'
  20. }
  21. encryption: {
  22. keySource: 'Microsoft.Automation'
  23. identity: {}
  24. }
  25. }
  26. }
  27. resource runbook 'Microsoft.Automation/automationAccounts/runbooks@2022-08-08' = {
  28. parent: automationAccount
  29. name: runbookName
  30. location: location
  31. properties: {
  32. runbookType: 'PowerShell'
  33. logVerbose: true
  34. logProgress: true
  35. logActivityTrace: 1
  36. draft: {
  37. inEdit: false
  38. parameters: {
  39. storageAccountName: {
  40. type: 'string'
  41. defaultValue: storageAccountName
  42. }
  43. resourceGroupName: {
  44. type: 'string'
  45. defaultValue: resourceGroupName
  46. }
  47. }
  48. }
  49. publishContentLink: {
  50. uri: runbookContentUrl
  51. version: '1.0.0.0'
  52. }
  53. }
  54. }
  55. resource runbookSchedule 'Microsoft.Automation/automationAccounts/schedules@2022-08-08' = {
  56. parent: automationAccount
  57. name: scheduleName
  58. properties: {
  59. frequency: 'Day'
  60. interval: 1
  61. startTime: '2023-06-08T00:00:00Z'
  62. expiryTime: '9999-12-31T23:59:59Z'
  63. timeZone: 'Europe/London'
  64. }
  65. }
  66. resource jobScheduleLink 'Microsoft.Automation/automationAccounts/jobSchedules@2022-08-08' = {
  67. parent: automationAccount
  68. name: jobScheduleLinkName
  69. properties: {
  70. schedule: {
  71. name: scheduleName
  72. }
  73. runbook: {
  74. name: runbookName
  75. }
  76. }
  77. dependsOn: [
  78. runbook
  79. runbookSchedule
  80. ]
  81. }
  82. 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 对象。

  1. resource automationJobs 'Microsoft.Automation/automationAccounts/jobSchedules@2022-08-08' = {
  2. parent: automationAccount
  3. name: guid(automationAccount.id, runbook.name, job.schedule)
  4. properties: {
  5. schedule: {
  6. name: runbookSchedule.name
  7. }
  8. runbook: {
  9. name: runbook.name
  10. }
  11. parameters: {
  12. ResourceGroupName : 'myRG'
  13. AksClusterName : 'myVM'
  14. Operation: 'start'
  15. }
  16. }
  17. dependsOn: [runbookSchedule]
  18. }

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

英文:

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

  1. resource automationJobs 'Microsoft.Automation/automationAccounts/jobSchedules@2022-08-08' = {
  2. parent: automationAccount
  3. name: guid(automationAccount.id, runbook.name, job.schedule)
  4. properties: {
  5. schedule: {
  6. name: runbookSchedule.name
  7. }
  8. runbook: {
  9. name: runbook.name
  10. }
  11. parameters: {
  12. ResourceGroupName : 'myRG'
  13. AksClusterName : 'myVM'
  14. Operation: 'start'
  15. }
  16. }
  17. dependsOn: [runbookSchedule]
  18. }

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:

确定