lifecycle_inventory.py 2.71 KB
Newer Older
e0d committed
1 2
#!/usr/bin/env python

e0d committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
"""
Build an ansible inventory based on autoscaling group instance lifecycle state.

Outputs JSON to stdout with keys for each state and combination of autoscaling
group and state.

{
  "InService": [
    "10.0.47.127",
    "10.0.46.174"
  ],
  "Terminating:Wait": [
    "10.0.48.104"
  ],
  "e-d-CommonClusterServerAsGroup": [
    "10.0.47.127",
    "10.0.46.174"
  ],
  "e-d-CommonClusterServerAsGroup_InService": [
    "10.0.47.127",
    "10.0.46.174"
  ],
  "e-d-CommonClusterServerAsGroup_InService": [
    "10.0.48.104"
  ]

}
"""
e0d committed
31 32 33
import argparse
import boto
import json
e0d committed
34
from collections import defaultdict
e0d committed
35 36 37 38 39

class LifecycleInventory():

    profile = None

e0d committed
40
    def __init__(self, profile):
e0d committed
41
        parser = argparse.ArgumentParser()
e0d committed
42
        self.profile = profile
e0d committed
43

e0d committed
44 45 46 47 48 49 50 51 52 53 54 55
    def get_e_d_from_tags(self, group):

        environment = "default_environment"
        deployment = "default_deployment"

        for r in group.tags:
            if r.key == "environment":
                environment = r.value
            elif r.key == "deployment":
                deployment = r.value
        return environment,deployment

e0d committed
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
    def get_instance_dict(self):
        ec2 = boto.connect_ec2(profile_name=self.profile)
        reservations = ec2.get_all_instances()

        dict = {}

        for instance in [i for r in reservations for i in r.instances]:
            dict[instance.id] = instance

        return dict

    def run(self):
        autoscale = boto.connect_autoscale(profile_name=self.profile)
        groups = autoscale.get_all_groups()

        instances = self.get_instance_dict()
e0d committed
72
        inventory = defaultdict(list)
e0d committed
73 74 75 76 77 78

        for group in groups:

            for instance in group.instances:

                private_ip_address = instances[instance.instance_id].private_ip_address
79
                if private_ip_address:
e0d committed
80 81
                    environment,deployment = self.get_e_d_from_tags(group)
                    inventory[environment + "_" + deployment + "_" + instance.lifecycle_state.replace(":","_")].append(private_ip_address)
82
                    inventory[group.name].append(private_ip_address)
e0d committed
83
                    inventory[group.name + "_" + instance.lifecycle_state.replace(":","_")].append(private_ip_address)
84
                    inventory[instance.lifecycle_state.replace(":","_")].append(private_ip_address)
e0d committed
85 86 87

        print json.dumps(inventory, sort_keys=True, indent=2)

e0d committed
88 89 90 91
if __name__=="__main__":

    parser = argparse.ArgumentParser()
    parser.add_argument('-p', '--profile', help='The aws profile to use when connecting.')
e0d committed
92
    parser.add_argument('-l', '--list', help='Ansible passes this, we ignore it.', action='store_true', default=True)
e0d committed
93
    args = parser.parse_args()
94

e0d committed
95 96 97
    LifecycleInventory(args.profile).run()


98