From 369cb8fa9f64578ce2c8e0252921caa42020487a Mon Sep 17 00:00:00 2001 From: William Durand Date: Sun, 14 Feb 2016 17:04:26 +0100 Subject: [PATCH] [contrib] Add option to define group vars in DigitalOcean dynamic inventory script --- contrib/inventory/digital_ocean.ini | 6 ++++++ contrib/inventory/digital_ocean.py | 26 +++++++++++++++++--------- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/contrib/inventory/digital_ocean.ini b/contrib/inventory/digital_ocean.ini index 01afe33968..b809554b20 100644 --- a/contrib/inventory/digital_ocean.ini +++ b/contrib/inventory/digital_ocean.ini @@ -26,3 +26,9 @@ cache_max_age = 300 # Use the private network IP address instead of the public when available. # use_private_network = False + +# Pass variables to every group, e.g.: +# +# group_variables = { 'ansible_user': 'root' } +# +group_variables = {} diff --git a/contrib/inventory/digital_ocean.py b/contrib/inventory/digital_ocean.py index 1c0ef68cff..8eeeba8de6 100755 --- a/contrib/inventory/digital_ocean.py +++ b/contrib/inventory/digital_ocean.py @@ -137,6 +137,7 @@ import re import argparse from time import time import ConfigParser +import ast try: import json @@ -168,6 +169,7 @@ class DigitalOceanInventory(object): self.cache_path = '.' self.cache_max_age = 0 self.use_private_network = False + self.group_variables = {} # Read settings, environment variables, and CLI arguments self.read_settings() @@ -261,6 +263,10 @@ or environment variables (DO_API_TOKEN)''') if config.has_option('digital_ocean', 'use_private_network'): self.use_private_network = config.get('digital_ocean', 'use_private_network') + # Group variables + if config.has_option('digital_ocean', 'group_variables'): + self.group_variables = ast.literal_eval(config.get('digital_ocean', 'group_variables')) + def read_environment(self): ''' Reads the settings from environment variables ''' # Setup credentials @@ -359,22 +365,24 @@ or environment variables (DO_API_TOKEN)''') else: dest = droplet['ip_address'] - self.inventory[droplet['id']] = [dest] - self.push(self.inventory, droplet['name'], dest) - self.push(self.inventory, 'region_' + droplet['region']['slug'], dest) - self.push(self.inventory, 'image_' + str(droplet['image']['id']), dest) - self.push(self.inventory, 'size_' + droplet['size']['slug'], dest) + dest = { 'hosts': [ dest ], 'vars': self.group_variables } + + 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.push(self.inventory, 'image_' + self.to_safe(image_slug), dest) + self.inventory['image_' + self.to_safe(image_slug)] = dest else: image_name = droplet['image']['name'] if image_name: - self.push(self.inventory, 'image_' + self.to_safe(image_name), dest) + self.inventory['image_' + self.to_safe(image_name)] = dest - self.push(self.inventory, 'distro_' + self.to_safe(droplet['image']['distribution']), dest) - self.push(self.inventory, 'status_' + droplet['status'], dest) + self.inventory['distro_' + self.to_safe(droplet['image']['distribution'])] = dest + self.inventory['status_' + droplet['status']] = dest def load_droplet_variables_for_host(self):