Search code examples
azureautomationazure-resource-manager

ARM : Get network Interface Private IP and Default Gateway


im deploying an ARM template for a virtual machine with network interfaces

i have a .sh script that needs parameters to be passed to it

parameters :

NIC private ip

NIC default Gateway

nic template :

{
  "type": "Microsoft.Network/networkInterfaces",
  "apiVersion": "2019-06-01",
  "name": "[parameters('nic2name')]",
  "location": "[parameters('location')]",
  "dependsOn": [
    "[parameters('virtualNetworkName')]"
  ],
  "properties": {
    "ipConfigurations": [
      {
        "name": "ipconfig1",
        "properties": {
          "privateIPAllocationMethod": "Dynamic",
          "subnet": {
            "id": "[variables('subnetRef')]"
          },
          "primary": true
        }
      }
    ],
    "primary": false
  }
}

im sitting variables to get this parameters but idk how to get them

"smnet_dev": "[resourceId('Microsoft.Network/networkInterfaces', parameters('nic2name')).ipConfigurations]",
"smnet_dflt_gw": "[resourceId('Microsoft.Network/networkInterfaces', parameters('nic2name')).<default gateway>]"

i hope someone can guide me to the right direction


Solution

  • AFAIK, We can not give it directly instead of that we can use PowerShell script or bicep which will retrieve the value of nic and put it in variable.

    In ARM while we are creating resources (e.g virtual machine with nic) we can add the variable as following and then we can deploy the NIC with gateways.

    Things we have tried to create a virtual Machine with nic and provide the variable as shown below it got succeed .

    You can use the below template by passing your required name in parameter file for the Automation.

    Template.json:-

    {
    "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
    "location": {
    "type": "string"
    },
    "networkInterfaceName": {
    "type": "string"
    },
    "enableAcceleratedNetworking": {
    "type": "bool"
    },
    "networkSecurityGroupName": {
    "type": "string"
    },
    "networkSecurityGroupRules": {
    "type": "array"
    },
    "subnetName": {
    "type": "string"
    },
    "virtualNetworkName": {
    "type": "string"
    },
    "addressPrefixes": {
    "type": "array"
    },
    "subnets": {
    "type": "array"
    },
    "publicIpAddressName": {
    "type": "string"
    },
    "publicIpAddressType": {
    "type": "string"
    },
    "publicIpAddressSku": {
    "type": "string"
    },
    "pipDeleteOption": {
    "type": "string"
    },
    "virtualMachineName": {
    "type": "string"
    },
    
    "virtualMachineComputerName": {
    "type": "string"
    },
    "virtualMachineRG": {
    "type": "string"
    },
    "osDiskType": {
    "type": "string"
    },
    "osDiskDeleteOption": {
    "type": "string"
    },
    "virtualMachineSize": {
    "type": "string"
    },
    "nicDeleteOption": {
    "type": "string"
    },
    "adminUsername": {
    "type": "string"
    },
    "adminPassword": {
    "type": "secureString"
    },
    "patchMode": {
    "type": "string"
    },
    "enableHotpatching": {
    "type": "bool"
    },
    "tags": {
     "type": "object"
     }
     },
    "variables": {
                                  "nsgId": "[resourceId(resourceGroup().name, 'Microsoft.Network/networkSecurityGroups', parameters('networkSecurityGroupName'))]",
                                  "vnetName": "[parameters('virtualNetworkName')]",
                                  "vnetId": "[resourceId(resourceGroup().name,'Microsoft.Network/virtualNetworks', parameters('virtualNetworkName'))]",
                                  "subnetRef": "[concat(variables('vnetId'), '/subnets/', parameters('subnetName'))]"
                          
    },
                                  "resources": [
                                      {
                                          "name": "[parameters('networkInterfaceName')]",
                                          "type": "Microsoft.Network/networkInterfaces",
                                          "apiVersion": "2021-03-01",
                                          "location": "[parameters('location')]",
                                          "dependsOn": [
                                              "[concat('Microsoft.Network/networkSecurityGroups/', parameters('networkSecurityGroupName'))]",
                                              "[concat('Microsoft.Network/virtualNetworks/', parameters('virtualNetworkName'))]",
                                              "[concat('Microsoft.Network/publicIpAddresses/', parameters('publicIpAddressName'))]"
                                          ],
                                          "properties": {
                                              "ipConfigurations": [
                                                  {
                                                      "name": "ipconfig1",
                                                      "properties": {
                                                          "subnet": {
                                                              "id": "[variables('subnetRef')]"
                                                          },
                                                          "privateIPAllocationMethod": "Dynamic",
                                                          "publicIpAddress": {
                                                              "id": "[resourceId(resourceGroup().name, 'Microsoft.Network/publicIpAddresses', parameters('publicIpAddressName'))]",
                                                              "properties": {
                                                                  "deleteOption": "[parameters('pipDeleteOption')]"
                                                              }
                                                          }
                                                      }
                                                  }
                                              ],
                                              "enableAcceleratedNetworking": "[parameters('enableAcceleratedNetworking')]",
                                              "networkSecurityGroup": {
                                                  "id": "[variables('nsgId')]"
                                              }
                                          },
                                          "tags": "[parameters('tags')]"
                                      },
                                      {
                                          "name": "[parameters('networkSecurityGroupName')]",
                                          "type": "Microsoft.Network/networkSecurityGroups",
                                          "apiVersion": "2019-02-01",
                                          "location": "[parameters('location')]",
                                          "properties": {
                                              "securityRules": "[parameters('networkSecurityGroupRules')]"
                                          },
                                          "tags": "[parameters('tags')]"
                                      },
                                      {
                                          "name": "[parameters('virtualNetworkName')]",
                                          "type": "Microsoft.Network/virtualNetworks",
                                          "apiVersion": "2020-11-01",
                                          "location": "[parameters('location')]",
                                          "properties": {
                                              "addressSpace": {
                                                  "addressPrefixes": "[parameters('addressPrefixes')]"
                                              },
                                              "subnets": "[parameters('subnets')]"
                                          },
                                          "tags": "[parameters('tags')]"
                                      },
                                      {
                                          "name": "[parameters('publicIpAddressName')]",
                                          "type": "Microsoft.Network/publicIpAddresses",
                                          "apiVersion": "2019-02-01",
                                          "location": "[parameters('location')]",
                                          "properties": {
                                              "publicIpAllocationMethod": "[parameters('publicIpAddressType')]"
                                          },
                                          "sku": {
                                              "name": "[parameters('publicIpAddressSku')]"
                                          },
                                          "tags": "[parameters('tags')]"
                                      },
                                      {
                                          "name": "[parameters('virtualMachineName')]",
                                          "type": "Microsoft.Compute/virtualMachines",
                                          "apiVersion": "2021-07-01",
                                          "location": "[parameters('location')]",
                                          "dependsOn": [
                                              "[concat('Microsoft.Network/networkInterfaces/', parameters('networkInterfaceName'))]"
                                          ],
                                          "properties": {
                                              "hardwareProfile": {
                                                  "vmSize": "[parameters('virtualMachineSize')]"
                                              },
                                              "storageProfile": {
                                                  "osDisk": {
                                                      "createOption": "fromImage",
                                                      "managedDisk": {
                                                          "storageAccountType": "[parameters('osDiskType')]"
                                                      },
                                                      "deleteOption": "[parameters('osDiskDeleteOption')]"
                                                  },
                                                  "imageReference": {
                                                      "publisher": "MicrosoftWindowsServer",
                                                      "offer": "WindowsServer",
                                                      "sku": "2019-datacenter-gensecond",
                                                      "version": "latest"
                                                  }
                                              },
                                              "networkProfile": {
                                                  "networkInterfaces": [
                                                      {
                                                          "id": "[resourceId('Microsoft.Network/networkInterfaces', parameters('networkInterfaceName'))]",
                                                          "properties": {
                                                              "deleteOption": "[parameters('nicDeleteOption')]"
                                                          }
                                                      }
                                                  ]
                                              },
                                              "osProfile": {
                                                  "computerName": "[parameters('virtualMachineComputerName')]",
                                                  "adminUsername": "[parameters('adminUsername')]",
                                                  "adminPassword": "[parameters('adminPassword')]",
                                                  "windowsConfiguration": {
                                                      "enableAutomaticUpdates": true,
                                                      "provisionVmAgent": true,
                                                      "patchSettings": {
                                                          "enableHotpatching": "[parameters('enableHotpatching')]",
                                                          "patchMode": "[parameters('patchMode')]"
                                                      }
                                                  }
                                              },
                                              "licenseType": "Windows_Server"
                                          },
                                          "tags": "[parameters('tags')]"
                                      }
                                  ],
                                  "outputs": {
                                      "adminUsername": {
                                          "type": "string",
                                          "value": "[parameters('adminUsername')]"
                                      }
                                  }
                              }
    

    From this aforementioned script here is our NIC template ;

    "resources": [
                                      {
                                          "name": "[parameters('networkInterfaceName')]",
                                          "type": "Microsoft.Network/networkInterfaces",
                                          "apiVersion": "2021-03-01",
                                          "location": "[parameters('location')]",
                                          "dependsOn": [
                                              "[concat('Microsoft.Network/networkSecurityGroups/', parameters('networkSecurityGroupName'))]",
                                              "[concat('Microsoft.Network/virtualNetworks/', parameters('virtualNetworkName'))]",
                                              "[concat('Microsoft.Network/publicIpAddresses/', parameters('publicIpAddressName'))]"
                                          ],
                                          "properties": {
                                              "ipConfigurations": [
                                                  {
                                                      "name": "ipconfig1",
                                                      "properties": {
                                                          "subnet": {
                                                              "id": "[variables('subnetRef')]"
                                                          },
                                                          "privateIPAllocationMethod": "Dynamic",
                                                          "publicIpAddress": {
                                                              "id": "[resourceId(resourceGroup().name, 'Microsoft.Network/publicIpAddresses', parameters('publicIpAddressName'))]",
                                                              "properties": {
                                                                  "deleteOption": "[parameters('pipDeleteOption')]"
                                                              }
                                                          }
                                                      }
                                                  }
                                              ],
    

    OUTPUT SCREENSHOT FOR REFERENCE:- enter image description here enter image description here

    enter image description here enter image description here

    For more information please refer the below links:-