new setup script
This commit is contained in:
@@ -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."
|
||||
# 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