【问题标题】:List vs string in cloud init runcmd云初始化运行命令中的列表与字符串
【发布时间】:2021-09-01 16:43:02
【问题描述】:

official example page 上,云配置描述使用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

【问题讨论】:

    标签: cloud-init


    【解决方案1】:

    两者都不是更好,但字符串形式会更直接。 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'
    

    【讨论】:

    • 该路径是在虚拟机上还是在运行multipass/cloud config的主机上?
    • 虚拟机(我这里是容器)
    猜你喜欢
    • 2017-10-23
    • 1970-01-01
    • 1970-01-01
    • 2012-07-18
    • 2015-09-18
    • 2018-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多