如何使用JMESPath和Ansible正确过滤列表中的元素?

时间:2018-01-17 13:56:44

标签: json ansible filtering jmespath

我有以下结构。

[
            {
                "ami_launch_index": 0, 
                "architecture": "x86_64",                     
                "instance_type": "t2.micro", 
                "monitoring": {
                    "state": "disabled"
                } ,
                "placement": {
                    "availability_zone": "eu-central-1a", 
                    "group_name": "", 
                    "tenancy": "default"
                },                 
                "tags": {
                    "Name": "MYNAME 1", 
                    "Project": "MY PROJECT", 
                    "Purpose": "MYTEST"
                }, 
                "virtualization_type": "hvm"
            }, 
            {
                "ami_launch_index": 2, 
                "architecture": "x86_64",              
                "instance_type": "t2.micro", 
                "monitoring": {
                    "state": "disabled"
                }, 
                "placement": {
                    "availability_zone": "eu-central-1a", 
                    "group_name": "", 
                    "tenancy": "default"
                },
                "tags": {
                    "Name": "MYNAME 2", 
                    "Project": "MY SECOND PROJECT", 
                    "Purpose": "WHY THE HECK NOT"
                }, 
                "virtualization_type": "hvm"
            }
        ]

我想打印所有元素,其中[] .tags.Name包含字符串" SECOND" (所以只是第二个元素。我如何实现这个目标?

这是我尝试过的,但它没有用。

- debug:
        msg: "{{ instances.stdout | from_json | json_query(\"[][?contains(tags.Name, 'SECOND')]\") }}"

我尝试使用http://jmespath.org,但无法按照我想要的方式提取数据。

1 个答案:

答案 0 :(得分:1)

要获取<input type="text" [ngClass]="{red: sampledetails.rules[0].query3.dirty}" class="form-control" name="query3" id="query3" [(ngModel)]="sampledetails.rules[0].query3" > SECOND项的内容,您可以使用此查询:

tags.Name

但似乎您的示例数据与任何内容都不匹配,因为[?contains(tags.Name,'SECOND')] 仅在SECOND属性而非Project