这是我的对流层文件的相关部分:
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 时,如何在实例上设置主机名?