条件语句不工作的VM分机

问题描述 投票:1回答:1

在蔚蓝的VM分机条件语句不工作。我有一个基于数量的VM提供可变创建执行扩展块。我创建这个模板的多个虚拟机。我曾尝试下面的代码与条件,但没有运气。我想基于numberofvm变量值执行块。完整的模板代码如下。

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "location": {
      "type": "string",
      "defaultValue": "[resourceGroup().location]",
      "metadata": {
        "description": "Location for the resources."
      }
    },
    "vmName": {
      "type": "string",
      "defaultValue": "vm",
      "metadata": {
        "description": "Name for the Virtual Machine."
      }
    },
    "ClusterType": {
      "type": "string",
      "defaultValue": "3 vm apache",
      "metadata": {
        "description": "Type of cluster to deploy, this is using a single storage account"
      }
    },
    "adminUsername": {
      "type": "string",
      "defaultValue": "centos",
      "metadata": {
        "description": "User name for the Virtual Machine."
      }
    },
    "adminPasswordOrKey": {
      "type": "string"
    },
    "vmSize": {
      "type": "string",
      "metadata": {
        "description": "Size for the Virtual Machine."
      }
    },
    "storageNewOrExisting": {
      "type": "string",
      "defaultValue": "new",
      "metadata": {
        "description": "Determines whether or not a new storage account should be provisioned."
      }
    },
    "storageAccountName": {
      "type": "string",
      "defaultValue": "[concat('storage', uniqueString(resourceGroup().id))]",
      "metadata": {
        "description": "Name of the storage account"
      }
    },
    "storageAccountType": {
      "type": "string",
      "defaultValue": "Standard_LRS",
      "metadata": {
        "description": "Storage account type"
      }
    },
    "storageAccountResourceGroupName": {
      "type": "string",
      "defaultValue": "[resourceGroup().name]",
      "metadata": {
        "description": "Name of the resource group for the existing storage account"
      }
    },
    "virtualNetworkNewOrExisting": {
      "type": "string",
      "defaultValue": "new",
      "metadata": {
        "description": "Determines whether or not a new virtual network should be provisioned."
      }
    },
    "virtualNetworkName": {
      "type": "string",
      "defaultValue": "VirtualNetwork",
      "metadata": {
        "description": "Name of the virtual network"
      }
    },
    "addressPrefixes": {
      "type": "array",
      "defaultValue": [
        "10.0.0.0/16"
      ],
      "metadata": {
        "description": "Address prefix of the virtual network"
      }
    },
    "subnetName": {
      "type": "string",
      "defaultValue": "default",
      "metadata": {
        "description": "Name of the subnet"
      }
    },
    "subnetPrefix": {
      "type": "string",
      "defaultValue": "10.0.0.0/24",
      "metadata": {
        "description": "Subnet prefix of the virtual network"
      }
    },
    "virtualNetworkResourceGroupName": {
      "type": "string",
      "defaultValue": "[resourceGroup().name]",
      "metadata": {
        "description": "Name of the resource group for the existing virtual network"
      }
    },
    "vmDataDiskSize":{
      "type": "string",
      "defaultValue": "50",
      "metadata": {
        "description": "Minimum data disk size should be 50 GB"
      }    
    }

  },
  "variables": {
    "publisher": "OpenLogic",
    "offer": "CentOS",
    "sku": "7.3",
    "version": "latest",
    "vmBootDiskSize": 50,
    "nicName": "[concat(parameters('vmName'), '-nic-')]",
    "numberOfVM": "[int(first(parameters('ClusterType')))]",
    "apacheinstallation": "[contains(parameters('ClusterType'), 'apache')]",
    "networkSecurityGroupName": "[concat(parameters('vmName'), '-nsg-ssh')]",
    "publicIpName": "[concat(parameters('vmName'),'-publicip')]"
  },
  "resources": [
    {
      "condition": "[equals(parameters('storageNewOrExisting'), 'new')]",
      "type": "Microsoft.Storage/storageAccounts",
      "apiVersion": "2018-02-01",
      "name": "[parameters('storageAccountName')]",
      "location": "[parameters('location')]",
      "kind": "Storage",
      "sku": {
        "name": "[parameters('storageAccountType')]"
      }
    },
    {
      "type": "Microsoft.Network/publicIPAddresses",
      "apiVersion": "2018-04-01",
      "name": "[concat(variables('publicIpName'), copyIndex())]",
      "location": "[parameters('location')]",
      "copy": {
        "name": "ipLoop",
        "count": "[variables('numberOfVM')]"
      },
      "sku": {
        "name": "Basic"
      },    
      "properties": {
        "publicIPAllocationMethod": "Dynamic"
      }
    },
    {
      "condition": "[equals(parameters('virtualNetworkNewOrExisting'), 'new')]",
      "type": "Microsoft.Network/virtualNetworks",
      "apiVersion": "2018-04-01",
      "name": "[parameters('virtualNetworkName')]",
      "location": "[parameters('location')]",
      "properties": {
        "addressSpace": {
          "addressPrefixes": "[parameters('addressPrefixes')]"
        },
        "subnets": [
          {
            "name": "[parameters('subnetName')]",
            "properties": {
              "addressPrefix": "[parameters('subnetPrefix')]"
            }
          }
        ]
      }
    },
    {
      "name": "[variables('networkSecurityGroupName')]",
      "type": "Microsoft.Network/networkSecurityGroups",
      "apiVersion": "2018-04-01",
      "location": "[parameters('location')]",
      "properties": {
        "securityRules": [
          {
            "name": "default-allow-ssh",
            "properties": {
              "priority": 1000,
              "sourceAddressPrefix": "*",
              "protocol": "Tcp",
              "destinationPortRange": "22",
              "access": "Allow",
              "direction": "Inbound",
              "sourcePortRange": "*",
              "destinationAddressPrefix": "*"
            }
          },
          {
            "name": "allow-webport-8080",
            "properties": {
              "priority": 1200,
              "sourceAddressPrefix": "*",
              "protocol": "Tcp",
              "destinationPortRange": "8080",
              "access": "Allow",
              "direction": "Inbound",
              "sourcePortRange": "*",
              "destinationAddressPrefix": "*"
            }
          },          
        ]
      }
    },
    {
      "apiVersion": "2018-04-01",
      "type": "Microsoft.Network/networkInterfaces",
      "name": "[concat(variables('nicName'), copyindex())]",
      "location": "[parameters('location')]",
      "copy": {
        "name": "nicLoop",
        "count": "[variables('numberOfVM')]"
      },
      "dependsOn": [
        "ipLoop",
        "[parameters('virtualNetworkName')]",
        "[variables('networkSecurityGroupName')]"
      ],
      "properties": {
        "ipConfigurations": [
          {
            "name": "ipconfig1",
            "properties": {
              "privateIPAllocationMethod": "Dynamic",
              "publicIPAddress": {
                "id": "[resourceId('Microsoft.Network/publicIPAddresses', concat(variables('publicIpName'), copyIndex()))]"
              },
              "subnet": {
                "id": "[resourceId(parameters('virtualNetworkResourceGroupName'), 'Microsoft.Network/virtualNetworks/subnets/', parameters('virtualNetworkName'), parameters('subnetName'))]"
              }
            }
          }
        ],
        "networkSecurityGroup": {
          "id": "[resourceId('Microsoft.Network/networkSecurityGroups', variables('networkSecurityGroupName'))]"
        }
      }
    },
    {
      "apiVersion": "2018-04-01",
      "type": "Microsoft.Compute/virtualMachines",
      "name": "[concat(parameters('vmName'), copyIndex())]",
      "location": "[parameters('location')]",
      "copy": {
        "name": "virtualMachineLoop",
        "count": "[variables('numberOfVM')]"
      },
      "dependsOn": [
        "[parameters('storageAccountName')]",
        "nicLoop"
      ],
      "properties": {
        "hardwareProfile": {
          "vmSize": "[parameters('vmSize')]"
        },
        "osProfile": {
          "computerName": "[concat(parameters('vmName'), copyIndex())]",
          "adminUsername": "[parameters('adminUsername')]",
          "adminPassword": "[parameters('adminPasswordOrKey')]",
          "linuxConfiguration": {
            "disablePasswordAuthentication": true,
            "ssh": {
              "publicKeys": [
                {
                  "path": "[concat('/home/', parameters('adminUsername'), '/.ssh/authorized_keys')]",
                  "keyData": "[parameters('adminPasswordOrKey')]"
                }
              ]
            }
          }
        },
        "storageProfile": {
          "imageReference": {
            "publisher": "[variables('publisher')]",
            "offer": "[variables('offer')]",
            "sku": "[variables('sku')]",
            "version": "[variables('version')]"
          },
          "osDisk": {
            "caching": "ReadWrite",
            "createOption": "FromImage",
            "diskSizeGB": "[variables('vmBootDiskSize')]"
          },
          "copy": [
            {
              "name": "dataDisks",
              "count": 1,
              "input": {
                "caching": "ReadWrite",
                "diskSizeGB": "[parameters('vmDataDiskSize')]",
                "lun": "[copyIndex('dataDisks')]",
                "name": "[concat(parameters('vmName'), '-datadisk', copyIndex(), copyIndex('dataDisks'))]",
                "createOption": "Empty"
              }
            }
          ]
        },
        "networkProfile": {
          "networkInterfaces": [
            {
              "id": "[resourceId('Microsoft.Network/networkInterfaces', concat(variables('nicName'), copyindex()))]"
            }
          ]
        },
        "diagnosticsProfile": {
          "bootDiagnostics": {
            "enabled": true,
            "storageUri": "[reference(resourceId(parameters('storageAccountResourceGroupName'), 'Microsoft.Storage/storageAccounts/', parameters('storageAccountName')), '2018-02-01').primaryEndpoints.blob]"
          }
        }
      },
      "resources": [
        {
          "condition": "[equals(variables('numberOfVM'), 3)]",
          "name": "config-app-3",
          "type": "extensions",
          "location": "[parameters('location')]",
          "apiVersion": "2015-06-15",
          "dependsOn": [
             "[concat('Microsoft.Compute/virtualMachines/', concat(parameters('vmName'), 0))]",
             "[concat('Microsoft.Compute/virtualMachines/', concat(parameters('vmName'), 1))]",
             "[concat('Microsoft.Compute/virtualMachines/', concat(parameters('vmName'), 2))]",
           ],
          "tags": {
            "displayName": "config-app-3"
          },
          "properties": {
            "publisher": "Microsoft.Azure.Extensions",
            "type": "CustomScript",
            "typeHandlerVersion": "2.0",
            "autoUpgradeMinorVersion": true,
            "protectedSettings": {
              "commandToExecute": "[concat('sh /var/lib/waagent/custom-script/download/0/install_apache.sh')]"
            }
          }
        },
        {
          "condition": "[equals(variables('numberOfVM'), 5)]",
          "name": "config-app-5",
          "type": "extensions",
          "location": "[parameters('location')]",
          "apiVersion": "2015-06-15",
          "dependsOn": [
             "[concat('Microsoft.Compute/virtualMachines/', concat(parameters('vmName'), 0))]",
             "[concat('Microsoft.Compute/virtualMachines/', concat(parameters('vmName'), 1))]",
             "[concat('Microsoft.Compute/virtualMachines/', concat(parameters('vmName'), 2))]",
             "[concat('Microsoft.Compute/virtualMachines/', concat(parameters('vmName'), 3))]",
             "[concat('Microsoft.Compute/virtualMachines/', concat(parameters('vmName'), 4))]",
           ],
          "tags": {
            "displayName": "config-app-5"
          },
          "properties": {
            "publisher": "Microsoft.Azure.Extensions",
            "type": "CustomScript",
            "typeHandlerVersion": "2.0",
            "autoUpgradeMinorVersion": true,
            "protectedSettings": {
              "commandToExecute": "[concat('sh /var/lib/waagent/custom-script/download/0/install_apache.sh')]"
            }
          }
        }
      ]
   }
  ],
  "outputs": {
  }
}

我收到以下错误,同时部署。 “模板参考‘VM-NIC-3’是无效的:找不到具有此名称的模板资源或资源副本。请参阅https://aka.ms/arm-template-expressions/#reference的使用细节。“。

azure arm-template azure-template
1个回答
2
投票

这个错误无关,与你的条件,它发生在你引用您拨打vm-nic-1资源其他地方。它不存在的,所以最有可能叫别的东西。

好吧,看你的模板,我想我明白你正在尝试做的,你尝试应用取决于您的模板的情况不同扩展名。但模板后,工作条件进行评估,因此你收到此错误(如用3个虚拟机的情况下,只有网卡正在创建0,1,2,其寻找的NIC 3和4的一样)。解决这个的最简单方法是移动扩展单独的资源,并依赖于vmloop,所以它不会在意如何创建多的虚拟机,这也将创造1个扩展为每个虚拟机。如果你想要不同的东西在里面延期发生,你可以使用if()功能在模板中定义它们。

{
    "apiVersion": "2018-04-01",
    "type": "Microsoft.Compute/virtualMachines",
    "name": "[concat(parameters('vmName'), copyIndex())]",
    "location": "[parameters('location')]",
    "copy": {
        "name": "virtualMachineLoop",
        "count": "[variables('numberOfVM')]"
    },
    "dependsOn": [
        "[parameters('storageAccountName')]",
        "nicLoop"
    ],
    "properties": {
        "hardwareProfile": {
            "vmSize": "[parameters('vmSize')]"
        },
        "osProfile": {
            "computerName": "[concat(parameters('vmName'), copyIndex())]",
            "adminUsername": "[parameters('adminUsername')]",
            "adminPassword": "[parameters('adminPasswordOrKey')]"
        },
        "storageProfile": {
            "imageReference": {
                "publisher": "[variables('publisher')]",
                "offer": "[variables('offer')]",
                "sku": "[variables('sku')]",
                "version": "[variables('version')]"
            },
            "osDisk": {
                "caching": "ReadWrite",
                "createOption": "FromImage",
                "diskSizeGB": "[variables('vmBootDiskSize')]"
            },
            "copy": [
                {
                    "name": "dataDisks",
                    "count": 1,
                    "input": {
                        "caching": "ReadWrite",
                        "diskSizeGB": "[parameters('vmDataDiskSize')]",
                        "lun": "[copyIndex('dataDisks')]",
                        "name": "[concat(parameters('vmName'), '-datadisk', copyIndex(), copyIndex('dataDisks'))]",
                        "createOption": "Empty"
                    }
                }
            ]
        },
        "networkProfile": {
            "networkInterfaces": [
                {
                    "id": "[resourceId('Microsoft.Network/networkInterfaces', concat(variables('nicName'), copyindex()))]"
                }
            ]
        },
        "diagnosticsProfile": {
            "bootDiagnostics": {
                "enabled": true,
                "storageUri": "[reference(resourceId(parameters('storageAccountResourceGroupName'), 'Microsoft.Storage/storageAccounts/', parameters('storageAccountName')), '2018-02-01').primaryEndpoints.blob]"
            }
        }
    }
},
{
    "condition": "[equals(variables('numberOfVM'), 3)]",
    "name": "[concat(parameters('vmName'), copyIndex(), '/config-app-3')]",
    "type": "Microsoft.Compute/virtualMachines/extensions",
    "location": "[parameters('location')]",
    "apiVersion": "2015-06-15",
    "dependsOn": [
        "virtualMachineLoop"
    ],
    "copy": {
        "name": "extensionLoop",
        "count": "[variables('numberOfVM')]"
    },
    "tags": {
        "displayName": "config-app-3"
    },
    "properties": {
        "publisher": "Microsoft.Azure.Extensions",
        "type": "CustomScript",
        "typeHandlerVersion": "2.0",
        "autoUpgradeMinorVersion": true,
        "protectedSettings": {
            "commandToExecute": "[concat('sh /var/lib/waagent/custom-script/download/0/install_apache.sh')]"
        }
    }
}

PS。我从VM定义remowed非工作的一部分,你的脚本扩展定义是错误的为好。

© www.soinside.com 2019 - 2024. All rights reserved.