Merge pull request #14471 from willdurand/contrib-do-inventory

[contrib] Add option to define group vars in DigitalOcean dynamic inventory script
This commit is contained in:
Brian Coca 2016-03-21 07:17:46 -07:00
commit d026bc5e17
2 changed files with 23 additions and 9 deletions

View file

@ -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 = {}

View file

@ -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):