From 2a3bbb3377b3001aac596a632c5bfbe4c287b489 Mon Sep 17 00:00:00 2001 From: saltydk Date: Wed, 6 Dec 2023 20:30:14 +0100 Subject: [PATCH] ansible: tweak network facts error handling --- ansible_facts.d/network.fact | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/ansible_facts.d/network.fact b/ansible_facts.d/network.fact index 5c39c251ba..0a08de5eb1 100755 --- a/ansible_facts.d/network.fact +++ b/ansible_facts.d/network.fact @@ -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: @@ -67,7 +66,8 @@ 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 } } @@ -75,14 +75,16 @@ 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