Skip to content

Commit

Permalink
ansible: tweak network facts error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
saltydk committed Dec 6, 2023
1 parent 74eedb7 commit 2a3bbb3
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions ansible_facts.d/network.fact
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,10 @@ def has_valid_ipv6():
for line in lines:
line = line.strip()
if line.startswith("inet6") and not line.startswith("inet6 fe80"):
return True
return False
return True, None # Valid IPv6 found, no error
return False, None # No valid IPv6 found, no error
except Exception as e:
print(f"Error checking for IPv6 addresses: {e}")
return False
return False, f"Error checking for IPv6 addresses: {e}" # Error occurred

def validate_ip(ip, version):
try:
Expand Down Expand Up @@ -67,22 +66,25 @@ data = {
"error_ipv4": None,
"error_ipv6": None,
"failed_ipv4": False,
"failed_ipv6": False
"failed_ipv6": False,
"ipv6_check_error": None # Add field for IPv6 check error
}
}

public_ip, error_v4, failed_v4 = get_ip(urls["ipv4_primary"], "ipv4")
if not public_ip:
public_ip, error_v4, failed_v4 = get_ip(urls["ipv4_fallback"], "ipv4")

# Only fetch IPv6 data if a valid IPv6 address is present on the system
if has_valid_ipv6():
ipv6_present, ipv6_error = has_valid_ipv6()
if ipv6_present:
public_ipv6, error_v6, failed_v6 = get_ip(urls["ipv6_primary"], "ipv6")
if not public_ipv6:
public_ipv6, error_v6, failed_v6 = get_ip(urls["ipv6_fallback"], "ipv6")
data["ip"]["public_ipv6"] = public_ipv6 or ""
data["ip"]["error_ipv6"] = error_v6
data["ip"]["failed_ipv6"] = failed_v6
else:
data["ip"]["ipv6_check_error"] = ipv6_error # Store the IPv6 check error

data["ip"]["public_ip"] = public_ip or ""
data["ip"]["error_ipv4"] = error_v4
Expand Down

0 comments on commit 2a3bbb3

Please sign in to comment.