【问题标题】:Unable to set ASG instance hostnames using Troposphere/CloudFormation UserData无法使用 Troposphere/CloudFormation UserData 设置 ASG 实例主机名
【发布时间】:2023-04-05 01:49:02
【问题描述】:

这是我的对流层文件的相关部分:

LaunchConfiguration = t.add_resource(LaunchConfiguration(
    "LaunchConfigA",
    ImageId=UBUNTU_IMG,
    SecurityGroups=[Ref(SecurityGroup)],
    InstanceType="m3.medium",
    UserData=Base64(Join('', [
        "#cloud-boothook\n",
        "#!/bin/bash\n",
        "sudo hostname test\n",
        "sudo sh -c 'echo test > /etc/hostname'\n",
        "sudo sh -c 'echo 127.0.0.1 test >> /etc/hosts'\n",
        "sudo touch /var/log/TESTING\n"
    ])),
))

AutoScalingGroupA = t.add_resource(AutoScalingGroup(
    "GroupA",
    AvailabilityZones=GetAZs(Ref(AWS_REGION)),
    LaunchConfigurationName=Ref(LaunchConfiguration),
    MinSize="1",
    DesiredCapacity="2",
    MaxSize="2",
))

当我从这个模板创建一个全新的 CloudFormation 堆栈时,实例上的主机名看起来像 ip-172-XXX-XXX-XXX,这是默认值。

我确定脚本正在运行,因为我的TESTING 文件:

atrose@ip-172-31-32-40:~$ ls -la /var/log/TESTING
-rw-r--r-- 1 root root 0 Jul 14 20:10 /var/log/TESTING

如果我手动运行脚本,则主机名设置正确。像这样:

atrose@ip-172-31-32-40:~$ hostname
ip-172-31-32-40

atrose@ip-172-31-32-40:~$ sudo cat /var/lib/cloud/instance/user-data.txt
#cloud-boothook
#!/bin/bash
sudo hostname test
sudo sh -c 'echo test > /etc/hostname'
sudo sh -c 'echo 127.0.0.1 test >> /etc/hosts'

atrose@ip-172-31-32-40:~$ sudo bash /var/lib/cloud/instance/user-data.txt

atrose@ip-172-31-32-40:~$ hostname
test

如何在实例首次启动到 ASG 时为其设置主机名?

【问题讨论】:

    标签: amazon-web-services amazon-ec2 amazon-cloudformation cloud-init troposphere


    【解决方案1】:

    看起来您正在使用 Ubuntu AMI,这意味着 CloudInit 应该有一个内置的主机名参数,并且您不需要 shell 脚本来执行您想要的操作。我猜测 cloudinit 本身正在与您的脚本发生冲突。你应该看看这个:

    http://bazaar.launchpad.net/~cloud-init-dev/cloud-init/trunk/view/head:/doc/examples/cloud-config.txt#L540

    如果您对如何使用它有任何疑问,请告诉我。谢谢!

    【讨论】:

      猜你喜欢
      • 2017-04-16
      • 1970-01-01
      • 2019-11-04
      • 2012-01-14
      • 1970-01-01
      • 1970-01-01
      • 2015-02-14
      • 1970-01-01
      相关资源
      最近更新 更多