首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在ARM模板中多次运行同一脚本?

如何在ARM模板中多次运行同一脚本?
EN

Stack Overflow用户
提问于 2018-07-02 22:50:31
回答 2查看 1.6K关注 0票数 2

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

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

代码语言:javascript
复制
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.

我的问题是,如何使用copy函数安装10个构建代理,以便拥有vsts-agent-1, vsts-agent-2, etc

下面是该模板的相关代码片段:

代码语言:javascript
复制
{
      "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

我已经更新了模板并移出子资源,以致子资源与父资源处于同一级别。此部分现在如下所示:

代码语言:javascript
复制
{
  "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.

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-07-03 08:45:09

我相信你要找的东西就在这里:https://docs.microsoft.com/en-us/azure/azure-resource-manager/resource-group-create-multiple

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

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

像这样的东西应该会帮助你开始行动:

代码语言:javascript
复制
{
   "name" : "[concat(parameters('vmName'), copyIndex())]",
   "copy": {
      "name" : "vmCopy",
      "count": 10
   }
   ...
   ...
   ...
   "osProfile" : "[concat(parameters('vmName'), copyIndex())]"
   ...
   ...
}

此处的"copy"属性设置复制循环的名称和它将遍历的计数。在上面的链接中也有如何做到这一点的示例。

希望这能有所帮助!

干杯,拉奇

票数 0
EN

Stack Overflow用户

发布于 2019-10-23 21:41:20

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

代码语言:javascript
复制
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 

}

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51138840

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档