4

使用我的 ARM 模板,我想创建一个带有 runbook 和 shedule 的自动化帐户,到目前为止一切顺利。但是,如果我想通过模板将我的 shedule 连接到我的运行手册,我似乎无法找到执行此操作的工作方式。

第一次尝试(工作):使用运行手册和时间表创建自动化帐户

"variables": {
    "name": "StartAllVM",
    "url": "https://gallery.technet.microsoft.com/scriptcenter/Start-Azure-V2-VMs-6352312e/file/147007/1/Start-AzureV2VMs.ps1",
    "version": "1.0.0.0",
    "type": "PowerShell",
    "description": "This PowerShell script runbook connects to Azure and starts all VMs in an Azure subscription or cloud service"
},
"resources": [
    {
        "name": "AutomationDev",
        "type": "Microsoft.Automation/automationAccounts",
        "apiVersion": "2015-10-31",
        "properties": {
            "sku": {
                "name": "Free"
            }
        },
        "location": "[parameters('location')]",
        "tags": {},
        "resources": [
            {
                "name": "[variables('name')]",
                "type": "runbooks",
                "apiVersion": "2015-01-01-preview",
                "location": "[resourceGroup().location]",
                "dependsOn": [
                    "[concat('Microsoft.Automation/automationAccounts/', 'AutomationDev')]"
                ],
                "properties": {
                    "runbookType": "PowerShell",
                    "logProgress": false,
                    "logVerbose": true,
                    "publishContentLink": {
                        "uri": "[variables('url')]",
                        "version": "[variables('version')]"
                    }
                }
            },
            {
                "comments": "",
                "type": "schedules",
                "name": "shedule1",
                "apiVersion": "2015-10-31",
                "location": "[resourceGroup().location]",
                "dependsOn": [
                    "[concat('Microsoft.Automation/automationAccounts/', 'AutomationDev')]",
                    "[variables('name')]"
                ],
                "properties": {
                    "description": "VM Patch Automation Schedule",
                    "startTime": "06:00PM",
                    "expiryTime": "",
                    "isEnabled": true,
                    "interval": 1,
                    "frequency": "Week",
                    "timeZone": "UTC",
                    "advancedSchedule": {
                        "weekDays": [
                            "Monday"
                        ]
                    }
                }
            }
        ]
    }
]

第二次尝试这里我没有收到错误,但 shedule 未连接到运行手册 - 我添加"runbook": "variables('name')",到了 shedule 属性

第三次尝试(在这里我得到我所依赖的错误配置不正确

我试图像这样在 Runbook 的资源值中添加 shedule 块

 {
                "name": "[variables('name')]",
                "type": "runbooks",
                "apiVersion": "2015-01-01-preview",
                "location": "[resourceGroup().location]",
                "dependsOn": [
                    "[concat('Microsoft.Automation/automationAccounts/', 'AutomationDev')]"
                ],
                "properties": {
                    "runbookType": "PowerShell",
                    "logProgress": false,
                    "logVerbose": true,
                    "publishContentLink": {
                        "uri": "[variables('url')]",
                        "version": "[variables('version')]"
                    }
                },
                "resources": [
                    {
                        "comments": "",
                        "type": "schedules",
                        "name": "shedule1",
                        "apiVersion": "2015-10-31",
                        "location": "[resourceGroup().location]",
                        "dependsOn": [
                            "[concat('Microsoft.Automation/automationAccounts/', 'AutomationDev' , variables('name'))]",

                        ],
                        "properties": {
                            "description": "VM Patch Automation Schedule",
                            "runbook": "variables('name')",
                            "startTime": "06:00PM",
                            "expiryTime": "",
                            "isEnabled": true,
                            "interval": 1,
                            "frequency": "Week",
                            "timeZone": "UTC",
                            "advancedSchedule": {
                                "weekDays": [
                                    "Monday"
                                ]
                            }
                        }
                    }
                ]
            }

我得到的错误如下:

New-AzureRmResourceGroupDeployment : 16:43:44 - Error: Code=InvalidTemplate; Message=Deployment template validation fai
led: 'The resource '/subscriptions/xxxxxxxx/resourceGroups/xxxx/providers/Microsoft.Automa
tion/automationAccounts/AutomationDev/runbooks/StartAllVM/schedules/shedule1' at line '54' and column '17' doesn't depe
nd on parent resource '/subscriptions/xxxxxxxx/resourceGroups/xxx/providers/Microsoft.Aut
omation/automationAccounts/AutomationDev/runbooks/StartAllVM'. Please add dependency explicitly using the 'dependsOn' s
yntax. Please see https://aka.ms/arm-template/#resources for usage details.'.

我不知道哪个选项是正确的,我认为我的第三次尝试是在运行手册中添加一个 shedule 的正确方法,但我似乎无法找到使用正确的正确方法取决于

[编辑]

就像提到的答案一样,我的依赖结构不好,在我改变这个之后,我不断收到以下错误。我现在正在寻找一些时间来寻找解决方案,但似乎无法找到他们提到的缺少的资源我使用的以下取决于:

"[resourceId('Microsoft.Automation/automationAccounts/runbooks', 'AutomationDev' , variables('name'))]"

并得到了这个错误。

New-AzureRmResourceGroupDeployment : 9:03:47 - Resource Microsoft.Automation/automationAccounts/runbooks/schedules 'AutomationDev/StartAllVM/shedule1' failed with message '{
  "error": {
    "code": "BadRequest",
    "message": "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\r\n<html xmlns=\"http://www.w3.org/1999/xhtml\">\r\n<head>\r\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\"/>\
r\n<title>404 - File or directory not found.</title>\r\n<style type=\"text/css\">\r\n<!--\r\nbody{margin:0;font-size:.7em;font-family:Verdana, Arial, Helvetica, sans-serif;background:#EEEEEE;}\r\nfieldset{padding:0 15px 10px 15px;} \r\nh1{font-size:2.4em;margin:0;color:
#FFF;}\r\nh2{font-size:1.7em;margin:0;color:#CC0000;} \r\nh3{font-size:1.2em;margin:10px 0 0 0;color:#000000;} \r\n#header{width:96%;margin:0 0 0 0;padding:6px 2% 6px 2%;font-family:\"trebuchet MS\", Verdana, sans-serif;color:#FFF;\r\nbackground-color:#555555;}\r\n#cont
ent{margin:0 0 0 2%;position:relative;}\r\n.content-container{background:#FFF;width:96%;margin-top:8px;padding:10px;position:relative;}\r\n-->\r\n</style>\r\n</head>\r\n<body>\r\n<div id=\"header\"><h1>Server Error</h1></div>\r\n<div id=\"content\">\r\n <div class=\"con
tent-container\"><fieldset>\r\n  <h2>404 - File or directory not found.</h2>\r\n  <h3>The resource you are looking for might have been removed, had its name changed, or is temporarily unavailable.</h3>\r\n </fieldset></div>\r\n</div>\r\n</body>\r\n</html>\r\n"
  }
4

4 回答 4

2

我知道这个问题是不久前被问到的,但我只是想出了如何为自己做这个,并认为我会发布以防它可以帮助其他人:

在模板中添加计划块将创建计划,但不会将其连接到 Runbook。要将两者联系在一起,您还必须创建一个作业计划

我为解决此问题而采取的步骤如下:

1. 将 Runbook 块添加为自动化帐户的子资源

2.将计划块添加为自动化帐户的子资源(而不是作为运行手册的子资源 - 这是引发最后一个错误的原因)

3. 添加作业计划块作为自动化帐户的子资源,并传入运行手册的名称计划的名称:

    {
      "name": "string",
      "type": "Microsoft.Automation/automationAccounts/jobSchedules",
      "apiVersion": "2015-10-31",
      "properties": {
        "schedule": {
          "name": "string"
        },
        "runbook": {
          "name": "string"
        }
      }
    }

显然,您可能需要多花点时间才能让您的正常工作,但这些是我采取的一般步骤:)

-注意-不要忘记在必要时添加依赖项(例如,作业计划将取决于运行手册和已经存在的计划)

参考:

于 2019-04-29T17:57:18.900 回答
1

或者,您可以使用resourceId()函数,它会提供更易读的结果:

"dependsOn": [
    "[resourceId('Microsoft.Automation/automationAccounts/runbooks', 'AutomationDev' , variables('name'))]",
]

使用resourceId,您还可以轻松地为其他订阅\资源组中的资源构造resourceId。

resourceId([subscriptionId], [resourceGroupName], resourceType, resourceName1, [resourceName2]...)

https://docs.microsoft.com/en-us/azure/azure-resource-manager/resource-group-template-functions-resource#resourceid

于 2018-11-17T10:18:15.763 回答
1

你的依赖应该是:

  "dependsOn": [
                            "[concat('Microsoft.Automation/automationAccounts/', 'AutomationDev/runbooks/' , variables('name'))]",

                        ],
于 2018-11-16T16:30:49.333 回答
0

使用二头肌这要简单得多。

创建具有以下内容的 schedules.bicep 文件:

param guidValue string = newGuid()

var aaName = 'your-automation-account-name'
var runbookName = 'your-runbook-name'
var scheduleName = 'the-desired-schedule-name'
var scheduleFullName = '${aaName}/${scheduleName}'
var scheduleAssignment = '${aaName}/${guidValue}'

resource schedule 'Microsoft.Automation/automationAccounts/schedules@2020-01-13-preview' = {
  name: scheduleFullName
  properties: {
    frequency: 'Day'
    interval: any(6)
    startTime: '2021-10-10'
  }
}

resource jobSchedule 'Microsoft.Automation/automationAccounts/jobSchedules@2020-01-13-preview' = {
  name: scheduleAssignment
  properties: {
    runbook: {
      name: runbookName
    }
    schedule: {
      name: scheduleName
    }
  }
}

然后使用 Azure CLI 运行az deployment group create -f schedules.bicep -g your-resourcegroup-name.

注意:我引用了现有的自动化帐户和运行手册,但您也可以将这些资源添加到模板中。还要生成您可以运行的经典 JSON ARM 模板az bicep build -f schedules.bicep

于 2021-08-19T14:02:13.627 回答