1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

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
This commit is contained in:
Brian Coca 2016-06-03 15:13:13 -04:00
parent 14fec788a1
commit 3121e352f7

View file

@ -145,7 +145,7 @@ except ImportError:
import simplejson as json import simplejson as json
try: try:
from dopy.manager import DoError, DoManager from dopy.manager import DoManager
except ImportError as e: except ImportError as e:
print("failed=True msg='`dopy` library required for this script'") print("failed=True msg='`dopy` library required for this script'")
sys.exit(1) sys.exit(1)
@ -351,7 +351,13 @@ or environment variables (DO_API_TOKEN)''')
def build_inventory(self): def build_inventory(self):
'''Build Ansible inventory of droplets''' '''Build Ansible inventory of droplets'''
self.inventory = {} self.inventory = {
'all': {
'hosts': [],
'vars': self.group_variables
},
'_meta': {'hostvars': {}}
}
# add all droplets by id and name # add all droplets by id and name
for droplet in self.data['droplets']: for droplet in self.data['droplets']:
@ -365,24 +371,35 @@ or environment variables (DO_API_TOKEN)''')
else: else:
dest = droplet['ip_address'] 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['id']] = dest
self.inventory[droplet['name']] = 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'] # groups that are always present
if image_slug: for group in [
self.inventory['image_' + self.to_safe(image_slug)] = dest 'region_' + droplet['region']['slug'],
else: 'image_' + str(droplet['image']['id']),
image_name = droplet['image']['name'] 'size_' + droplet['size']['slug'],
if image_name: 'distro_' + self.to_safe(droplet['image']['distribution']),
self.inventory['image_' + self.to_safe(image_name)] = dest '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): def load_droplet_variables_for_host(self):