Support multiple IPs per server and robust mapping
All checks were successful
Build and Release / Build Windows Exe (push) Successful in 10s

This commit is contained in:
2026-02-06 16:19:05 -05:00
parent 34f936e21c
commit fc1c4bfaa8
4 changed files with 35 additions and 23 deletions

View File

@@ -5,7 +5,7 @@ from wif2ansible.inventory import generate_inventory
class TestInventoryKeys(unittest.TestCase): class TestInventoryKeys(unittest.TestCase):
def test_inventory_keys_are_hostnames(self): def test_inventory_keys_are_hostnames(self):
# Create a server with Ref, Hostname, IP # Create a server with Ref, Hostname, IP
s1 = Server(reference="SERVER_REF_01", hostname="server01", ip_address="192.168.1.10", platform="windows") s1 = Server(reference="SERVER_REF_01", hostname="server01", ip_addresses=["192.168.1.10"], platform="windows")
# Create a flow matching this server # Create a flow matching this server
f1 = Flow(flow_id="1", source_ip="192.168.1.10", destination_ip="10.0.0.1", ports=[80]) f1 = Flow(flow_id="1", source_ip="192.168.1.10", destination_ip="10.0.0.1", ports=[80])
@@ -18,12 +18,12 @@ class TestInventoryKeys(unittest.TestCase):
# Verify stricture # Verify stricture
hosts = inventory['all']['hosts'] hosts = inventory['all']['hosts']
# Key should be REFERENCE "SERVER_REF_01" (or hostname/ip fallback) # Key should be HOSTNAME "server01" (prioritized over Ref)
self.assertIn("SERVER_REF_01", hosts) self.assertIn("server01", hosts)
self.assertNotIn("192.168.1.10", hosts) self.assertNotIn("192.168.1.10", hosts)
# Check variables # Check variables
host_vars = hosts["SERVER_REF_01"] host_vars = hosts["server01"]
self.assertEqual(host_vars['ansible_host'], "192.168.1.10") self.assertEqual(host_vars['ansible_host'], "192.168.1.10")
self.assertEqual(host_vars['ansible_connection'], "winrm") self.assertEqual(host_vars['ansible_connection'], "winrm")

View File

@@ -120,27 +120,24 @@ def read_servers(filename: str) -> Dict[str, Server]:
plat = get_val(plat_idx) or 'unknown' plat = get_val(plat_idx) or 'unknown'
# Parse Management IP # Parse Management IP
# Support multiple IPs
ip_raw = get_val(ip_idx) ip_raw = get_val(ip_idx)
ip_addr = None ip_list = []
if ip_raw: if ip_raw:
ips = parse_ip(ip_raw) ip_list = parse_ip(ip_raw)
if ips:
ip_addr = ips[0]
# Parse Production IP # Parse Production IP
prod_ip_raw = get_val(prod_ip_idx) prod_ip_raw = get_val(prod_ip_idx)
prod_ip_addr = None prod_ip_list = []
if prod_ip_raw: if prod_ip_raw:
ips = parse_ip(prod_ip_raw) prod_ip_list = parse_ip(prod_ip_raw)
if ips:
prod_ip_addr = ips[0]
s = Server( s = Server(
reference=ref, reference=ref,
hostname=final_hostname, hostname=final_hostname,
platform=plat, platform=plat,
ip_address=ip_addr, ip_addresses=ip_list,
production_ip=prod_ip_addr production_ips=prod_ip_list
) )
servers[ref] = s servers[ref] = s

View File

@@ -18,10 +18,14 @@ def generate_inventory(servers: Dict[str, Server], flows: List[Flow]) -> Dict[st
ip_to_server = {} ip_to_server = {}
for s in servers.values(): for s in servers.values():
if s.ip_address: # Index all Management IPs
ip_to_server[s.ip_address] = s for ip in s.ip_addresses:
if s.production_ip: ip_to_server[ip] = s
ip_to_server[s.production_ip] = s
# Index all Production IPs
for ip in s.production_ips:
ip_to_server[ip] = s
# Also index by reference/hostname for DNS matches # Also index by reference/hostname for DNS matches
if s.reference: if s.reference:
ip_to_server[s.reference.lower()] = s ip_to_server[s.reference.lower()] = s
@@ -62,7 +66,7 @@ def generate_inventory(servers: Dict[str, Server], flows: List[Flow]) -> Dict[st
# Prepare host entry if new # Prepare host entry if new
# We use the Hostname (from Server Name col) -> Reference (cleaned) -> IP match # We use the Hostname (from Server Name col) -> Reference (cleaned) -> IP match
host_key = server.hostname or server.reference or server.ip_address host_key = server.hostname or server.reference or server.primary_ip
if host_key not in inventory_hosts: if host_key not in inventory_hosts:
host_vars = server.get_ansible_vars() host_vars = server.get_ansible_vars()

View File

@@ -5,8 +5,19 @@ from typing import List, Dict, Optional, Any
class Server: class Server:
reference: str reference: str
hostname: str # This might be same as reference hostname: str # This might be same as reference
ip_address: Optional[str] = None # Support multiple IPs per field (lists)
production_ip: Optional[str] = None ip_addresses: List[str] = field(default_factory=list)
production_ips: List[str] = field(default_factory=list)
# helper for compatibility/primary IP
@property
def primary_ip(self) -> Optional[str]:
return self.ip_addresses[0] if self.ip_addresses else None
@property
def primary_prod_ip(self) -> Optional[str]:
return self.production_ips[0] if self.production_ips else None
platform: str = 'unknown' # e.g. 'Windows', 'Linux' platform: str = 'unknown' # e.g. 'Windows', 'Linux'
def get_ansible_vars(self) -> Dict[str, Any]: def get_ansible_vars(self) -> Dict[str, Any]:
@@ -23,8 +34,8 @@ class Server:
# Default ssh is usually fine, but being explicit doesn't hurt # Default ssh is usually fine, but being explicit doesn't hurt
pass pass
if self.ip_address: if self.primary_ip:
vars['ansible_host'] = self.ip_address vars['ansible_host'] = self.primary_ip
return vars return vars