我和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查询过滤器中提取短划线?
答案 0 :(得分:3)
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"'