代码之家  ›  专栏  ›  技术社区  ›  achahbar

我想在一个自动化帐户上创建一个runbook,它已经通过arm连接了一个计划

  •  1
  • achahbar  · 技术社区  · 6 年前

    使用我的ARM模板,我想创建一个带有runbook和schedule的自动化帐户,目前为止还不错。但是,如果我想通过模板将我的日程安排与我的runbook连接起来,我似乎找不到这样做的工作方式。

    首次尝试(工作):使用runbook和Schedule创建自动化帐户

    "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"
                            ]
                        }
                    }
                }
            ]
        }
    ]
    

    第二次尝试在这里我没有得到错误,但时间表没有连接到runbook -我补充说 "runbook": "variables('name')", 到计划属性

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

    我试图将schedule块添加到runbook的资源值中,如下所示

     {
                    "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.'.
    

    我不知道哪个选项是正确的,我认为我的第三次尝试是在runbook中添加计划表的正确方法,但是我似乎找不到正确的使用方法,这取决于

    [编辑]

    就像上面提到的答案一样,我的依赖结构不好,在我修改了这个之后,我不断地出现以下错误。我正在寻找解决方案,但似乎找不到他们提到的缺少的资源 我用了以下取决于:

    "[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"
      }
    
    2 回复  |  直到 6 年前
        1
  •  1
  •   Sam Cogan    6 年前

    你的依赖应该是:

      "dependsOn": [
                                "[concat('Microsoft.Automation/automationAccounts/', 'AutomationDev/runbooks/' , variables('name'))]",
    
                            ],
    
        2
  •  1
  •   4c74356b41    6 年前

    或者,您可以使用 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

        3
  •  1
  •   BammItsJordan    5 年前

    我知道刚才有人问过这个问题,但我只是自己想了一下如何做到这一点,我想我会发帖,以防对其他人有所帮助:

    在模板中添加计划块将创建计划,但不会将其连接到runbook。要将两者连接起来,必须创建 工作安排 也。

    我采取的步骤如下:

    一。将runbook块添加为自动化帐户的子资源

    2。将计划块添加为自动化帐户的子资源( 作为runbook的孩子-这是引发最后一个错误的原因)

    三。添加作业计划块作为自动化帐户的子资源,并传入runbook的名称 计划的名称:

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

    显然,你可能需要更多的时间来让自己正常工作,但这些是我采取的一般步骤:)

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

    参考文献:

    推荐文章