2 Commits

Author SHA1 Message Date
34f936e21c Capture Server Name column and prioritize for inventory keys
All checks were successful
Build and Release / Build Windows Exe (push) Successful in 10s
2026-02-06 16:11:48 -05:00
5c95469ca3 Support SVR prefix in hostname cleanup
All checks were successful
Build and Release / Build Windows Exe (push) Successful in 11s
2026-02-06 16:06:30 -05:00
4 changed files with 29 additions and 7 deletions

View File

@@ -27,5 +27,20 @@ class TestInventoryKeys(unittest.TestCase):
self.assertEqual(host_vars['ansible_host'], "192.168.1.10")
self.assertEqual(host_vars['ansible_connection'], "winrm")
def test_clean_reference_logic(self):
from wif2ansible.parsers import clean_reference
# Test cases
self.assertEqual(clean_reference("SRV123 MyServer"), "MyServer")
self.assertEqual(clean_reference("SVR999 AnotherServer"), "AnotherServer")
self.assertEqual(clean_reference("srv001 lowercase"), "lowercase")
self.assertEqual(clean_reference("SvR555 MixedCase"), "MixedCase")
self.assertEqual(clean_reference("JustName"), "JustName")
self.assertEqual(clean_reference("SRV123"), "") # Should be empty? or handle?
# If it's just SRV123, strip returns empty.
# User said "never include these in output".
# If the server is ONLY named SRV123, what then?
# Assuming there is usually a name.
if __name__ == '__main__':
unittest.main()

View File

@@ -77,8 +77,8 @@ def read_servers(filename: str) -> Dict[str, Server]:
print("Warning: No 'Servers' sheet found.")
return {}
# keywords: reference, platform, ip address, management ip, production ip
header_keywords = ['reference', 'platform', 'ip address', 'production ip']
# keywords: reference, platform, ip address, management ip, production ip, server name
header_keywords = ['reference', 'platform', 'ip address', 'production ip', 'server name']
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
ref_idx = col_map.get('reference')
name_idx = col_map.get('server name') # User confirmed header
plat_idx = col_map.get('platform')
ip_idx = col_map.get('ip address') # Generic/Management IP
prod_ip_idx = col_map.get('production ip') # Specific Production IP
@@ -110,6 +111,12 @@ def read_servers(filename: str) -> Dict[str, Server]:
if not ref or ref.lower() == 'example':
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'
# Parse Management IP
@@ -130,7 +137,7 @@ def read_servers(filename: str) -> Dict[str, Server]:
s = Server(
reference=ref,
hostname=clean_reference(ref),
hostname=final_hostname,
platform=plat,
ip_address=ip_addr,
production_ip=prod_ip_addr

View File

@@ -61,8 +61,8 @@ def generate_inventory(servers: Dict[str, Server], flows: List[Flow]) -> Dict[st
match_count += 1
# Prepare host entry if new
# We use the Reference/Hostname as the key in inventory 'hosts'
host_key = server.reference or server.hostname or server.ip_address
# We use the Hostname (from Server Name col) -> Reference (cleaned) -> IP match
host_key = server.hostname or server.reference or server.ip_address
if host_key not in inventory_hosts:
host_vars = server.get_ansible_vars()

View File

@@ -67,8 +67,8 @@ def clean_reference(ref: str) -> str:
return ""
s = str(ref)
# Remove SRV followed by digits and whitespace
s = re.sub(r'SRV\d+\s*', '', s, flags=re.IGNORECASE)
# Remove SRV or SVR followed by digits and whitespace
s = re.sub(r'S(RV|VR)\d+\s*', '', s, flags=re.IGNORECASE)
# Remove leading/trailing whitespace
return s.strip()