Compare commits
2 Commits
v2026.02.0
...
v2026.02.0
| Author | SHA1 | Date | |
|---|---|---|---|
| fc1c4bfaa8 | |||
| 34f936e21c |
@@ -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")
|
||||||
|
|
||||||
|
|||||||
@@ -77,8 +77,8 @@ def read_servers(filename: str) -> Dict[str, Server]:
|
|||||||
print("Warning: No 'Servers' sheet found.")
|
print("Warning: No 'Servers' sheet found.")
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
# keywords: reference, platform, ip address, management ip, production ip
|
# keywords: reference, platform, ip address, management ip, production ip, server name
|
||||||
header_keywords = ['reference', 'platform', 'ip address', 'production ip']
|
header_keywords = ['reference', 'platform', 'ip address', 'production ip', 'server name']
|
||||||
|
|
||||||
header_row_idx, col_map = find_header_row(target_sheet, header_keywords)
|
header_row_idx, col_map = find_header_row(target_sheet, header_keywords)
|
||||||
|
|
||||||
@@ -96,6 +96,7 @@ def read_servers(filename: str) -> Dict[str, Server]:
|
|||||||
|
|
||||||
# Extract data
|
# Extract data
|
||||||
ref_idx = col_map.get('reference')
|
ref_idx = col_map.get('reference')
|
||||||
|
name_idx = col_map.get('server name') # User confirmed header
|
||||||
plat_idx = col_map.get('platform')
|
plat_idx = col_map.get('platform')
|
||||||
ip_idx = col_map.get('ip address') # Generic/Management IP
|
ip_idx = col_map.get('ip address') # Generic/Management IP
|
||||||
prod_ip_idx = col_map.get('production ip') # Specific Production IP
|
prod_ip_idx = col_map.get('production ip') # Specific Production IP
|
||||||
@@ -110,30 +111,33 @@ def read_servers(filename: str) -> Dict[str, Server]:
|
|||||||
if not ref or ref.lower() == 'example':
|
if not ref or ref.lower() == 'example':
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
# Hostname Logic:
|
||||||
|
# 1. Use 'Server Name' column if available (e.g. ITSMDEV-5009898)
|
||||||
|
# 2. Fallback to cleaned Reference (Stripping SRV###)
|
||||||
|
server_name_raw = get_val(name_idx)
|
||||||
|
final_hostname = server_name_raw if server_name_raw else clean_reference(ref)
|
||||||
|
|
||||||
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=clean_reference(ref),
|
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
|
||||||
|
|
||||||
|
|||||||
@@ -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
|
||||||
@@ -61,8 +65,8 @@ def generate_inventory(servers: Dict[str, Server], flows: List[Flow]) -> Dict[st
|
|||||||
match_count += 1
|
match_count += 1
|
||||||
|
|
||||||
# Prepare host entry if new
|
# Prepare host entry if new
|
||||||
# We use the Reference/Hostname as the key in inventory 'hosts'
|
# We use the Hostname (from Server Name col) -> Reference (cleaned) -> IP match
|
||||||
host_key = server.reference or server.hostname 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()
|
||||||
|
|||||||
@@ -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
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user