Skip to content

Commit 36ffaa1

Browse files
committed
ansible: Use our own ipify server and fallback to icanhazip if it fails
1 parent e6b79ec commit 36ffaa1

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

Diff for: ansible_facts.d/network.fact

+22-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,28 @@
22

33
# https://github.com/mtecer/terraform/blob/master/bmc/files/ansible/custom.fact
44

5-
PUBLIC_IP=$(curl --connect-timeout 2.37 -s -4 https://ipv4.icanhazip.com)
6-
PUBLIC_IPv6=$(curl --connect-timeout 2.37 -s -6 https://ipv6.icanhazip.com)
5+
RES=$(curl -s -w "%{http_code}" --connect-timeout 2.37 -4 https://ipify.saltbox.dev)
6+
BODY=${RES::-3}
7+
STATUS=$(printf "%s" "$RES" | tail -c 3)
8+
if [[ "$STATUS" == 200 ]]; then
9+
# Server returned 200 response
10+
PUBLIC_IP=$BODY
11+
else
12+
# Server didn't return 200 response, so falling back to icanhazip.com
13+
PUBLIC_IP=$(curl --connect-timeout 2.37 -s -4 https://ipv4.icanhazip.com)
14+
fi
15+
16+
RES6=$(curl -s -w "%{http_code}" --connect-timeout 2.37 -6 https://ipify6.saltbox.dev)
17+
BODY6=${RES6::-3}
18+
STATUS6=$(printf "%s" "$RES6" | tail -c 3)
19+
if [[ "$STATUS" == 200 ]]; then
20+
# Server returned 200 response
21+
PUBLIC_IPv6=$BODY6
22+
else
23+
# Server didn't return 200 response, so falling back to icanhazip.com
24+
PUBLIC_IPv6=$(curl --connect-timeout 2.37 -s -6 https://ipv6.icanhazip.com)
25+
fi
26+
727
echo "[ip]"
828
echo "public_ip=${PUBLIC_IP}"
929
echo "public_ipv6=${PUBLIC_IPv6}"

0 commit comments

Comments
 (0)