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

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
This commit is contained in:
Jared Sprague 2016-09-09 06:14:27 -07:00 committed by Brian Coca
parent c81fe70fbf
commit 23f78efd74
2 changed files with 18 additions and 4 deletions

View file

@ -13,3 +13,6 @@ cache_path = /tmp
# The number of seconds a cache file is considered valid. After this many # 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. # seconds, a new API call will be made, and the cache file will be updated.
cache_max_age = 300 cache_max_age = 300
# If set to true use the hosts public ip in the dictionary instead of the label
use_public_ip = false

View file

@ -15,9 +15,13 @@ installed and has a valid config at ~/.chube. If not, run:
For more details, see: https://github.com/exosite/chube 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. labels that correspond to hostnames that are in your resolver search path.
Your resolver search path resides in /etc/hosts. 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: 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_cache = cache_path + "/ansible-linode.cache"
self.cache_path_index = cache_path + "/ansible-linode.index" self.cache_path_index = cache_path + "/ansible-linode.index"
self.cache_max_age = config.getint('linode', 'cache_max_age') 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): def parse_cli_args(self):
"""Command line argument processing""" """Command line argument processing"""
@ -212,8 +217,10 @@ class LinodeInventory(object):
def add_node(self, node): def add_node(self, node):
"""Adds an node to the inventory and index.""" """Adds an node to the inventory and index."""
if self.use_public_ip:
dest = node.label dest = self.get_node_public_ip(node)
else:
dest = node.label
# Add to index # Add to index
self.index[dest] = node.api_id self.index[dest] = node.api_id
@ -227,6 +234,10 @@ class LinodeInventory(object):
# Inventory: Group by dipslay group # Inventory: Group by dipslay group
self.push(self.inventory, node.display_group, dest) 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): def get_host_info(self):
"""Get variables about a specific host.""" """Get variables about a specific host."""
@ -272,7 +283,7 @@ class LinodeInventory(object):
node_vars[direct_attr] = getattr(node, direct_attr) node_vars[direct_attr] = getattr(node, direct_attr)
node_vars["datacenter_city"] = self.get_datacenter_city(node) 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 # Set the SSH host information, so these inventory items can be used if
# their labels aren't FQDNs # their labels aren't FQDNs