json - Azure Bicep/Json - 无法正确解析 JSON

标签 json azure azure-resource-manager infrastructure-as-code azure-bicep

我在从 JSON 文件解析网络安全组时遇到问题,因此我们可以使用单个模板创建多个 NSG

param NSGs array = array(json(loadTextContent('./shared-rules.json')))
resource nsg 'Microsoft.Network/networkSecurityGroups@2020-05-01' = [for (ns, index) in NSGs: {
  name: ?????
  location: resourceGroup().location
  properties: {
    securityRules: ????
  }
}]

这是我的模板文件的副本:

{
  "NSG-1": [
    {
      "name": "Allow_RDP_from_company_IP_address",
      "properties": {
        "description": "Allow inbound RDP from the company's IP address range.",
        "protocol": "Tcp",
        "sourceAddressPrefix": "203.0.113.0/24",
        "sourcePortRange": "*",
        "destinationAddressPrefix": "VirtualNetwork",
        "destinationPortRange": "3389",
        "access": "Allow",
        "priority": 100,
        "direction": "Inbound"
      }
    },
    {
      "name": "Allow_VirtualNetwork_to_Storage",
      "properties": {
        "description": "Allow outbound connections to the Azure Storage service tag.",
        "protocol": "Tcp",
        "sourceAddressPrefix": "VirtualNetwork",
        "sourcePortRange": "*",
        "destinationAddressPrefix": "Storage",
        "destinationPortRange": "*",
        "access": "Allow",
        "priority": 100,
        "direction": "Outbound"
      }
    }
  ],
  "NSG-2": [
    {
      "name": "Allow_RDP_from_company_IP_address",
      "properties": {
        "description": "Allow inbound RDP from the company's IP address range.",
        "protocol": "Tcp",
        "sourceAddressPrefix": "203.0.113.0/24",
        "sourcePortRange": "*",
        "destinationAddressPrefix": "VirtualNetwork",
        "destinationPortRange": "3389",
        "access": "Allow",
        "priority": 100,
        "direction": "Inbound"
      }
    },
    {
      "name": "Allow_VirtualNetwork_to_Storage",
      "properties": {
        "description": "Allow outbound connections to the Azure Storage service tag.",
        "protocol": "Tcp",
        "sourceAddressPrefix": "VirtualNetwork",
        "sourcePortRange": "*",
        "destinationAddressPrefix": "Storage",
        "destinationPortRange": "*",
        "access": "Allow",
        "priority": 100,
        "direction": "Outbound"
      }
    }
  ]
}

我认为只需填写两行即可使其正常工作:即二头肌模板中的名称和 securityRules((第一个代码块)

假设我的 JSON 文件也是正确的 - 想知道是否有人有任何想法?

最佳答案

正如已经建议的,您可以使用 items function .

可以在此处找到有关如何使用它的示例:Dictionary object .

对于您的用例,它应该如下所示:

param sharedRules object = json(loadTextContent('./shared-rules.json'))

resource nsgs 'Microsoft.Network/networkSecurityGroups@2020-05-01' = [for rule in items(sharedRules): {
  name: rule.key
  location: resourceGroup().location
  properties: {
    securityRules: rule.value
  }
}]

关于json - Azure Bicep/Json - 无法正确解析 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71259586/

相关文章:

android - 如何在 android 中使用身份验证连接到 Web 服务 JSON?

Azure 应用服务

azure - 从二头肌中的 Azure 应用服务访问应用程序(客户端)ID

azure - 从脚本获取 AKS 节点资源组中的 Azure 资源

aws-cloudformation - 模板(即基础设施即代码)相对于 API 调用的优势

javascript - PHP 数组到 Javascript 对象在 console.log 之后没有数组

java - Liferay + JSON + AJAX

json - 可以使用 GHCI 中的 json 包解析 JSON,但使用 GHC 编译时不行

azure - 使用 Symfony 的 Microsoft Azure 应用服务的性能问题

.net - 如何使用其他租户的 Azure AD 应用程序?