Ansible AWS创建实例标签计数的字典 [英] Ansible AWS create dict of count of instance tags
问题描述
我有多个需要使用Ansible收集事实的EC2实例.根据这些事实,我需要创建一个具有特定标记的实例数字典,格式为:
I have multiple EC2 instances that I need to use Ansible to gather facts for. From those facts, I need to create a dict of counts of instances with specific tags in the format:
{tag.value: count-of-instances-with-that-tag.value}
例如,"Role"标签将如下所示:
So the "Role" tag, for example, will look like:
{"role1": 3,
"role2": 11,
"role3": 5}
我对事实收集部分有很好的了解:
I have a decent understanding of the fact gathering portion:
- name: Get existing instance facts
ec2_instance_facts:
region: "{{ aws_region }}"
filters:
instance-state-name: [pending, running, stopping, stopped]
"tag:Stack": "{{ stack }}"
vpc-id: "{{ vpc_id }}"
register: existing_instance_facts
以下是上述模块的简短示例输出:
Here is an abbreviated sample output from the above module:
TASK [vpc.ec2 : Output existing_instance_facts] **********************************
ok: [localhost] => {
"msg": {
"changed": false,
"failed": false,
"instances": [
{
"architecture": "x86_64",
"ebs_optimized": false,
"image_id": "ami-xxxxxxxxxxx",
"instance_id": "i-xxxxxxxxxxx",
"instance_type": "t2.micro",
"key_name": "some_key",
"launch_time": "2018-04-04T20:19:55+00:00"
},
"private_dns_name": "ip-xx-xxx-xx-xx.us-west-2.compute.internal",
"private_ip_address": "xx.xxx.xx.xx",
"subnet_id": "subnet-xxxxxxxxxxx",
"tags": {
"Environment": "dev",
"Role": "role1",
"Stack": ""
},
"vpc_id": "vpc-xxxxxxxxxxxxxx"
},
{
"architecture": "x86_64",
"ebs_optimized": false,
"image_id": "ami-xxxxxxxxxxx",
"instance_id": "i-xxxxxxxxxxx",
"instance_type": "t2.micro",
"key_name": "some_key",
"launch_time": "2018-04-04T20:19:55+00:00"
},
"private_dns_name": "ip-xx-xxx-xx-xx.us-west-2.compute.internal",
"private_ip_address": "xx.xxx.xx.xx",
"subnet_id": "subnet-xxxxxxxxxxx",
"tags": {
"Environment": "dev",
"Role": "role1",
"Stack": ""
},
"vpc_id": "vpc-xxxxxxxxxxxxx"
},
{
"architecture": "x86_64",
"ebs_optimized": false,
"image_id": "ami-xxxxxxxxxxx",
"instance_id": "i-xxxxxxxxxxx",
"instance_type": "t2.micro",
"key_name": "some_key",
"launch_time": "2018-04-04T20:19:55+00:00"
},
"private_dns_name": "ip-xx-xxx-xx-xx.us-west-2.compute.internal",
"private_ip_address": "xx.xxx.xx.xx",
"subnet_id": "subnet-xxxxxxxxxxx",
"tags": {
"Environment": "dev",
"Role": "role2",
"Stack": ""
},
"vpc_id": "vpc-xxxxxxxxxxxxx"
}
]
}
}
我遇到的问题是我无法弄清楚如何使用ansible来获取任何东西. 到目前为止,这是我的任务:
The issue I'm having is that I can't figure out how to use ansible to get a count of, well, anything. Here is my task so far:
- name: "Set fact: count of existing instances by role"
set_fact:
count_of_tags: "{{ count_of_tags | default({}) | combine({??item.tags.Role.value?? : ??count-of-instances-with-that-tag.value?? }) }}"
loop: "{{ existing_instance_facts.instances }}"
推荐答案
假设您的数据正确(您发布的数据不正确),则可以通过以下方式进行操作:
Assuming your data was correct (the one you posted is not), you can do it for example this way:
- set_fact:
counted_instances: "{{ counted_instances | default({}) | combine({item.0: item.1|length}) }}"
loop: "{{ existing_instance_facts.instances|groupby('tags.Role') }}"
这篇关于Ansible AWS创建实例标签计数的字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!