Mastering SSH Config Generation: A Galactic Script
In the vast universe of multi-cloud deployments, managing SSH configurations across different providers and instances can be a daunting task. Fear not, for the Force is with you. With the following script, you can generate an SSH config file effortlessly, bringing order to your galactic fleet.
#!/usr/bin/env bash
read -r -d '' CONFIG << EOM
Host {name}
User user
HostName {ip}
Port {port}
IdentityFile <path/to/secure/storage>/{name}
EOM
ip_array=()
port_array=()
json_data=$(terraform output -json)
nodes=$(jq -r 'to_entries[] | select(.key|endswith("nodes_ipv4s")) | .value.value |to_entries[] | .key' <<< "${json_data}")
ip=$(jq -r 'to_entries[] | select(.key|endswith("nodes_ipv4s")) | .value.value |to_entries[] | [.key, .value]| @csv' <<< "${json_data}" | tr -d '"')
for i in ${ip}; do
ip_array+=(${i/,/::})
done
port=$(jq -r 'to_entries[] | select(.key|endswith("nodes_ports")) | .value.value |to_entries[] | [.key, .value]| @csv' <<< "${json_data}" | tr -d '"')
for i in ${port}; do
port_array+=(${i/,/::})
done
for n in ${nodes}; do
for p in "${port_array[@]}"; do
if grep -q $n <<< $p; then
port=${p##*::}
fi
done
for i in "${ip_array[@]}"; do
if grep -q $n <<< $i; then
ip=${i##*::}
fi
done
echo "${CONFIG}" | sed -e "s/{name}/${n}/g" -e "s/{ip}/${ip}/g" -e "s/{port}/${port}/g"
done
Utilize the power of this script to gather all the public IPs and SSH ports from your Terraform outputs. Replace <path/to/secure/storage>
with the path to your secure storage, ensuring the identity files for each instance are properly located.
Let the Force guide you as you deploy multiple instances across various cloud providers. With this SSH config generator, you’ll gain seamless access to your galactic fleet, simplifying your journey through the cosmos.
May the Force be with you as you master SSH configuration in your multi-cloud adventures!