refactor ♻️: Refactoring the tasks directory to include new roles and tasks.
The commit updates the `tasks` directory by adding new roles (`rapbian_desktop_prov.yml`, `test_remote.yml`, `update_roles.sh`) and tasks within these roles. This refactoring enhances the organization and maintainability of the Ansible playbook.
This commit is contained in:
65
tasks/get_ssh_key.sh
Normal file
65
tasks/get_ssh_key.sh
Normal file
@@ -0,0 +1,65 @@
|
||||
#!/bin/bash
|
||||
echo "[DEBUG] Script started"
|
||||
echo "Running as $(whoami)"
|
||||
echo
|
||||
|
||||
# Force use of specific home directory
|
||||
USER_HOME="/home/semaphore"
|
||||
|
||||
# Get current user home directory
|
||||
# USER_HOME=$(eval echo ~${SUDO_USER:-$USER})
|
||||
|
||||
# Define default public key locations
|
||||
KEY_FILES=(
|
||||
"$USER_HOME/.ssh/id_rsa.pub"
|
||||
"$USER_HOME/.ssh/id_ecdsa.pub"
|
||||
"$USER_HOME/.ssh/id_ed25519.pub"
|
||||
)
|
||||
|
||||
# Loop through each key file to find the first one that exists
|
||||
for key_file in "${KEY_FILES[@]}"; do
|
||||
if [ -f "$key_file" ]; then
|
||||
if [ -r "$key_file" ]; then
|
||||
echo "✅ Public SSH key found at: $key_file"
|
||||
echo
|
||||
cat "$key_file"
|
||||
exit 0
|
||||
else
|
||||
echo "⚠️ Found public key at $key_file, but it's not readable (permission issue)"
|
||||
fi
|
||||
fi
|
||||
done
|
||||
|
||||
# If no public key was found, check for private key to regenerate the pub key
|
||||
PRIVATE_KEY="$USER_HOME/.ssh/id_rsa"
|
||||
if [ -f "$PRIVATE_KEY" ] && [ ! -f "${PRIVATE_KEY}.pub" ]; then
|
||||
echo "Public key missing, but private key found. Regenerating .pub file..."
|
||||
ssh-keygen -y -f "$PRIVATE_KEY" > "${PRIVATE_KEY}.pub"
|
||||
if [ $? -eq 0 ]; then
|
||||
echo "Public key regenerated at: ${PRIVATE_KEY}.pub"
|
||||
echo
|
||||
cat "${PRIVATE_KEY}.pub"
|
||||
exit 0
|
||||
else
|
||||
echo "Failed to regenerate public key from private key." >&2
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
|
||||
# If no keys at all, generate new one
|
||||
echo "No SSH key found. Generating a new SSH key at: $PRIVATE_KEY"
|
||||
mkdir -p "$USER_HOME/.ssh"
|
||||
chmod 700 "$USER_HOME/.ssh"
|
||||
|
||||
ssh-keygen -t rsa -b 4096 -C "admin@localhost" -f "$PRIVATE_KEY" -N ""
|
||||
|
||||
# Output the new key
|
||||
if [ -f "${PRIVATE_KEY}.pub" ]; then
|
||||
echo
|
||||
echo "New SSH key generated at: ${PRIVATE_KEY}.pub"
|
||||
cat "${PRIVATE_KEY}.pub"
|
||||
exit 0
|
||||
else
|
||||
echo "Failed to generate SSH key." >&2
|
||||
exit 1
|
||||
fi
|
||||
Reference in New Issue
Block a user