在EC2上使用cloud-init和实例标签设置主机名?

问题描述 投票:0回答:1

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

类似这样的方法?

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

除了使用实例标签,比如 "Name "或 "hostname"。 我看过一些使用Jinja模板的例子,似乎是正确的思路。但我不知道如何在用户数据云-init中引用实例标签。如果这样做更有意义,可以烘焙到AMI中,我不介意--它不一定是用户数据。然而,我目前正在运行一个 hostmod.sh 的 shell 脚本,读取标签并设置主机名。

## template: jinja
preserve_hostname: false
hostname: {% tags.hostname %}
fqdn: {% tags.hostname %}.local
manage_etc_hosts: true
amazon-ec2 user-data cloud-init
1个回答
1
投票

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

#!/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 运行脚本。

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

我还试过通过云配置在用户数据中创建整个脚本。这也可以,但我把文件拆分出来,以便在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的原因。我以后可能会添加一个类似的模板来更新主机名。


0
投票

这个脚本将允许你更新在亚马逊Linux (v1)主机名中配置的 /etc/sysconfig/network - 这两部分都是正确配置主机名所必需的。

#! /bin/bash
#
export EC2_AVAIL_ZONE=`curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone` 
export EC2_REGION="`echo \"$EC2_AVAIL_ZONE\" | sed 's/[a-z]$//'`"
export ec2id=$(curl http://169.254.169.254/latest/meta-data/instance-id )
export ec2name=$(aws ec2 describe-tags --filters "Name=resource-id,Values=$ec2id" "Name=key,Values=Name" --region $EC2_REGION | awk '/"Value":/ {print $2}' | tr -d '",')
#
hostname ${ec2name}
cat > /etc/sysconfig/network << EOF
NETWORKING=yes
HOSTNAME=${ec2name}
NOZEROCONF=yes
EOF 
© www.soinside.com 2019 - 2024. All rights reserved.