0

I want to deploy a VM in azure using Azure Resource Manager (ARM), and then run a PowerShell script inside the VM post deployment to configure it.

I can do this fine with something like this: https://github.com/Azure/azure-quickstart-templates/tree/master/201-vm-vsts-agent

However that template grabs the PowerShell script from GitHub. As part of my deployment I want to upload the script to Azure Storage, and then have the VM get the script from Azure storage and run it. How can I do that part with regards to dependencies on the PowerShell script, because it has to exist in Azure Storage somewhere before being executed.

I currently have this to install a VSTS Agent as part of a deployment, but the script is downloaded from GitHub, I don't want to do that, I want the installation script of the VSTS Agent to be part of my ARM Project.

{
          "name": "vsts-build-agents",
          "type": "extensions",
          "location": "[parameters('location')]",
          "apiVersion": "2017-12-01",
          "dependsOn": [
            "vsts-build-vm"
          ],
          "tags": {
            "displayName": "VstsInstallScript"
          },
          "properties": {
            "publisher": "Microsoft.Compute",
            "type": "CustomScriptExtension",
            "typeHandlerVersion": "1.9",
            "settings": {
              "fileUris": [
                "[concat(parameters('_artifactsLocation'), '/', variables('powerShell').folder, '/', variables('powerShell').script, parameters('_artifactsLocationSasToken'))]"
              ]
            },
            "protectedSettings": {
              "commandToExecute": "[concat('powershell.exe -ExecutionPolicy Unrestricted -Command \"& {', './', variables('powerShell').script, ' ', variables('powerShell').buildParameters, '}\"')]"
            }
          }
        }

I guess my question is really about how to set _azurestoragelocation to an azure storage location where the script has just been uploaded as part of the deployment.

2 Answers 2

1

chicken\egg problem. you cannot upload to azure storage with arm template, you need to use script to upload to azure storage, but if you have that script on vm to upload it you dont really need to upload it.

that being said, why dont you use VSTS agent extension?

{
    "name": "xxx",
    "apiVersion": "2015-01-01",
    "type": "Microsoft.Resources/deployments",
    "properties": {
        "mode": "Incremental",
        "templateLink": {
            "uri": "https://gallery.azure.com/artifact/20161101/microsoft.vsts-agent-windows-arm.1.0.0/Artifacts/MainTemplate.json"
        },
        "parameters": {
            "vmName": {
                "value": "xxx"
            },
            "location": {
                "value": "xxx"
            },
            "VSTSAccountName": {
                "value": "xxx"
            },
            "TeamProject": {
                "value": "xxx"
            },
            "DeploymentGroup": {
                "value": "Default"
            },
            "AgentName": {
                "value": "xxx"
            },
            "PATToken": {
                "value": "xxx"
            }
        }
    }
},
Sign up to request clarification or add additional context in comments.

5 Comments

Interesting, I thought I was going mad. :-) So let's say I want 4 VSTS agents in a VM, can I run that extension four times with the copy() function and get 4 agents?
Also using the quickstart template, the files do get uploaded to ARM_Deploy_Staging resource group, but the problem with that is, I don't know what the storage account is prior to it being created so can't reference a script in there from my template. The deployment PowerShell has an -UploadArtifacts switch, could I use that?
first question - yes, no. i think it will overwrite the agent :) second - arm templates cannot do that. I have no idea what -UploadArtifacts is, certainly doesnt exist for new-azurermresourcegroupdeployment
-UploadArtifacts is part of Deploy-AzureResourceGroup.ps1. I'll have a play around with that, if I can work out how to generate the deterministic hash based on the resource group with uniquestring I might be onto something... :)
well, thats a custom solution, which means you can write yours, its like a 3 liner to upload stuff.
0

Do you mean how to set _artifactsLocation as in the quickstart sample? If so you have 2 options (or 3 depending)

1) use the script in the QS repo, the defaultValue for the _artifactsLocation param will set that for you...

2) if you want to customize, from your local copy of the sample, just use the Deploy-AzureResourceGroup.ps1 in the repo and it will stage and set the value for you accordingly (when you use the -UploadArtifacts switch)

3) stage the PS1 somewhere yourself and manually set the values of _artifactsLocation and _artifactsLocationSasToken

You can also deploy from gallery.azure.com, but that will force you to use the script that is stored in the galley (same as using the defaults in GitHub)

That help?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.