Skip to content

Instantly share code, notes, and snippets.

@6d61726b760a
Last active January 31, 2022 00:46
Show Gist options
  • Save 6d61726b760a/172a36469c6da46c37f89fd7971988a5 to your computer and use it in GitHub Desktop.
Save 6d61726b760a/172a36469c6da46c37f89fd7971988a5 to your computer and use it in GitHub Desktop.
cloud-init hostnames
#!/bin/bash
# /var/lib/cloud/scripts/per-boot/00-hostname.sh
# https://cloudinit.readthedocs.io/en/latest/topics/modules.html#scripts-per-boot
#
# create/set server hostname using a static prefix with a cloud instance id appended.
#
# sed -i '' \
# 's/instance_name_prefix="NOT-CONFIGURED"/instance_name_prefix="cloudserver"/g' \
# /var/lib/cloud/scripts/per-boot/00-hostname.sh
#
# would result in something like:
# cloudserver-ac283a2e52
#
instance_name_prefix="NOT-CONFIGURED"
# if our script hasnt been configured, exit
if [ ${instance_name_prefix} == "NOT-CONFIGURED" ]; then
exit 0
fi
# generate a token for the metadata service
TOKEN="$(curl --silent -X PUT """http://169.254.169.254/latest/api/token""" -H """X-aws-ec2-metadata-token-ttl-seconds: 300""")"
# take our instance id and strip the "i-" from the front
instance_id="$(curl --silent -H """X-aws-ec2-metadata-token: ${TOKEN}""" http://169.254.169.254/latest/meta-data/instance-id)"
instance_id="${instance_id##*-}"
# then append the id to our hostname prefix
new_hostname="${instance_name_prefix}-${instance_id}"
# and update our hostname
hostnamectl set-hostname "${new_hostname}"
hostnamectl set-hostname --static "${new_hostname}"
hostnamectl
#!/bin/bash
# /var/lib/cloud/scripts/per-boot/00-hostname.sh
# https://cloudinit.readthedocs.io/en/latest/topics/modules.html#scripts-per-boot
#
# create/set server hostname using a static prefix with a cloud instance id appended.
#
# sed -i '' \
# 's/instance_name_prefix="NOT-CONFIGURED"/instance_name_prefix="cloudserver"/g' \
# /var/lib/cloud/scripts/per-boot/00-hostname.sh
#
# would result in something like:
# cloudserver-ac283a2e52
#
instance_name_prefix="NOT-CONFIGURED"
# if our script hasnt been configured, exit
if [ ${instance_name_prefix} == "NOT-CONFIGURED" ]; then
exit 0
fi
# if our script hasnt been configured, exit
if [ ${instance_name_prefix} == "NOT-CONFIGURED" ]; then
exit 0
fi
AWS_URL="http://169.254.169.254/latest/meta-data/instance-id"
AWS_TOKEN_URL="http://169.254.169.254/latest/api/token"
GCP_URL="http://169.254.169.254/computeMetadata/v1/instance/id"
if AWS_TOKEN="$(curl --silent -X PUT ${AWS_TOKEN_URL} -H """X-aws-ec2-metadata-token-ttl-seconds: 300""")"; then
# we're running in AWS
# retrive our identity document
_instance_id="$(curl --silent -H """X-aws-ec2-metadata-token: ${AWS_TOKEN}""" ${AWS_URL})"
# generate our new hostname
new_hostname="${instance_name_prefix}-${_instance_id##*-}"
:;
elif curl --silent --output /dev/null --fail --connect-timeout 10 ${GCP_URL} --header "Metadata-Flavor: Google"; then
# we're running in GCP
# get our instance id
_instanceid="$(curl --silent -H 'Metadata-Flavor: Google' ${GCP_URL})"
# generate our new hostname
new_hostname="${instance_name_prefix}-${_instanceid/i-/}"
:;
else
# we're not in cloud
new_hostname="${instance_name_prefix}"
:;
fi
hostnamectl set-hostname "${new_hostname}"
hostnamectl set-hostname --static "${new_hostname}"
hostnamectl
#!/bin/bash
# /var/lib/cloud/scripts/per-boot/00-hostname.sh
# https://cloudinit.readthedocs.io/en/latest/topics/modules.html#scripts-per-boot
#
# create/set server hostname using the "Name" tag/label and
# append the cloud instance id
#
# eg: <name tag/label>-<instance_id>
#
AWS_URL="http://169.254.169.254/latest/meta-data/"
GCP_URL="http://169.254.169.254/computeMetadata/v1/instance/"
if curl --silent --output /dev/null --fail --connect-timeout 10 ${AWS_URL}; then
# we're running in AWS
# retrive our identity document
_identity_document=$(curl -s http://169.254.169.254/latest/dynamic/instance-identity/document)
# get region from identity doc
_region=$(jq -r .region <<< "${_identity_document}")
# get instance_id from identity doc
_instanceid="$(jq -r .instanceId <<< "${_identity_document}")"
# get the value of the "Name" tag
_tags=$(aws ec2 describe-tags --filter "Name=resource-id,Values=${_instanceid}" --region "${_region}")
_tag_name=$(jq -r '.Tags[] | select(.Key=="Name") | .Value' <<< "${_tags}")
# construct a "nice, unique hostname" using the value of name tag and instance id
new_hostname="${_tag_name}-${_instanceid/i-/}"
elif curl --silent --output /dev/null --fail --connect-timeout 10 ${GCP_URL} --header "Metadata-Flavor: Google"; then
# we're running in GCP
# get our instance id
_instanceid=$(curl -s -H 'Metadata-Flavor: Google' http://metadata.google.internal/computeMetadata/v1/instance/id)
# get our instance name (from tag)
_name=$(curl -s -H 'Metadata-Flavor: Google' http://metadata.google.internal/computeMetadata/v1/instance/name)
# construct a "nice, unique hostname" using the value of name label and instance id
new_hostname="${_name}-${_instanceid/i-/}"
:;
else
# we're not in cloud
exit 0
:;
fi
hostnamectl set-hostname "${new_hostname}"
hostnamectl set-hostname --static "${new_hostname}"
hostnamectl
EOF
# prevent cloud-init from resetting your instance name
# For those times when you absolutely, positively need to run your datacentre in the cloud!
# https://cloudinit.readthedocs.io/en/latest/topics/modules.html#set-hostname
# https://cloudinit.readthedocs.io/en/latest/topics/modules.html#update-hostname
preserve_hostname: true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment