雲初始化支持此現在本身,參見RUNCMD VS的文檔中bootcmd命令描述(http://cloudinit.readthedocs.io/en/latest/topics/examples.html#run-commands-on-first-boot):
「RUNCMD」:
#cloud-config
# run commands
# default: none
# runcmd contains a list of either lists or a string
# each item will be executed in order at rc.local like level with
# output to the console
# - runcmd only runs during the first boot
# - if the item is a list, the items will be properly executed as if
# passed to execve(3) (with the first arg as the command).
# - if the item is a string, it will be simply written to the file and
# will be interpreted by 'sh'
#
# Note, that the list has to be proper yaml, so you have to quote
# any characters yaml would eat (':' can be problematic)
runcmd:
- [ ls, -l,/]
- [ sh, -xc, "echo $(date) ': hello world!'" ]
- [ sh, -c, echo "=========hello world'=========" ]
- ls -l /root
- [ wget, "http://slashdot.org", -O, /tmp/index.html ]
「bootcmd」:
#cloud-config
# boot commands
# default: none
# this is very similar to runcmd, but commands run very early
# in the boot process, only slightly after a 'boothook' would run.
# bootcmd should really only be used for things that could not be
# done later in the boot process. bootcmd is very much like
# boothook, but possibly with more friendly.
# - bootcmd will run on every boot
# - the INSTANCE_ID variable will be set to the current instance id.
# - you can use 'cloud-init-per' command to help only run once
bootcmd:
- echo 192.168.1.130 us.archive.ubuntu.com >> /etc/hosts
- [ cloud-init-per, once, mymkfs, mkfs, /dev/vdb ]
也記錄了bootcmd中的「cloud-init-per」命令示例。從它的幫助:
Usage: cloud-init-per frequency name cmd [ arg1 [ arg2 [ ... ] ]
run cmd with arguments provided.
This utility can make it easier to use boothooks or bootcmd
on a per "once" or "always" basis.
If frequency is:
* once: run only once (do not re-run for new instance-id)
* instance: run only the first boot for a given instance-id
* always: run every boot
>我希望增加修改配置部分的功能。 現在是否已添加此功能?我發現最新的cloud-init中有一個「合併」功能,但我不知道如何使用它來只改變'scripts-user'這一行。無論我通過的選項如何,它都會覆蓋整個列表。 – Meta
下面是一個在線修改的代碼:'sed -i's/scripts-user $/\ [scripts-user,always \] /'/ etc/cloud/cloud.cfg' – wjordan
將文件放入'/ var/lib/cloud/scripts/per-boot'似乎更容易,我可以使用它來設置[auto ec2 shutdown](http://stackoverflow.com/a/38186787/4058484)。 – hyip