使用Ansible中的json_query / jmespath过滤器进行多次搜索

时间:2018-02-13 12:47:07

标签: ansible jmespath

我试图在json_query中使用contains_with或starts_with过滤器来解析下一段json中的特定子网名称。 它包含两个vnet,每个vnet都有多个子网:

{
  "azure_virtualnetworks": [
    {
      "name": "test-vnet-172-17-0-0-19", 
      "properties": {
        "subnets": [
            {
              "name": "test-confluent-subnet-172-17-0-0-28", 
              "properties": {
                  "addressPrefix": "172.20.88.0/28", 
                  "networkSecurityGroup": {
                      "id": "/subscriptions/********/resourceGroups/test-confluent-rg/providers/Microsoft.Network/networkSecurityGroups/test-confluent-nsg"
                  }, 
                  "provisioningState": "Succeeded"
              }
            }, 
            {
              "name": "test-test-subnet-172-17-0-32-28", 
              "properties": {
                  "addressPrefix": "172.20.88.32/28", 
                  "networkSecurityGroup": {
                      "id": "/subscriptions/********/resourceGroups/test-test-rg/providers/Microsoft.Network/networkSecurityGroups/test-test-nsg"
                  }, 
                  "provisioningState": "Succeeded"
              }
            }
        ]
      } 
    },
    {
      "name": "test2-vnet-172-17-1-0-19", 
      "properties": {
        "subnets": [
            {
              "name": "test-confluent-subnet-172-17-1-0-28", 
              "properties": {
                  "addressPrefix": "172.20.88.0/28", 
                  "networkSecurityGroup": {
                      "id": "/subscriptions/********/resourceGroups/test-confluent-rg/providers/Microsoft.Network/networkSecurityGroups/test-confluent-nsg"
                  }, 
                  "provisioningState": "Succeeded"
              }
            }, 
            {
              "name": "test-qatesting-subnet-172-17-1-16-28", 
              "properties": {
                  "addressPrefix": "172.20.88.16/28", 
                  "networkSecurityGroup": {
                      "id": "/subscriptions/********/resourceGroups/test-qatesting-rg/providers/Microsoft.Network/networkSecurityGroups/test-qatesting-nsg"
                  }, 
                  "provisioningState": "Succeeded"
              }
            }
        ]
      }
    }
  ]
}

我需要在按虚拟网络名称搜索后搜索子网名称。

我可以毫无问题地过滤到子网列表。例如

azure_virtualnetworks[?contains(name,`test2-vnet`)].properties.subnets[]

返回:

 [
   {
     "name": "test-confluent-subnet-172-17-1-0-28",
     "properties": {
       "addressPrefix": "172.20.88.0/28",
       "networkSecurityGroup": {
         "id": "/subscriptions/********/resourceGroups/test-confluent-rg/providers/Microsoft.Network/networkSecurityGroups/test-confluent-nsg"
       },
       "provisioningState": "Succeeded"
     }
   },
   {
     "name": "test-qatesting-subnet-172-17-1-16-28",
     "properties": {
       "addressPrefix": "172.20.88.16/28",
       "networkSecurityGroup": {
         "id": "/subscriptions/********/resourceGroups/test-qatesting-rg/providers/Microsoft.Network/networkSecurityGroups/test-qatesting-nsg"
       },
       "provisioningState": "Succeeded"
     }
   }
 ]

然而,我在搜索子网时遇到了问题。我曾经认为以下的一些变化可能有用,但是没有任何成功:

azure_virtualnetworks[?contains(name,`test2-vnet`)].properties.subnets[?contains(name,`test-confluent`) ]

我正在努力弄清楚纠正语法是什么。

1 个答案:

答案 0 :(得分:2)

选择所需的子网,使用pipe表达式停止投影,从子网列表中筛选所需的项目:

azure_virtualnetworks[?contains(name,`test2-vnet`)].properties.subnets[] | [?contains(name,`test-confluent`)]