使用 cloud-init 和实例标签在 EC2 上设置主机名? [英] Set hostname on EC2 using cloud-init and instance tags?

查看:59
本文介绍了使用 cloud-init 和实例标签在 EC2 上设置主机名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法使用实例标签中的值来设置 EC2 实例上的主机名,在用户数据中使用 cloud-init?

Is there a way to use the values from instance tags to set the hostname on an EC2 instance, using cloud-init in user-data?

类似的东西?

#cloud-config
preserve_hostname: false
hostname: my-custom-hostname
fqdn: my-custom-hostname.local
manage_etc_hosts: true

除非使用实例标记,例如名称"或主机名".我看过一些使用 Jinja 模板的例子,这似乎是正确的.但我不确定是否/如何在 user-data/cloud-init.xml 中引用实例标签.如果这更适合融入 AMI,我不介意 - 它不必是用户数据.但是,我目前正在实例启动时运行 hostmod.sh shell 脚本,该脚本读取标签并设置主机名.

Except using an instance tag, such a "Name" or "hostname". I've seen some examples using Jinja templates, which seems to be along the right lines. But I'm not sure if/how to reference the instance tags in the user-data/cloud-init. If this makes more sense to bake into the AMI, I don't mind that - it doesn't have to be user-data. However, I currently am running a hostmod.sh shell script at instance launch that reads the tags, and sets the hostname.

## template: jinja
preserve_hostname: false
hostname: {% tags.hostname %}
fqdn: {% tags.hostname %}.local
manage_etc_hosts: true

推荐答案

对我有用的一个想法是使用 shell 参数扩展(我认为这是正确的术语).我仍在维护我自己的脚本,嵌入到我的 AMI 中,但与我以前使用 sed 修改 /etc/hosts 文件的脚本相比,我更喜欢类似模板的方法.

One idea that worked for me was use shell parameter expansion (I think that's the right term). I am still maintaining my own script, baked into my AMI, but I prefer the template-like approach versus my previous script that used sed to modify the /etc/hosts file.

#!/bin/bash

# Get Name tag from AWS instance metadata
ec2id=$(curl http://169.254.169.254/latest/meta-data/instance-id)
hostname=$(aws ec2 describe-tags --filters "Name=resource-id,Values=$ec2id" "Name=key,Values=Name" --region us-east-1 | awk '/"Value":/ {print $2}' | tr -d '",')
fqdn="${hostname}.local"

# Write a new hosts file using variable expansion
cat >/etc/hosts <<EOF
# The following lines are desirable for IPv4 capable hosts
127.0.0.1 ${fqdn} ${hostname}
127.0.0.1 localhost.localdomain localhost
127.0.0.1 localhost4.localdomain4 localhost4
# The following lines are desirable for IPv6 capable hosts
::1 ${fqdn} ${hostname}
::1 localhost.localdomain localhost
::1 localhost6.localdomain6 localhost6
EOF

我通过用户数据中定义的 cloud-init 运行脚本:

I run the script via cloud-init defined in user data:

#cloud-config
runcmd:
  - bash /usr/local/bin/hostmod.sh

我还尝试通过 cloud-config 在用户数据中创建整个脚本.这也有效,但我将文件拆分为更容易在 Ansible 中单独管理.

I also tried creating the entire script within user data via cloud-config. This works as well, but I split the file out to make it easier to manage separately in Ansible.

#cloud-config
write_files:
  - path: /usr/local/bin/hostmod.sh
    permissions: 0744
    owner: root
    content: |
      #!/bin/bash

      ec2id=$(curl http://169.254.169.254/latest/meta-data/instance-id)
      hostname=$(aws ec2 describe-tags --filters "Name=resource-id,Values=$ec2id" "Name=key,Values=Name" --region us-east-1 | awk '/"Value":/ {print $2}' | tr -d '",')
      fqdn="${hostname}.local"

      cat >/etc/hosts <<EOF
      # The following lines are desirable for IPv4 capable hosts
      127.0.0.1 ${fqdn} ${hostname}
      127.0.0.1 localhost.localdomain localhost
      127.0.0.1 localhost4.localdomain4 localhost4
      # The following lines are desirable for IPv6 capable hosts
      ::1 ${fqdn} ${hostname}
      ::1 localhost.localdomain localhost
      ::1 localhost6.localdomain6 localhost6
      EOF

runcmd:
  - bash /usr/local/bin/hostmod.sh

这显然不会改变主机名,它更依赖于平台.这就是为什么我从一开始就更喜欢使用 cloud-init.我稍后可能会添加一个类似的模板来更新主机名.

This obviously doesn't change the hostname, which is a bit more platform dependent. That is why I preferred to use cloud-init from the start. I may later add a similar template for updating the hostname.

这篇关于使用 cloud-init 和实例标签在 EC2 上设置主机名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆