Commit 44f8f35f authored by Alberts S's avatar Alberts S
Browse files

Add functionality to Inventory to override node hostnames

parent 0e82b6a1
......@@ -12,6 +12,7 @@ class Inventory(CapybaraNetty):
self.chef_api = chef.autoconfigure()
self.chef_api.ssl_verify = False
self.inventory_match_hosts = self.config.get("inventory_match_hosts", None)
self.inventory_name_overrides = self.config.get("inventory_hostname_overrides", {})
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
def is_matched_host(self, hostname: str):
......@@ -23,6 +24,9 @@ class Inventory(CapybaraNetty):
return True
return False
def get_hostname_override(self, hostname):
return self.inventory_name_overrides.get(hostname) or hostname
async def get_hosts(self):
hosts = []
for node_name in self.chef_api["/nodes"]:
......@@ -31,6 +35,7 @@ class Inventory(CapybaraNetty):
nodeTime = datetime.utcfromtimestamp(chefNode.attributes["ohai_time"])
ipv4 = chefNode.attributes["base"]["public_ipv4"].strip()
hostname = chefNode.attributes["base"]["hostname"]
hostname = self.get_hostname_override(hostname)
if not self.is_matched_host(hostname):
self.__logger.warning(f"host {hostname} is FILTERED out")
......@@ -40,6 +45,7 @@ class Inventory(CapybaraNetty):
if (nodeTime + timedelta(hours=1)) > datetime.utcnow():
if await self.ssh_is_alive(ipv4, self.config["default_router_ssh_user"]):
self.__logger.info(f"Loaded {hostname=} {ipv4=}")
hosts.append(
{
"public_ipv4": ipv4,
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment