From 8dfc73714b1d900960c329f6cb52172c2324ef62 Mon Sep 17 00:00:00 2001 From: tolerryan <105945216+tolerryan@users.noreply.github.com> Date: Sun, 15 Dec 2024 23:00:51 -0600 Subject: [PATCH] new setup script --- .ansible.d/setup.sh | 44 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 37 insertions(+), 7 deletions(-) diff --git a/.ansible.d/setup.sh b/.ansible.d/setup.sh index 2db1b8a..e8bb3bb 100755 --- a/.ansible.d/setup.sh +++ b/.ansible.d/setup.sh @@ -1,13 +1,43 @@ #!/bin/bash -IP=$1 +host=$1 KEY_FILE="~/.ssh/setuproot.key.priv" PLAYBOOK="./.ansible.d/setup.yml" +port=22 -until nc -z -w $timeout $host $port; do - echo "Attempting connection to $IP. For first setup script." - ansible-playbook --key-file "$KEY_FILE" -i "$IP," "$PLAYBOOK" - echo "Connection attempt failed. Retrying in 1 second." - sleep 1 +# 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 -echo "Successfully connected to $IP and ran playbook." \ No newline at end of file +# If we reach here, max retries were exceeded +echo "Max retries ($max_retries) exceeded. Connection failed." +exit 1 \ No newline at end of file