Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Json Azure ARM内联(如果不工作)_Json_Azure_Templates_Vnet - Fatal编程技术网

Json Azure ARM内联(如果不工作)

Json Azure ARM内联(如果不工作),json,azure,templates,vnet,Json,Azure,Templates,Vnet,我正在尝试通过.json模板构建Azure VNET 我试图使用内联条件语句创建第二个子网或跳过创建第二个子网。我认为我没有正确地使用json('null'),或者如果这是可能的话。我的理解是,如果选择了json('null'),则不选择任何内容 感谢您的帮助 "apiVersion": "2016-06-01", "type": "Microsoft.Network/virtualNetworks", "name": "My-VNET", "location": "[resourceGroup

我正在尝试通过.json模板构建Azure VNET

我试图使用内联条件语句创建第二个子网或跳过创建第二个子网。我认为我没有正确地使用json('null'),或者如果这是可能的话。我的理解是,如果选择了json('null'),则不选择任何内容

感谢您的帮助

"apiVersion": "2016-06-01",
"type": "Microsoft.Network/virtualNetworks",
"name": "My-VNET",
"location": "[resourceGroup().location]",
"properties": {
    "addressSpace": {
        "addressPrefixes": [
            "[parameters('virtualNetworkCIDR')]"
        ]
    },
    "subnets": [{
            "name": "[parameters('firstSubnetName')]",
            "properties": {
                "addressPrefix": "10.10.1.0/24"
            }
        }, {
            "name": "[if(equals(parameters('createSecondSubnet'), 'Yes'), parameters('secondSubnetName'), json('null'))]",
            "properties": {
                "addressPrefix": "10.10.2.0/24"
            }
        }
    ]
}

通常,要在模板中有条件地创建资源,可以使用“Condition”属性:

如果要创建多个单一类型的资源,可以使用“复制”属性:

不幸的是,子网没有条件或复制属性,因为它们是“虚拟网络”资源的子资源。因此,整个VNET需要是有条件的,您可以指定多个VNET,只部署其中的一个。VNET也不能具有相同的名称,因此需要在模板中指定多个VNET

例如:

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
    "NumberofSubnets": {
      "type": "string",
      "allowedValues": ["1","2"],
      "metadata": {
        "description": "would you like to deploy 1 or 2 subnets?"
      }
    }
    },
    "resources": [
        {
        "type": "Microsoft.Network/virtualNetworks",
        "apiVersion": "2016-06-01",
        "name": "My-VNET1",
        "location": "[resourceGroup().location]",
        "condition": "[equals(parameters('NumberofSubnets'), 1)]",
        "properties": {
            "addressSpace": {
            "addressPrefixes": ["10.10.0.0/23"]
            },
    "subnets": [{
            "name": "Subnet1",
            "properties": {
                "addressPrefix": "10.10.0.0/24"
            }
        }
        ]
        }
        },
        {
        "type": "Microsoft.Network/virtualNetworks",
        "apiVersion": "2016-06-01",
        "name": "My-VNET2",
        "location": "[resourceGroup().location]",
        "condition": "[equals(parameters('NumberofSubnets'), 2)]",
        "properties": {
            "addressSpace": {
            "addressPrefixes": ["10.10.0.0/23"]
            },
    "subnets": [{
            "name": "Subnet1",
            "properties": {
                "addressPrefix": "10.10.0.0/24"
            }
        },
        {
            "name": "Subnet2",
            "properties": {
                "addressPrefix": "10.10.1.0/24"
            }
        }
        ]
        }
        }
    ]
}
这将解决您的问题,但是如果有大量的子网,您可以看到模板是如何变得非常乏味的


最好但最复杂的方法是使用链接模板

通常,要在模板中有条件地创建资源,可以使用“Condition”属性:

如果要创建多个单一类型的资源,可以使用“复制”属性:

不幸的是,子网没有条件或复制属性,因为它们是“虚拟网络”资源的子资源。因此,整个VNET需要是有条件的,您可以指定多个VNET,只部署其中的一个。VNET也不能具有相同的名称,因此需要在模板中指定多个VNET

例如:

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
    "NumberofSubnets": {
      "type": "string",
      "allowedValues": ["1","2"],
      "metadata": {
        "description": "would you like to deploy 1 or 2 subnets?"
      }
    }
    },
    "resources": [
        {
        "type": "Microsoft.Network/virtualNetworks",
        "apiVersion": "2016-06-01",
        "name": "My-VNET1",
        "location": "[resourceGroup().location]",
        "condition": "[equals(parameters('NumberofSubnets'), 1)]",
        "properties": {
            "addressSpace": {
            "addressPrefixes": ["10.10.0.0/23"]
            },
    "subnets": [{
            "name": "Subnet1",
            "properties": {
                "addressPrefix": "10.10.0.0/24"
            }
        }
        ]
        }
        },
        {
        "type": "Microsoft.Network/virtualNetworks",
        "apiVersion": "2016-06-01",
        "name": "My-VNET2",
        "location": "[resourceGroup().location]",
        "condition": "[equals(parameters('NumberofSubnets'), 2)]",
        "properties": {
            "addressSpace": {
            "addressPrefixes": ["10.10.0.0/23"]
            },
    "subnets": [{
            "name": "Subnet1",
            "properties": {
                "addressPrefix": "10.10.0.0/24"
            }
        },
        {
            "name": "Subnet2",
            "properties": {
                "addressPrefix": "10.10.1.0/24"
            }
        }
        ]
        }
        }
    ]
}
这将解决您的问题,但是如果有大量的子网,您可以看到模板是如何变得非常乏味的


最好但最复杂的方法是使用链接模板

我敢肯定,您前一段时间设法找到了某种解决方案。。。然而,我有一个解决方案,对这类事情很有效。。。但它不使用条件语句

在PowerShell中创建一些哈希表,如下所示

# Resource group Hashtables.
$rgDev = @{
    Name = "DEV-RG"
    SubscriptionId = $subNonProd
    Location = "desiredregion"
}
$rgUat = @{
    Name = "UAT-RG"
    SubscriptionId = $subNonProd
    Location = "desiredregion"
}

#Vnet Hashtables
$vnetDev = @{
    ResourceGroup = $rgDev
    VnetName = "Dev-vnet"
    CIDR = @('x.x.x.x/27')
    Subnets = @(
             @{
                Name = "Dev-Web-subnet"
                CIDR = "y.y.y.y/28"
             },
             @{
                Name = "Dev-DB-subnet"
                CIDR = "z.z.z.z/28"
             })
}
$vnetUat = @{
    ResourceGroup = $rgUat
    VnetName = "UAT-vnet"
    CIDR = @('f.f.f.f/27')
    Subnets = @(
             @{
                Name = "UAT-Web-subnet"
                CIDR = "g.g.g.g/28"
             },
             @{
                Name = "UAT-DB-subnet"
                CIDR = "h.h.h.h/28"
             })
}

接下来,我将哈希表合并到一个数组中,并对该批进行foreach。我有一个切换上下文的小脚本,这样我就可以在一个引导类型的脚本中部署到多个订阅

$vnets = @($vnetDev, $vnetUat)

ForEach ($vn in $vnets) {

    $deploymentName = $vn.VnetName + "_Deployment."

    .\SwitchSubscription.ps1 -subName $vn.ResourceGroup.SubscriptionName -subId $vn.ResourceGroup.SubscriptionId

    New-AzureRmResourceGroupDeployment  -Name $deploymentName `
                                        -ResourceGroupName $vn.ResourceGroup.Name `
                                        -TemplateFile .\JSONFiles\Vnets.json `
                                        -vnet $vn

}

ARM模板的参数部分如下所示

    "parameters": {        
        "vnet": {
            "type": "object",
        }
    },
        {
            "name": "[concat(parameters('vnet').VnetName)]",
            "type": "Microsoft.Network/virtualNetworks",
            "apiVersion": "2017-10-01",
            "location": "[resourceGroup().location]",
            "tags": "[parameters('vnet').tags]",
            "properties": {
                "addressSpace": {
                    "addressPrefixes": "[parameters('vnet').CIDR]"
                },
                "copy": [
                    {
                        "name": "subnets",
                        "count": "[length(parameters('vnet').Subnets)]",
                        "input": {
                            "name": "[parameters('vnet').Subnets[copyIndex('Subnets')].Name]",
                            "properties": {
                                "addressPrefix": "[parameters('vnet').Subnets[copyIndex('Subnets')].CIDR]"
                            }
                        }
                    }
                ]
            }
        }
    ]
然后资源部分看起来像这样

    "parameters": {        
        "vnet": {
            "type": "object",
        }
    },
        {
            "name": "[concat(parameters('vnet').VnetName)]",
            "type": "Microsoft.Network/virtualNetworks",
            "apiVersion": "2017-10-01",
            "location": "[resourceGroup().location]",
            "tags": "[parameters('vnet').tags]",
            "properties": {
                "addressSpace": {
                    "addressPrefixes": "[parameters('vnet').CIDR]"
                },
                "copy": [
                    {
                        "name": "subnets",
                        "count": "[length(parameters('vnet').Subnets)]",
                        "input": {
                            "name": "[parameters('vnet').Subnets[copyIndex('Subnets')].Name]",
                            "properties": {
                                "addressPrefix": "[parameters('vnet').Subnets[copyIndex('Subnets')].CIDR]"
                            }
                        }
                    }
                ]
            }
        }
    ]
因此,这一切所做的就是将一个对象传递给ARM模板,该模板可以有一个Vnet和一个或多个子网,并创建它们

希望这能帮助其他人在谷歌搜索时发现这一点

干杯

戴夫


:)

我肯定你在不久前就设法找到了解决办法。。。然而,我有一个解决方案,对这类事情很有效。。。但它不使用条件语句

在PowerShell中创建一些哈希表,如下所示

# Resource group Hashtables.
$rgDev = @{
    Name = "DEV-RG"
    SubscriptionId = $subNonProd
    Location = "desiredregion"
}
$rgUat = @{
    Name = "UAT-RG"
    SubscriptionId = $subNonProd
    Location = "desiredregion"
}

#Vnet Hashtables
$vnetDev = @{
    ResourceGroup = $rgDev
    VnetName = "Dev-vnet"
    CIDR = @('x.x.x.x/27')
    Subnets = @(
             @{
                Name = "Dev-Web-subnet"
                CIDR = "y.y.y.y/28"
             },
             @{
                Name = "Dev-DB-subnet"
                CIDR = "z.z.z.z/28"
             })
}
$vnetUat = @{
    ResourceGroup = $rgUat
    VnetName = "UAT-vnet"
    CIDR = @('f.f.f.f/27')
    Subnets = @(
             @{
                Name = "UAT-Web-subnet"
                CIDR = "g.g.g.g/28"
             },
             @{
                Name = "UAT-DB-subnet"
                CIDR = "h.h.h.h/28"
             })
}

接下来,我将哈希表合并到一个数组中,并对该批进行foreach。我有一个切换上下文的小脚本,这样我就可以在一个引导类型的脚本中部署到多个订阅

$vnets = @($vnetDev, $vnetUat)

ForEach ($vn in $vnets) {

    $deploymentName = $vn.VnetName + "_Deployment."

    .\SwitchSubscription.ps1 -subName $vn.ResourceGroup.SubscriptionName -subId $vn.ResourceGroup.SubscriptionId

    New-AzureRmResourceGroupDeployment  -Name $deploymentName `
                                        -ResourceGroupName $vn.ResourceGroup.Name `
                                        -TemplateFile .\JSONFiles\Vnets.json `
                                        -vnet $vn

}

ARM模板的参数部分如下所示

    "parameters": {        
        "vnet": {
            "type": "object",
        }
    },
        {
            "name": "[concat(parameters('vnet').VnetName)]",
            "type": "Microsoft.Network/virtualNetworks",
            "apiVersion": "2017-10-01",
            "location": "[resourceGroup().location]",
            "tags": "[parameters('vnet').tags]",
            "properties": {
                "addressSpace": {
                    "addressPrefixes": "[parameters('vnet').CIDR]"
                },
                "copy": [
                    {
                        "name": "subnets",
                        "count": "[length(parameters('vnet').Subnets)]",
                        "input": {
                            "name": "[parameters('vnet').Subnets[copyIndex('Subnets')].Name]",
                            "properties": {
                                "addressPrefix": "[parameters('vnet').Subnets[copyIndex('Subnets')].CIDR]"
                            }
                        }
                    }
                ]
            }
        }
    ]
然后资源部分看起来像这样

    "parameters": {        
        "vnet": {
            "type": "object",
        }
    },
        {
            "name": "[concat(parameters('vnet').VnetName)]",
            "type": "Microsoft.Network/virtualNetworks",
            "apiVersion": "2017-10-01",
            "location": "[resourceGroup().location]",
            "tags": "[parameters('vnet').tags]",
            "properties": {
                "addressSpace": {
                    "addressPrefixes": "[parameters('vnet').CIDR]"
                },
                "copy": [
                    {
                        "name": "subnets",
                        "count": "[length(parameters('vnet').Subnets)]",
                        "input": {
                            "name": "[parameters('vnet').Subnets[copyIndex('Subnets')].Name]",
                            "properties": {
                                "addressPrefix": "[parameters('vnet').Subnets[copyIndex('Subnets')].CIDR]"
                            }
                        }
                    }
                ]
            }
        }
    ]
因此,这一切所做的就是将一个对象传递给ARM模板,该模板可以有一个Vnet和一个或多个子网,并创建它们

希望这能帮助其他人在谷歌搜索时发现这一点

干杯

戴夫

:)