I want to create a runbook on an automation account with a shedule already connected to it through arm

Viewed 1917

With my ARM template I want to create an automation account with a runbook and a shedule , so far so good. But if i want to connect my shedule to my runbook through the template I can't seem to find the working way to do this.

First try (working) : create automation account with a runbook and a 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"
                        ]
                    }
                }
            }
        ]
    }
]

Second try here i don't get errors but the shedule is not connected to the runbook - I added "runbook": "variables('name')", to the shedule properties

third try (here i get errors that my dependes on is not right configured

i tried to add the shedule block inside a resource value of the runbook like this

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

The error i got is as followed:

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

I have no clue which option is the right one, i think my third try is the right way to add a shedule to a runbook but i can't seem to find the right way to use the right depends on

[Edit]

Like the answers mentioned my depends on structure was not good , after I changed this I keep getting following error. And I am looking some time now for a solution but can't seem to find which resource they are mentioning that is missing I used following depends on :

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

And got this error.

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 Answers

I know this question was asked a while ago, but I just worked out how to do this for myself and thought I'd post in case it can help anyone else:

Adding a schedule block inside the template will create the schedule but not connect it to the runbook. To connect the two together, you have to create a job schedule as well.

The steps I took to fix this are as follows:

1. Add the runbook block as a child resource of the Automation Account

2. Add the schedule block as a child resource of the Automation Account (not as a child of the runbook - this is what threw the last error)

3. Add a job schedule block as a child resource of the Automation Account, and pass in the name of the runbook and the name of the schedule:

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

Obviously you might need to mess around a bit more to get yours working properly but these are the general steps I took :)

-NOTE- don't forget to add dependencies where necessary (e.g. job schedule will depend on the runbook and the schedule already existing)

References:

Your depends on should be:

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

                        ],

Alternatively, you can use resourceId() function, which gives a more readable result:

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

with resourceId you can, also, construct resourceId for resources in other subscriptions \ resourcegroups easily.

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

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

Using Bicep this is a lot simpler.

Create a schedules.bicep file with the following content:

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

Then using Azure CLI just run az deployment group create -f schedules.bicep -g your-resourcegroup-name.

Note: I was referencing an existing Automation Account and Runbook but you could add those resources to the template as well. Also to generate the classic JSON ARM Templates you could run az bicep build -f schedules.bicep.

Related