Search code examples
azureazure-resource-managerazure-rm-template

Assigning Static Ip to nic(copy loop) in ARM template


I am trying to change dynamic private ip allocated VM to Static IP via ARM template.

It works for a single vm. But facing issue for multiple VM's deployment. I am trying nested deployment.

The error I am facing is: The template reference "nic-"somevmname"-01" is not valid: could not find template resource or resource copy with this name.

I am not able to figure out what is wrong with the nested resource

{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
 "parameters": {

"VnetResourceGroup": {
  "type": "string"
},
"VnetName": {
  "type": "string"
},
"SubnetName": {
  "type": "string"
},

"adminusername": {
  "type": "string",
  "metadata": {
    "description": "Admin username for VM"
  }

},
"adminpassword": {
  "type": "string"
},
"vm-name": {
  "type": "string",
  "metadata": {
    "description": ""
  }
},
"virtualMachineCount": {
  "type": "int",

  "metadata": {
    "description": "Number of Virtual machines to be deployed"
  }
},
"vmSize": {
  "type": "string",
  "metadata": {
    "description": "description"
  }
},
"vm-image-name": {
  "type": "string",
  "metadata": {
    "description": "description"
  }
},
"vm-image-rg": {
  "type": "string",
  "metadata": {
    "description": "description"
  }
},

"dataDisksize": {
  "type": "int"
},
"datadisks-count": {
  "type": "int"

},

"osDiskType": {
  "type": "string"

},
"osDiskSize": {
  "type": "int",
  "metadata": {
    "description": "description"
  }
},
"maxAvailabilityzones": {
  "type": "int",
  "metadata": {
    "description": "description"
  }
}
  },

"variables": {


"ImageReferenceId": "[concat(subscription().id, '/resourceGroups/', parameters('vm-image-rg'), '/providers/Microsoft.Compute/images/', parameters('vm-image-name'))]",
"vnetId": "[concat(subscription().id, '/resourceGroups/', parameters('VnetResourceGroup'), '/providers/Microsoft.Network/virtualNetworks/', parameters('VnetName'))]",
"subnetRef": "[concat(variables('vnetId'), '/subnets/', parameters('subnetName'))]"
},
 "resources": [
{
  "type": "Microsoft.Network/networkInterfaces",
  "name": "[concat('nic-',parameters('vm-name'),'-0', copyIndex(1))]",
  "apiVersion": "2018-11-01",
  "location": "[resourceGroup().location]",
  "copy": {
    "name": "nicLoop",
    "count": "[parameters('virtualMachineCount')]"
  },
  "properties": {
    "enableAcceleratedNetworking": true,
    "ipConfigurations": [
      {
        "name": "ipconfig1",
        "properties": {
          "privateIPAllocationMethod": "Dynamic",
          "subnet": {
            "id": "[variables('subnetRef')]"
          }
        }
      }
    ]
  }
},

{
  "type": "Microsoft.Compute/virtualMachines",
  "name": "[concat(parameters('vm-name'),'-0', copyIndex(1))]",
  "apiVersion": "2020-06-01",
  "location": "[resourceGroup().location]",
  "zones": [
    "[string(add(mod(copyIndex(0), parameters('maxAvailabilityzones')), 1))]"
  ],
  "copy": {
    "name": "virtualMachineLoop",
    "count": "[parameters('virtualMachineCount')]"
  },
  "dependsOn": [
    "nicLoop"


  ],
  "properties": {
    "hardwareProfile": {
      "vmSize": "[parameters('vmSize')]"
    },
    "osProfile": {
      "computerName": "[concat(parameters('vm-name'),'-0', copyIndex(1))]",
      "adminusername": "[parameters('adminusername')]",
      "adminPassword": "[parameters('adminpassword')]"


    },


    "storageProfile": {
      "copy": [
        {
          "name": "dataDisks",
          "count": "[parameters('datadisks-count')]",
          "input": {
            "lun": "[copyIndex('dataDisks')]",
            "name": "[concat('dataDisk',copyIndex('dataDisks',1),'-',parameters('vm-name'),'-0',copyIndex(1))]",
            "createOption": "Empty",
            "diskSizeGB": "[parameters('dataDisksize')]"
          }
        }
      ],

      "imageReference": { "id": "[variables('ImageReferenceId')]" },
      "osDisk": {
        "name": "[concat('osdisk-',parameters('vm-name'),'-0', copyIndex(1))]",
        "createOption": "FromImage",
        "caching": "ReadWrite",
        "diskSizeGB": "[parameters('osDiskSize')]",
        "managedDisk": {
          "storageAccountType": "[parameters('osDiskType')]"
        }
      }
    },
    "networkProfile": {
      "networkInterfaces": [
        {
          "id": "[resourceId('Microsoft.Network/networkInterfaces', concat('nic-',parameters('vm-name'),'-0', copyIndex(1)))]"
        }
      ]
    },
    "diagnosticsProfile": {
      "bootDiagnostics": {
        "enabled": true
      }
    }
  }
},
{
  "type": "Microsoft.Resources/deployments",
  "name": "StaticIP",
  "apiVersion": "2020-06-01",
  "dependsOn": [
    "nicLoop",
    "virtualMachineLoop"
  ],
  "properties": {
    "mode": "Incremental",
    "expressionEvaluationOptions": {
      "scope": "inner"
    },
    "parameters": {

      "SubnetRef": {
        "value": "[variables('SubnetRef')]"
      },

      "virtualMachineCount": {
        "value": "[parameters('virtualMachineCount')]"
      },
      "vm-name": {
        "value": "[parameters('vm-name')]"
      }

    },
    "template": {

      "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
      "contentVersion": "1.0.0.0",
      "parameters": {

        "subnetRef": {
          "type": "string"
        },


        "virtualMachineCount": {
          "type": "int",

          "metadata": {
            "description": "Number of Virtual machines to be deployed"
          }
        },
        "vm-name": {
          "type": "string",
          "metadata": {
            "description": "description"
          }
        }
      },

      "variables": {

      },
      "resources": [
        {
          "type": "Microsoft.Network/networkInterfaces",
          "name": "[concat('assignstaticip',copyIndex(1))]",
          "apiVersion": "2020-05-01",
          "location": "[resourceGroup().location]",
          "copy": {
            "name": "nicStaticIpLoop",
            "count": "[parameters('virtualMachineCount')]"
          },
          "properties": {
            "ipConfigurations": [
              {
                "name": "ipconfig1",
                "properties": {
                  "privateIPAllocationMethod": "Static",
                  "privateIPAddress": "[reference(concat('nic-',parameters('vm-name'),'-0', copyIndex('nicStaticIpLoop',1))).ipConfigurations[0].properties.privateIPAddress]",
                  "subnet": {
                    "id": "[parameters('subnetRef')]"
                  }
                }
              }
            ]
          }
        }
      ]
    }
  }
}
],
 "outputs": {

"vm-ipaddress": {
  "type": "array",
  "copy": {
    "count": "[parameters('virtualMachineCount')]",
    "input": "[reference(concat('nic-',parameters('vm-name'),'-0', copyIndex(1))).ipConfigurations[0].properties.privateIPAddress]"
  }
},
"vm-name": {
  "type": "array",
  "copy": {
    "count": "[parameters('virtualMachineCount')]",
    "input": "[concat(parameters('vm-name'),'-0', copyIndex(1))]"
  }
}
  }
 }

Solution

  • Thank you @Anonymouus, for sharing the update . And bring the question for how to change dynamic private ip allocated VM to Static IP via ARM template, It will be really helpful for the SO community for similar issue who encounter the same so that they can find & fix their problem by posting it as an answer .

    As stated in the given Blog which is Author by @Praveen.

    To change the dynamic IP to static IP through ARM TEMPLATE we can use the below example of template,

    ARM TEMPLATE:-

     {
          "type": "Microsoft.Resources/deployments",
          "apiVersion": "2018-05-01",
          "name": "[concat('StaticIp', copyIndex())]",
          "dependsOn": [
            "nicLoop"
          ],
          "copy": {
            "name": "ipLoop",
            "count": "[parameters('ServerInstanceCount')]"
          },
          "properties": {
            "mode": "Incremental",
            "template": {
              "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
              "contentVersion": "1.0.0.0",
              "resources": [
                {
                  "type": "Microsoft.Network/networkInterfaces",
                  "name": "[concat(parameters('ServerNamePrefix'),padLeft(copyIndex(),3,'0'),'-NIC')]",
                  "apiVersion": "2018-03-01",
                  "location": "[resourceGroup().location]",
                  "tags": "[parameters('tagValues')]",
                  "properties": {
                    "ipConfigurations": [
                      {
                        "name": "ipconfig1",
                        "properties": {
                          "privateIPAllocationMethod": "Static",
                          "privateIPAddress": "[reference(concat(parameters('ServerNamePrefix'),padLeft(copyIndex(),3,'0'),'-NIC')).ipConfigurations[0].properties.privateIPAddress]",
                          "subnet": {
                            "id": "[variables('subnetRef')]"
                          }
                        }
                      }
                    ],
                    "enableAcceleratedNetworking": true
                  }
                }
              ]
            }
          }
        }
    

    For more information please refer the below links:-