Akash Masand
Akash Masand

Reputation: 1741

How to replace the last octet of IP address in ARM template

I am trying to pass a parameter with an IP address prefix (say, 10.0.0.0/24) in an ARM template to deploy a VNET

In the ARM template, I would like to replace 10.0.0.0/24 with 10.0.0.123 and assign it to a NIC which I would like to use later to create a VM.

I was hoping is there any way to achieve this using an ARM template?

Upvotes: 0

Views: 554

Answers (3)

Jon Grey
Jon Grey

Reputation: 11

I'm doing like that

"variables" : {
    "subnetAddress" : "[first(split(parameters('subnetAddressWithMask'), '/'))]",
    "subnetOctets3" : "[take(split(variables('subnetAddress'), '.'), 3)]",
    "subnetNetwork" : "[concat(variables('subnetOctets3')[0], '.', variables('subnetOctets3')[1], '.', variables('subnetOctets3')[2])]"
  },
  "outputs": {
    "subnetAddress" : {
    "type": "string",
    "value" : "[variables('subnetAddress')]"
    },
    "subnetOctets3" : 
    {

    "type": "array",
    "value" :  "[variables('subnetOctets3')]"
    }
   ,
    "subnetNetwork" : {
"type" : "string",
"value" :     "[variables('subnetNetwork')]"

    }

with output

  "Outputs": {
    "deploymentAppGw": {
      "Type": "Object",
      "Value": {
        "subnetAddress": {
          "type": "String",
          "value": "10.0.0.0"
        },
        "subnetOctets3": {
          "type": "Array",
          "value": [
            "10",
            "0",
            "0"
          ]
        },
        "subnetNetwork": {
          "type": "String",
          "value": "10.0.0"
        }
      }
    }
  },

Later I'm using it in this way: "[concat(variables('subnetNetwork'), '.', copyIndex(10))]" or in similar way.

Upvotes: 0

4c74356b41
4c74356b41

Reputation: 72191

Well, there is no pretty way of doing this, easiest one:

"var1": "[first(split(parameters('addressPrefix'), '/'))]",
"var2": "[substring(variables('var1'), 0, sub(length(variables('var1'), 1)))]"
"var3": "[concat(variables('var2'), 'ipgoeshere')]"

alternatively you can just chop out last 4 characters and concat ip address, or split and concat parts into an ip address:

"var1": "[first(split(parameters('addressPrefix'), '/'))]",
"var2": "[concat(variables('var1')[0], '.', variables('var1')[1], '.', variables('var1')[2], '.ipgoeshere')]"

Upvotes: 1

Jason Ye
Jason Ye

Reputation: 13974

You can use "privateIPAllocationMethod": "Static" and specify the private IP address, like this:

"ipConfigurations": [
          {
            "name": "ipconfig1",
            "properties": {
              "privateIPAllocationMethod": "Static",
              "privateIPAddress": "[parameters('privateIPAddress')]",
              "publicIPAddress": {
                "id": "[resourceId('Microsoft.Network/publicIPAddresses',parameters('publicIPAddressName'))]"
              },
              "subnet": {
                "id": "[parameters('subnetRef')]"
              }
            }
          }
        ]

Here an example template about static private IP address, please refer to it.

Upvotes: 0

Related Questions