这是一个string/json/ansible解析问题。
大图:我正在创建一个VPC,添加一些安全组并创建一些VM。
下面的示例可以很好地处理如下所示的输出。但是,我需要标记新创建的安全组ID,以便可以使用它们来创建VM。
- name: Create an VPC and some security groups and show the security group ID
hosts: localhost
vars:
security_groups: []
aws_region: "us-east-1"
tasks:
- name: "Create VPC test in AWS"
amazon.aws.ec2_vpc_net:
name: "test_vpc"
cidr_block: "10.5.2.0/24"
state: present
region: "us-east-1"
register: create_vpc_output
- name: "Set VPC ID as fact"
ansible.builtin.set_fact:
vpc_id: "{{ create_vpc_output.vpc.id }}"
- name: "Configuring security groups for VPC {{ vpc_id }}"
amazon.aws.ec2_security_group:
name: "{{ item.value.name }}"
description: "{{ item.value.description }}"
tags:
Name: "{{ item.value.name }}"
state: present
vpc_id: "{{ vpc_id }}"
region: "{{ aws_region }}"
loop: "{{ query('dict', vpc_security_groups) }}"
register: "create_sg_output"
vars:
vpc_security_groups:
private_test1:
name: "test1"
description: "Allow all connections"
private_test2:
name: "test2"
description: "Allow all connections"
- name: Extract group_id and group_name
set_fact:
sg_groups: "{{ create_sg_output.results | map(attribute='group_id') | zip(create_sg_output.results | map(attribute='group_name')) }}"
- name: Display extracted groups
debug:
var: sg_groups
结果如下:
TASK [Display extracted groups] ****************************************************************************************
ok: [localhost] => {
"sg_groups": [
[
"sg-08xxxxxxxxxxxxxxxx",
"test1"
],
[
"sg-05xxxxxxxxxxxxxxxx",
"test2"
]
]
}
我如何调整它,使输出像这样:
"sg_groups": [
{
"group_id" : "sg-08xxxxxxxxxxxxxx",
"group_name" : "test1"
},
{
"group_id": "sg-05xxxxxxxxxxxxxx",
"group_name" : "test2"
}
]
}
注意添加的“group_id”和“group_name”,这是从ec2_security_group调用返回的json中数据的标签。
1条答案
按热度按时间yjghlzjz1#
还有更多的选择:
给你想要的
此选项允许您重命名属性。详情请参见JMESPath Specification。
用于测试的完整剧本示例
给字典
你可以把这本字典翻过来
gives(abridged)
如果你想将字典转换为列表,请使用过滤器 dict2items
给出了预期的结果
用于测试的完整剧本示例