我正在和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查询过滤器中提取破折号?
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"'