Reputation: 29
I have several EC2 instances that I need to extract the IP addresses from and group them by a specific instance tag.
So this sample JSON:
{
"changed": true,
"instances": [
{
"ami_launch_index": 0,
"architecture": "x86_64",
"private_ip_address": "11.111.1.111",
"public_dns_name": "",
"tags": {
"Environment": "dev",
"Role": "role1",
}
},
{
"ami_launch_index": 0,
"architecture": "x86_64",
"private_ip_address": "22.222.2.222",
"public_dns_name": "",
"tags": {
"Environment": "dev",
"Role": "role1",
}
},
{
"ami_launch_index": 0,
"architecture": "x86_64",
"private_ip_address": "33.333.3.333",
"public_dns_name": "",
"tags": {
"Environment": "dev",
"Role": "role2",
}
},
{
"ami_launch_index": 0,
"architecture": "x86_64",
"private_ip_address": "44.444.4.444",
"public_dns_name": "",
"tags": {
"Environment": "dev",
"Role": "role2",
}
}
]
}
would yield the following:
{
"role1": [
"11.111.1.111",
"22.222.2.222"
],
"role2": [
"33.333.3.333",
"44.444.4.444"
]
}
Getting the instance information from AWS is no problem, but I'm struggling with creating the dictionary that contains the grouped lists of IP addresses. I have tried several variations of the solution offered in this similar issue, but have not had any luck.
Upvotes: 0
Views: 79
Reputation: 68559
Here you are:
- set_fact:
my_result: "{{ my_result | default({}) | combine({item.0: item.1|map(attribute='private_ip_address')|list}) }}"
loop: "{{ my_input.instances|groupby('tags.Role') }}"
Upvotes: 1