Skip to content

Commit

Permalink
Register host variables for the DigitalOcean dynamic inventory
Browse files Browse the repository at this point in the history
  • Loading branch information
artmatsak authored and bcoca committed Jan 6, 2017
1 parent 40c6191 commit e4c1eff
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions contrib/inventory/digital_ocean.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
- size_NAME
- status_STATUS
When run against a specific host, this script returns the following variables:
For each host, the following variables are registered:
- do_backup_ids
- do_created_at
- do_disk
Expand Down Expand Up @@ -407,18 +407,16 @@ def build_inventory(self):
self.inventory[tag] = { 'hosts': [ ], 'vars': {} }
self.inventory[tag]['hosts'].append(dest)

# hostvars
info = self.do_namespace(droplet)
self.inventory['_meta']['hostvars'][dest] = info


def load_droplet_variables_for_host(self):
'''Generate a JSON response to a --host call'''
host = int(self.args.host)

droplet = self.manager.show_droplet(host)

# Put all the information in a 'do_' namespace
info = {}
for k, v in droplet.items():
info['do_'+k] = v

info = self.do_namespace(droplet)
return {'droplet': info}


Expand Down Expand Up @@ -477,6 +475,13 @@ def to_safe(self, word):
''' Converts 'bad' characters in a string to underscores so they can be used as Ansible groups '''
return re.sub("[^A-Za-z0-9\-\.]", "_", word)

def do_namespace(self, data):
''' Returns a copy of the dictionary with all the keys put in a 'do_' namespace '''
info = {}
for k, v in data.items():
info['do_'+k] = v
return info



###########################################################################
Expand Down

0 comments on commit e4c1eff

Please sign in to comment.