From 3121e352f7dc5514be345310dbba457efb01fa47 Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Fri, 3 Jun 2016 15:13:13 -0400 Subject: [PATCH] fixed group assignment in DO inventory (#16102) * fixed group assignment in DO inventory * changed sets to lists as we are appending dicts * deeper fix as original data structure was flawed --- contrib/inventory/digital_ocean.py | 47 ++++++++++++++++++++---------- 1 file changed, 32 insertions(+), 15 deletions(-) diff --git a/contrib/inventory/digital_ocean.py b/contrib/inventory/digital_ocean.py index 8eeeba8de6..78d18564e3 100755 --- a/contrib/inventory/digital_ocean.py +++ b/contrib/inventory/digital_ocean.py @@ -145,7 +145,7 @@ except ImportError: import simplejson as json try: - from dopy.manager import DoError, DoManager + from dopy.manager import DoManager except ImportError as e: print("failed=True msg='`dopy` library required for this script'") sys.exit(1) @@ -351,7 +351,13 @@ or environment variables (DO_API_TOKEN)''') def build_inventory(self): '''Build Ansible inventory of droplets''' - self.inventory = {} + self.inventory = { + 'all': { + 'hosts': [], + 'vars': self.group_variables + }, + '_meta': {'hostvars': {}} + } # add all droplets by id and name for droplet in self.data['droplets']: @@ -365,24 +371,35 @@ or environment variables (DO_API_TOKEN)''') else: dest = droplet['ip_address'] - dest = { 'hosts': [ dest ], 'vars': self.group_variables } + self.inventory['all']['hosts'].append(dest) self.inventory[droplet['id']] = dest self.inventory[droplet['name']] = dest - self.inventory['region_' + droplet['region']['slug']] = dest - self.inventory['image_' + str(droplet['image']['id'])] = dest - self.inventory['size_' + droplet['size']['slug']] = dest - image_slug = droplet['image']['slug'] - if image_slug: - self.inventory['image_' + self.to_safe(image_slug)] = dest - else: - image_name = droplet['image']['name'] - if image_name: - self.inventory['image_' + self.to_safe(image_name)] = dest + # groups that are always present + for group in [ + 'region_' + droplet['region']['slug'], + 'image_' + str(droplet['image']['id']), + 'size_' + droplet['size']['slug'], + 'distro_' + self.to_safe(droplet['image']['distribution']), + 'status_' + droplet['status'], + + ]: + if group not in self.inventory: + self.inventory[group] = { 'hosts': [ ], 'vars': {} } + self.inventory[group]['hosts'].append(dest) + + # groups that are not always present + for group in [ + droplet['image']['slug'], + droplet['image']['name'] + ]: + if group: + image = 'image_' + self.to_safe(group) + if image not in self.inventory: + self.inventory[image] = { 'hosts': [ ], 'vars': {} } + self.inventory[image]['hosts'].append(dest) - self.inventory['distro_' + self.to_safe(droplet['image']['distribution'])] = dest - self.inventory['status_' + droplet['status']] = dest def load_droplet_variables_for_host(self):