Ansible AWS create dict of count of instance tags

2019-08-29 05:43发布

问题:

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

        ]
    }
}

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

回答1:

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