【问题标题】:Azure RM Template. Change vNet's DNS Server without deleting subnetsAzure RM 模板。在不删除子网的情况下更改 vNet 的 DNS 服务器
【发布时间】:2018-06-19 17:45:25
【问题描述】:

我正在创建一个 ARM 模板,它将将可变数量的备份域控制器部署到现有的虚拟网络。我需要更改现有虚拟网络的 DNS 服务器。问题是我的虚拟网络彼此具有完全不同的子网,当我部署以下资源时,现有子网消失了。如何只更改虚拟网络的 DNS Servers 属性,而不影响其他属性?

  "resources": [
    {
      "name": "[parameters('existingVnetname')]",
      "type": "Microsoft.Network/virtualNetworks",
      "location": "[parameters('location')]",
      "apiVersion": "2016-10-01",
      "tags": {
        "displayName": "UpdateVNetDNS2"
      },
      "dependsOn": [
        "ConfiguringBDCloop"
      ],
      "condition": "[parameters('deployBackupDCs')]",
      "properties": {
        "addressSpace": {
          "addressPrefixes": [
            "[parameters('existingVnetAddressRange')]"
          ]
        },
        "dhcpOptions": {
          "dnsServers": "[take(variables('privateIParray'),variables('DNSref'))]"
        },
        "subnets": [
          {
            "name": "[parameters('existingSubnetName')]",
            "properties": {
              "addressPrefix": "[parameters('existingSubnetAddressRange')]"
            }
          }
        ]
      }
    }
]

我知道我可以指定现有子网,但我有很多子网名称和地址范围完全不同的虚拟网络,通过参数指定它们将是一场噩梦。

【问题讨论】:

  • 我们最终使用 PowerShell 脚本而不是此 ARM 模板部署来更新虚拟网络的 DNS 服务器。

标签: azure azure-virtual-network arm-template azure-template


【解决方案1】:

我目前使用一个模板用于具有一个虚拟网络资源的多个环境,通过循环不同的配置来创建多个 vNet。

参数包含这些 vNet 的 vNet 前缀和子网前缀。例如:

"virtualNetworkPrefixHub": {
    "type": "string",
    "metadata": {
        "description": "Specify the Virtual Network Prefix for the Hub vNet to create or to be used"
    },
    "allowedValues": [
        "192.168.0.0/25",
        "192.168.32.0/25",
        "192.168.56.0/25",
        "192.168.64.0/25",
        "192.168.96.0/25",
        "192.168.120.0/25"
    ]
},
"virtualNetworkPrefixMgmt": {
    "type": "string",
    "metadata": {
        "description": "Specify the Virtual Network Prefix for the Mgmt vNet to create or to be used"
    },
    "allowedValues": [
        "192.168.0.128/25",
        "192.168.32.128/25",
        "192.168.56.128/25",
        "192.168.64.128/25",
        "192.168.96.128/25",
        "192.168.120.128/25"
    ]
},
"subnet0PrefixHub": {
    "type": "string",
    "metadata": {
        "description": "Specify the the Subnet Prefix for the wan Subnet in the Hub vNet"
    },
    "allowedValues": [
        "192.168.0.0/27",
        "192.168.32.0/27",
        "192.168.56.0/27",
        "192.168.64.0/27",
        "192.168.96.0/27",
        "192.168.120.0/27"
    ]
},
"subnet1PrefixHub": {
    "type": "string",
    "metadata": {
        "description": "Specify the the Subnet Prefix for the lan Subnet in the Hub vNet"
    },
    "allowedValues": [
        "192.168.0.32/27",
        "192.168.32.32/27",
        "192.168.56.32/27",
        "192.168.64.32/27",
        "192.168.96.32/27",
        "192.168.120.32/27"
    ]
},
"subnet0PrefixMgmt": {
    "type": "string",
    "metadata": {
        "description": "Specify the the Subnet Prefix for the adds Subnet in the Mgmt vNet"
    },
    "allowedValues": [
        "192.168.0.128/28",
        "192.168.32.128/28",
        "192.168.56.128/28",
        "192.168.64.128/28",
        "192.168.96.128/28",
        "192.168.120.128/28"
    ]
},
"subnet1PrefixMgmt": {
    "type": "string",
    "metadata": {
        "description": "Specify the the Subnet Prefix for the Build Subnet in the Mgmt vNet"
    },
    "allowedValues": [
        "192.168.0.144/28",
        "192.168.32.144/28",
        "192.168.56.144/28",
        "192.168.64.144/28",
        "192.168.96.144/28",
        "192.168.120.144/28"
    ],
    "defaultValue": ""
}

我将每个 vNet 的配置数组定义为一个复杂变量。您可以在此处为适当的 vNet 定义 DNS 服务器,例如

"vnet-settings": [
    {
        "name": "[concat('vn-hub')]",
        "dnsServers": "[variables('dnsServers')]",
        "location": "[parameters('location')]",
        "prefix": "[parameters('virtualNetworkPrefixHub')]",
        "subnets": [
            {
                "name": "oob",
                "properties": {
                    "addressPrefix": "[parameters('subnet0PrefixHub')]"
                }
            },
            {
                "name": "GatewaySubnet",
                "properties": {
                    "addressPrefix": "[parameters('subnet1PrefixHub')]"
                }
            }
        ]
    },
    {
        "name": "[concat('vn-mgmt')]",
        "dnsServers": "[variables('dnsServers')]",
        "location": "[parameters('location')]",
        "prefix": "[parameters('virtualNetworkPrefixMgmt')]",
        "subnets": [
            {
                "name": "adds",
                "properties": {
                    "addressPrefix": "[parameters('subnet0PrefixMgmt')]"
                }
            },
            {
                "name": "build",
                "properties": {
                    "addressPrefix": "[parameters('subnet1PrefixMgmt')]"
                }
            }
        ]
    }
]

虚拟网络资源如下所示:

"resources": [
    {
        "name": "[variables('vnet-settings')[copyIndex()].name]",
        "copy": {
            "name": "vnetLoop",
            "count": "[length(variables('vnet-settings'))]"
        },
        "type": "Microsoft.Network/virtualNetworks",
        "location": "[variables('vnet-settings')[copyIndex()].location]",
        "apiVersion": "2017-06-01",
        "dependsOn": [],
        "tags": {
            "displayName": "virtualNetworks"
        },
        "properties": {
            "addressSpace": {
                "addressPrefixes": [
                    "[variables('vnet-settings')[copyIndex()].prefix]"
                ]
            },
            "dhcpOptions": {
                "dnsServers": "[variables('vnet-settings')[copyIndex()].dnsServers]"
            },
            "subnets": "[variables('vnet-settings')[copyIndex()].subnets]"
        }
    }
]

希望这会有所帮助。

PT.

【讨论】:

  • 谢谢,但这不适用于我的情况。已经存在具有不同子网数量、名称和地址的虚拟网络。
  • 是的 - 我从你的帖子中明白了这一点。第二段代码允许您在一个 ARM 模板中定义多个 vNet 配置并指定所需的配置,包括不同的子网和 DNS 服务器。
  • 是的 - 我从你的帖子中明白了这一点。我提供的第二段代码允许您在一个 ARM 模板中定义多个 vNet 配置,包括不同的子网和 DNS 服务器。 ARM 模板的本质是,如果某些东西没有改变,它就会被忽略。如果某些东西发生了变化,即 DNS 服务器,那么只有 DNS 服务器会改变。在您的情况下,根据您在变量“vnet-settings”中配置的定义 vNet 配置、每个子网和 DNS 服务器,我已经完成了。部署模板,相应的 contig 将被应用/更改。
  • 您建议“根据您的配置定义 vNet 配置、每个子网和 DNS 服务器”,但正如我之前所说,定义子网不是一个选项。
猜你喜欢
  • 1970-01-01
  • 2018-01-11
  • 1970-01-01
  • 2019-11-01
  • 2022-08-10
  • 1970-01-01
  • 1970-01-01
  • 2021-11-06
  • 1970-01-01
相关资源
最近更新 更多