云init runcmd中的列表与字符串



在官方示例页面上,cloud-config描述了使用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
# Note: Don't write files to /tmp from cloud-init use /run/somedir instead.
# Early boot environments can race systemd-tmpfiles-clean LP: #1707222.
- mkdir /run/mydir
- [ wget, "http://slashdot.org", -O, /run/mydir/index.html ]

现在我的问题是,当传递命令时,列表形式是否与字符串形式不同

这是吗

[sudo, ufw, --force, enable]

比这更正确?

sudo ufw --force enable

两者都不好,但字符串形式会更简单。runcmd下的所有内容都被写成一个稍后要执行的脚本。如果它被指定为列表,它会被引用,但这应该不是问题。

如果您想了解它是如何编写的,您应该能够找到在/var/lib/cloud/instance/scripts/runcmd中生成的脚本。例如,我看到:

root@me:~# cat /var/lib/cloud/instance/scripts/runcmd 
#!/bin/sh
'ls' '-l' '/'
'sh' '-xc' 'echo $(date) ''': hello world!''''
'sh' '-c' 'echo "=========hello world'''========="'
ls -l /root
mkdir /run/mydir
'wget' 'http://slashdot.org' '-O' '/run/mydir/index.html'

最新更新