Linode inventory improvements: Add _meta/hostvars to inventory output. Add a 'linode' host group with all the hosts (#19329)

This commit is contained in:
Nick Ball 2017-08-02 10:26:30 -07:00 committed by Sloane Hertel
parent a260d99f74
commit edf31f56af

View file

@ -115,10 +115,13 @@ import ConfigParser
class LinodeInventory(object):
def _empty_inventory(self):
return {"_meta": {"hostvars": {}}}
def __init__(self):
"""Main execution path."""
# Inventory grouped by display group
self.inventory = {}
self.inventory = self._empty_inventory()
# Index of label to Linode ID
self.index = {}
# Local cache of Datacenter objects populated by populate_datacenter_cache()
@ -139,7 +142,7 @@ class LinodeInventory(object):
data_to_print = self.get_host_info()
elif self.args.list:
# Display list of nodes for inventory
if len(self.inventory) == 0:
if len(self.inventory) == 1:
data_to_print = self.get_inventory_from_cache()
else:
data_to_print = self.json_format_dict(self.inventory, True)
@ -232,9 +235,15 @@ class LinodeInventory(object):
# Inventory: Group by datacenter city
self.push(self.inventory, self.get_datacenter_city(node), dest)
# Inventory: Group by dipslay group
# Inventory: Group by display group
self.push(self.inventory, node.display_group, dest)
# Inventory: Add a "linode" global tag group
self.push(self.inventory, "linode", dest)
# Add host info to hostvars
self.inventory["_meta"]["hostvars"][dest] = self.get_host_info(node)
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]
@ -254,8 +263,11 @@ class LinodeInventory(object):
return self.json_format_dict({}, True)
node_id = self.index[self.args.host]
node = self.get_node(node_id)
return self.json_format_dict(self.get_host_info(node), True)
def get_host_info(self, node):
node_vars = {}
for direct_attr in [
"api_id",
@ -296,7 +308,7 @@ class LinodeInventory(object):
if private_ips:
node_vars["private_ip"] = private_ips[0]
return self.json_format_dict(node_vars, True)
return node_vars
def push(self, my_dict, key, element):
"""Pushed an element onto an array that may not have been defined in the dict."""