代码之家  ›  专栏  ›  技术社区  ›  Nick Miller

Ansible AWS创建实例标记计数的dict

  •  0
  • Nick Miller  · 技术社区  · 6 年前

    我有多个EC2实例,我需要使用Ansible来收集事实。根据这些事实,我需要创建一个实例计数的dict,其格式为:

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

    {"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 回复  |  直到 6 年前
        1
  •  1
  •   techraf    6 年前

    假设您的数据是正确的(您发布的不是),您可以这样做,例如:

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