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

pull/23574/merge
Nick Ball 7 years ago committed by Sloane Hertel
parent a260d99f74
commit edf31f56af

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

Loading…
Cancel
Save