Files
devops/orchestrate/ansible.sh
2025-09-23 10:54:10 -05:00

124 lines
3.2 KiB
Bash
Executable File

#!/bin/bash
#
# Busnet Ansible
# @author Craig McDaniel
#
# This script uses Ansible to provision computers in the BusNet.
#
INCLUDE_DIR="/opt/busnet/git/devops/orchestrate/lib"
# Read in libs
source ${INCLUDE_DIR}/lib.sh
COMMAND=$1
# Show help text
show_help()
{
echo
echo "BusNet ansible script."
echo
echo "Usage: $0 <command>"
echo
echo "General Ansible stuff:"
echo "---------------------------------------------------------------------------------------------------------------"
echo " --build Build the Anisble docker image. We use this everywhere."
echo " --run Start bash shell inside the ansible container."
echo " --bootstrap-server Bootstrap a brand new server for the first time. This executes the Ansible bootstrap"
echo " role on it. Run this once."
echo
echo "scanner.busnet:"
echo "---------------------------------------------------------------------------------------------------------------"
echo " --scanner-direwolf Install and configure direwolf on scanner.busnet"
echo
}
# Main entry point.
main()
{
# Account/billing server
if [ "${COMMAND}" = "--run" ]; then
docker_run_it 'ansible-busnet:latest' runner bash
elif [ "${COMMAND}" = "--bootstrap-server" ]; then
ansible_bootstrap_server
elif [ "${COMMAND}" = "--build" ]; then
ansible_build
elif [ "${COMMAND}" = "--scanner-direwolf" ]; then
scanner_direwolf
# Show Help
else
show_help
fi
}
ansible_bootstrap_server()
{
# Check if the private key exists before proceeding.
local PRIVATE_KEY_PATH="${HOME}/.ssh/${CONFIG_ANSIBLE_KEY_NAME}"
local PUBLIC_KEY_PATH="${PRIVATE_KEY_PATH}.pub"
if [[ ! -f "$PUBLIC_KEY_PATH" ]]; then
echo "Error: Public key not found at $PUBLIC_KEY_PATH"
echo "The function generate_busnet_key() should have generated one already. Something did not work correctly. Doing nothing."
return 1 # Exit the function with an error code
fi
echo "Bootstrap a new server!"
echo "This will copy the SSH key and execute the ansible bootstrap role."
echo
echo "The hostname you enter below MUST be added to Ansible inventory first. If it's not, go add it now!"
echo
# Prompt for the hostname and the user to connect as.
read -e -p "Enter the hostname of the server: " HOSTNAME
read -e -p "Enter the remote username (e.g., ec2-user): " USERNAME
echo
# Copy the SSH public key to the remote server.
echo "Attempting to copy SSH key to ${USERNAME}@${HOSTNAME}..."
if ssh-copy-id -i "${PRIVATE_KEY_PATH}" "${USERNAME}"@"${HOSTNAME}"; then
echo "SSH key copied successfully."
else
echo "Error: Failed to copy SSH key. Please check the hostname, username, and your SSH connection."
return 1
fi
echo
echo "Running the Ansible playbook..."
ansible_playbook playbooks/general/bootstrap.yml "hostname=${HOSTNAME}"
if [ $? != 0 ]; then
echo
echo "Ansible playbook execution failed."
echo
else
echo
echo "Ansible playbook execution complete."
echo
fi
}
scanner_direwolf()
{
ansible_playbook playbooks/scanner/direwolf.yml
if [ $? != 0 ]; then
echo
echo "Ansible playbook execution failed."
echo
else
echo
echo "Ansible playbook execution complete."
echo
fi
}
# Start the script
main