Ansible AWS 创建实例标签计数的字典 [英] Ansible AWS create dict of count of instance tags
问题描述
我有多个 EC2 实例,需要使用 Ansible 为其收集事实.根据这些事实,我需要使用以下格式创建具有特定标签的实例计数字典:
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}
例如,角色"标签将如下所示:
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屋!