Ansible AWS创建实例标签计数的字典

时间:2018-10-04 21:20:12

标签: amazon-web-services amazon-ec2 ansible

我有多个需要使用Ansible收集事实的EC2实例。基于这些事实,我需要创建一个具有特定标记的实例数字典,格式为:

{tag.value: count-of-instances-with-that-tag.value}

例如,“ Role”标签如下所示:

{"role1": 3,
"role2": 11,
"role3": 5}

我对事实收集部分有很好的了解:

- 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

以下是上述模块的输出示例:

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来获取任何东西。 到目前为止,这是我的任务:

- 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 }}"

1 个答案:

答案 0 :(得分:1)

假设您的数据正确(您发布的数据不正确),则可以通过以下方式进行操作:

- set_fact:
    counted_instances: "{{ counted_instances | default({}) | combine({item.0: item.1|length}) }}"
  loop: "{{ existing_instance_facts.instances|groupby('tags.Role') }}"