Skip to content

Instantly share code, notes, and snippets.

@damoun
Created September 1, 2023 12:22
Show Gist options
  • Save damoun/ee5dc3ca89bd8cb4f8681727c62c02f0 to your computer and use it in GitHub Desktop.
Save damoun/ee5dc3ca89bd8cb4f8681727c62c02f0 to your computer and use it in GitHub Desktop.
Assign Hetzner Cloud floating IP to a server
#!/bin/bash
# Let you assign a floating IP to this server.
# Useful for Keepalived
#
# Require curl & jq
API_TOKEN=""
FLOATING_IP_ID=""
SERVER_ID=""
if [ "${SERVER_ID}" == "" ]
then
SERVER_ID="$(curl --silent -H \"Authorization: Bearer ${API_TOKEN}\" \"https://api.hetzner.cloud/v1/servers?name=$HOSTNAME\"|jq '.servers[0].id')"
fi
curl \
--silent \
-X POST \
-H "Authorization: Bearer ${API_TOKEN}" \
-H 'Content-Type: application/json' \
-d "{\"server\": ${SERVER_ID}}" \
"https://api.hetzner.cloud/v1/floating_ips/${FLOATING_IP_ID}/actions/assign"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment