summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJared Sprague <Jared-Sprague@users.noreply.github.com>2016-09-09 06:14:27 -0700
committerBrian Coca <bcoca@users.noreply.github.com>2016-09-09 09:14:27 -0400
commit23f78efd7411261a363fad0ffb8f7ebfdd279435 (patch)
treeb1bc5932ecaf6aa2315d4fb0a84fe7b04200c73b
parentc81fe70fbfcd083537558b5bd6bda9d284e2959d (diff)
downloadansible-23f78efd7411261a363fad0ffb8f7ebfdd279435.tar.gz
Add public_ip option to linode dynamic inventory (#17390)
* Add public_ip option to linode dynamic inventory * Add an option to use public IP instead of label for linode hosts
-rw-r--r--contrib/inventory/linode.ini3
-rwxr-xr-xcontrib/inventory/linode.py19
2 files changed, 18 insertions, 4 deletions
diff --git a/contrib/inventory/linode.ini b/contrib/inventory/linode.ini
index ec7f007fac..c925d970e9 100644
--- a/contrib/inventory/linode.ini
+++ b/contrib/inventory/linode.ini
@@ -13,3 +13,6 @@ cache_path = /tmp
# The number of seconds a cache file is considered valid. After this many
# seconds, a new API call will be made, and the cache file will be updated.
cache_max_age = 300
+
+# If set to true use the hosts public ip in the dictionary instead of the label
+use_public_ip = false \ No newline at end of file
diff --git a/contrib/inventory/linode.py b/contrib/inventory/linode.py
index b19967ab23..8e331e2f6e 100755
--- a/contrib/inventory/linode.py
+++ b/contrib/inventory/linode.py
@@ -15,9 +15,13 @@ installed and has a valid config at ~/.chube. If not, run:
For more details, see: https://github.com/exosite/chube
-NOTE: This script also assumes that the Linodes in your account all have
+NOTE: By default, this script also assumes that the Linodes in your account all have
labels that correspond to hostnames that are in your resolver search path.
Your resolver search path resides in /etc/hosts.
+Optionally, if you would like to use the hosts public IP instead of it's label use
+the following setting in linode.ini:
+
+ use_public_ip = true
When run against a specific host, this script returns the following variables:
@@ -161,6 +165,7 @@ class LinodeInventory(object):
self.cache_path_cache = cache_path + "/ansible-linode.cache"
self.cache_path_index = cache_path + "/ansible-linode.index"
self.cache_max_age = config.getint('linode', 'cache_max_age')
+ self.use_public_ip = config.getboolean('linode', 'use_public_ip')
def parse_cli_args(self):
"""Command line argument processing"""
@@ -212,8 +217,10 @@ class LinodeInventory(object):
def add_node(self, node):
"""Adds an node to the inventory and index."""
-
- dest = node.label
+ if self.use_public_ip:
+ dest = self.get_node_public_ip(node)
+ else:
+ dest = node.label
# Add to index
self.index[dest] = node.api_id
@@ -227,6 +234,10 @@ class LinodeInventory(object):
# Inventory: Group by dipslay group
self.push(self.inventory, node.display_group, dest)
+ def get_node_public_ip(self, node):
+ """Returns a the public IP address of the node"""
+ return [addr.address for addr in node.ipaddresses if addr.is_public][0]
+
def get_host_info(self):
"""Get variables about a specific host."""
@@ -272,7 +283,7 @@ class LinodeInventory(object):
node_vars[direct_attr] = getattr(node, direct_attr)
node_vars["datacenter_city"] = self.get_datacenter_city(node)
- node_vars["public_ip"] = [addr.address for addr in node.ipaddresses if addr.is_public][0]
+ node_vars["public_ip"] = self.get_node_public_ip(node)
# Set the SSH host information, so these inventory items can be used if
# their labels aren't FQDNs