Files
Terraform/.ansible.d/setup.sh
2024-12-15 23:00:51 -06:00

43 lines
953 B
Bash
Executable File

#!/bin/bash
host=$1
KEY_FILE="~/.ssh/setuproot.key.priv"
PLAYBOOK="./.ansible.d/setup.yml"
port=22
# Set variables
timeout=5
max_retries=10
retry_delay=3
# Function to check connection
check_connection() {
nc -z -w $timeout $host $port
}
# Main loop
for i in $(seq 1 $max_retries); do
echo "Attempt $i: Checking connection to $host:$port"
# Check connection
if check_connection; then
echo "Connection successful. Running Ansible"
ansible-playbook --key-file "$KEY_FILE" -i "$host," "$PLAYBOOK"
exit 0
else
echo "Connection failed. Retrying."
# Execute Ansible playbook
# Wait before next attempt
sleep $retry_delay
# Break out of loop since we've executed the playbook
fi
# Increment retry count
((i++))
done
# If we reach here, max retries were exceeded
echo "Max retries ($max_retries) exceeded. Connection failed."
exit 1