1

我正在与 Ansible 合作!我有一个很好的数据结构返回

- name: list ec2 instances
  ec2_instance_facts:
    region: '{{aws_primary_region}}'
    filters:
      "tag:organization-tag-1": "specific-value"
  register: instances

我想转换这些结果以提取org-specific-tag2. 我可以做类似的事情

- name: extract instance tags
  set_fact:
    instance_tags: '{{instances | json_query(query)}}'
  vars:
    query: "instances[*].tags"

将标签作为数据结构获取:

        {
            "Name": "box-1138", 
            "organization-tag-1": "specific-value", 
            "organization-tag-2": "value being queried"
        }, 
        {
            "Name": "box-999", 
            "organization-tag-1": "specific-value", 
            "organization-tag-2": "value being queried 2"
        }

我可以使用它with_items并访问item["organization-tag-2"]. 这不是问题。["value being queried", "value being queried 2"]但是,对我来说,将列表设置为开始会更加优雅。我只是不知道是怎么回事,因为标签中有一个破折号,而且 JMESPath 文档上的语法指南......非常不透明。

如何在 JSON 查询过滤器中提取破折号?

4

1 回答 1

4

看一下JMESPath 语法

sub-expression    = expression "." ( identifier /
                                     multi-select-list /
                                     multi-select-hash /
                                     function-expression /
                                     "*" )
identifier        = unquoted-string / quoted-string
quoted-string     = quote 1*(unescaped-char / escaped-char) quote
quote             = %x22   ; Double quote: '"'

所以你应该没问题:

- name: extract instance tags
  set_fact:
    instance_tags: '{{instances | json_query(query)}}'
  vars:
    query: 'instances[*].tags."organization-tag-2"'
于 2018-03-01T18:04:11.583 回答