new setup script
This commit is contained in:
@@ -1,13 +1,43 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
IP=$1
|
host=$1
|
||||||
KEY_FILE="~/.ssh/setuproot.key.priv"
|
KEY_FILE="~/.ssh/setuproot.key.priv"
|
||||||
PLAYBOOK="./.ansible.d/setup.yml"
|
PLAYBOOK="./.ansible.d/setup.yml"
|
||||||
|
port=22
|
||||||
|
|
||||||
until nc -z -w $timeout $host $port; do
|
# Set variables
|
||||||
echo "Attempting connection to $IP. For first setup script."
|
timeout=5
|
||||||
ansible-playbook --key-file "$KEY_FILE" -i "$IP," "$PLAYBOOK"
|
max_retries=10
|
||||||
echo "Connection attempt failed. Retrying in 1 second."
|
retry_delay=3
|
||||||
sleep 1
|
|
||||||
|
# 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
|
done
|
||||||
|
|
||||||
echo "Successfully connected to $IP and ran playbook."
|
# If we reach here, max retries were exceeded
|
||||||
|
echo "Max retries ($max_retries) exceeded. Connection failed."
|
||||||
|
exit 1
|
||||||
Reference in New Issue
Block a user