【问题标题】:How to run same script multiple times in ARM template?如何在 ARM 模板中多次运行相同的脚本?
【发布时间】:2018-07-02 14:50:31
【问题描述】:

我有一个用于在 ARM 模板中安装 VSTS 构建代理的 powershell 脚本。此模板基于 azure 快速入门模板 here

我想使用"copy" 函数多次运行脚本,因为我想在部署 VM 时安装 10 个代理。当我尝试部署我的模板时,我收到了这个错误:

Error: Code=InvalidTemplate; Message=Deployment template validation failed: 
'The template resource 'CustomScript' at line '247' column '13' is not valid. Copying nested resources is not supported.

我的问题是,如何安装 10 个带有 copy 功能的构建代理,以便拥有 vsts-agent-1, vsts-agent-2, etc

这是模板的相关sn-p:

{
      "name": "[parameters('vmName')]",
      "type": "Microsoft.Compute/virtualMachines",
      "location": "[parameters('location')]",
      "apiVersion": "2017-03-30",
      "dependsOn": [
        "[concat('Microsoft.Storage/storageAccounts/', variables('storageAccountName'))]",
        "[concat('Microsoft.Network/networkInterfaces/', variables('vmNicName'))]"
      ],
      "tags": {
        "displayName": "VM01"
      },
      "properties": {
        "hardwareProfile": {
          "vmSize": "[parameters('vmSize')]"
        },
        "osProfile": {
          "computerName": "[parameters('vmName')]",
          "adminUsername": "[parameters('vmAdminUserName')]",
          "adminPassword": "[parameters('vmAdminPassword')]"
        },
        "storageProfile": {
          "imageReference": {
            "publisher": "[variables('vmImagePublisher')]",
            "offer": "[variables('vmImageOffer')]",
            "sku": "[parameters('vmVisualStudioVersion')]",
            "version": "latest"
          },
          "osDisk": {
            "name": "[concat(parameters('vmName'),'_OSDisk')]",
            "caching": "ReadWrite",
            "createOption": "FromImage"
          }
        },
        "networkProfile": {
          "networkInterfaces": [
            {
              "id": "[resourceId('Microsoft.Network/networkInterfaces', variables('vmNicName'))]"
            }
          ]
        }
      },
      "resources": [
        {
          "name": "CustomScript",
          "type": "extensions",
          "location": "[parameters('location')]",
          "apiVersion": "2015-05-01-preview",
          "dependsOn": [
            "[concat('Microsoft.Compute/virtualMachines/', parameters('vmName'))]"
          ],
          "properties": {
            "publisher": "Microsoft.Compute",
            "type": "CustomScriptExtension",
            "typeHandlerVersion": "1.4",
            "settings": {
              "fileUris": [
                "[concat(parameters('_artifactsLocation'),'/InstallVSTSAgent.ps1')]"
              ],
              "commandToExecute": "[concat('powershell.exe -ExecutionPolicy Unrestricted -Command .\\InstallVSTSAgent.ps1 -vstsAccount ', parameters('vstsAccount'), ' -personalAccessToken ', parameters('personalAccessToken'), ' -AgentName ', parameters('vstsAccount'), ' -PoolName ', parameters('poolName'), ' -runAsAutoLogon ', parameters('enableAutologon'), ' -vmAdminUserName ', parameters('vmAdminUserName'), ' -vmAdminPassword ', parameters('vmAdminPassword'))]"
            }
          }
        }
      ]

EDIT1

我已更新模板并将子资源移出,以便子资源与父资源处于同一级别。这部分现在看起来像这样:

{
  "name": "CustomScript",
  "type": "Microsoft.Compute/virtualMachines/extensions",
  "location": "[parameters('location')]",
  "apiVersion": "2015-05-01-preview",
  "dependsOn": [
    "[concat('Microsoft.Compute/virtualMachines/', parameters('vmName'))]"
  ],
  "copy": {
    "name": "customScriptGroup",
    "count": "[parameters('agentCount')]"
  },
  "properties": {
    "publisher": "Microsoft.Compute",
    "type": "CustomScriptExtension",
    "typeHandlerVersion": "1.4",
    "protectedSettings": {
      "fileUris": [
        "[concat(parameters('_artifactsLocation'),'/InstallVSTSAgent.ps1')]"
      ],
      "commandToExecute": "[concat('powershell.exe -ExecutionPolicy Unrestricted -Command .\\InstallVSTSAgent.ps1 -vstsAccount ', parameters('vstsAccount'), ' -personalAccessToken ', parameters('personalAccessToken'), ' -AgentName ', parameters('vstsAccount')[copyIndex(1)], ' -PoolName ', parameters('poolName'), ' -runAsAutoLogon ', parameters('enableAutologon'), ' -vmAdminUserName ', parameters('vmAdminUserName'), ' -vmAdminPassword ', parameters('vmAdminPassword'))]"
    }
  }
}

但是,当我尝试部署时,我收到此错误:

Error: Code=InvalidTemplate; Message=Deployment template validation failed: The template resource 'CustomScript' for type 'Microsoft.Compute/virtualMachines/extensions' at line '247' and column '9' has incorrect segment lengths. A nested resource type must have identical number of segments as its resource name. A root resource type must have segment length one greater than its resource name.

【问题讨论】:

    标签: azure azure-devops azure-resource-manager azure-template


    【解决方案1】:

    我相信您正在寻找的内容位于此处: https://docs.microsoft.com/en-us/azure/azure-resource-manager/resource-group-create-multiple

    您可以在哪里使用copyIndex() 函数在循环中迭代以创建多个资源。

    上面的链接有很好的例子和一种简洁的方法,但是对于上面的模板,如果您希望命名约定也与您的 NIC 等保持一致,则需要对模板的大部分内容进行一些更改

    这样的事情应该可以帮助你开始:

    {
       "name" : "[concat(parameters('vmName'), copyIndex())]",
       "copy": {
          "name" : "vmCopy",
          "count": 10
       }
       ...
       ...
       ...
       "osProfile" : "[concat(parameters('vmName'), copyIndex())]"
       ...
       ...
    }
    

    这里的"copy" 属性设置了复制循环的名称以及它将迭代的次数。上面的链接中也有示例说明如何做到这一点。

    希望这会有所帮助!

    干杯, 拉奇

    【讨论】:

    • 谢谢,我查看了您发送的链接,看来我需要将子资源移动到与父资源相同的级别。但是它仍然无法正常工作,请参阅 EDIT1。非常感谢!
    • 对于该错误,请查看:stackoverflow.com/questions/26766882/… 我有一个类似的错误,这个答案之前已经解决了它
    • 您是否尝试构建 10 个虚拟机,在单个虚拟机上运行 10 个代理或 10 个 vsts 代理? @MarkAllison
    • 我重新阅读了您的问题,并相信您希望在一台机器上拥有 10 个代理。那么它可以在 JSON 中完成,但会很混乱。你会更好地编辑 installvstsagent.ps1 脚本
    • 我希望在单个 VM 上安装多个代理。我会看看编辑脚本。谢谢。
    【解决方案2】:

    我已经能够使用此脚本在服务器上安装多个代理:

    Get-ExecutionPolicy
    
    Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope CurrentUser
    
    for($i=0; $i -le 9;  $i++)
    {
        $suffix=$i.ToString('00')
        $folder="vsts-agent-win-x64-2.144.2-$suffix"
        $agent="myagent-$suffix"
    
        "$PSScriptRoot\$folder"
        #note: folder needs to be copied for each instance
        #'vsts-agent-win-x64-2.144.2' is the folder after downloading and unzipping agent -manually.
        Copy-Item "$PSScriptRoot\vsts-agent-win-x64-2.144.2" "$PSScriptRoot\$folder" -Recurse
    
        cd $PSScriptRoot\$folder
     #.\config.cmd --unattended --url https://myaccount.visualstudio.com --auth pat --token myToken --pool 
    #default --agent myAgent --runAsAutoLogon --windowsLogonAccount myDomain\myUserName 
    #--windowsLogonPassword myPassword
        Write-Host "vsts-agent-win-x64-2.144.2-$suffix"
    
         .\config.cmd    --unattended `
                        --url "https://devops.my.org/org/" ` #(url of tfs)
                        --auth "pat" `
                        --token "u7s2mbna5v7heqzyfmz5ufrnvlektessebs7flfaf2ll4efzuj7q" `  (tfs token)
                        --pool "foiaModernization" `
                        --agent $agent `
                        --replace `
                        --acceptTeeEula `
                        --runAsService `
                        --windowsLogonAccount "xxx\yyy" `  #not sure if these lines are needed
                        --windowsLogonPassword "JJJJJlllll!!11" #not sure if these lines are 
    

    }

    【讨论】:

      猜你喜欢
      • 2019-07-09
      • 1970-01-01
      • 1970-01-01
      • 2016-10-27
      • 1970-01-01
      • 1970-01-01
      • 2020-02-14
      • 1970-01-01
      • 2014-11-28
      相关资源
      最近更新 更多