我的云初始化中有一个非常简单的斯坦萨,
write_files:
- path: /home/ecarroll/.zshrc
content: |
source ~/powerlevel10k/powerlevel10k.zsh-theme
owner: ecarroll:ecarroll
permissions: '0644'
defer: true
该defer
标志记录在云初始化:写标志文档,
defer:(布尔值)将文件写入推迟到创建用户并安装软件包之后的“最终”阶段。默认:
False
。
但是当我像上面那样使用它时,我得到了
schema.py[WARNING]: Invalid config: write_files.0: Additional properties are not allowed ('defer' was unexpected)
util.py[DEBUG]: Writing to /home/ecarroll/.zshrc - wb: [644] 47 bytes
handlers.py[DEBUG]: finish: init-network/config-write-files: FAIL: running config-write-files with frequency once-per-instance
util.py[WARNING]: Running module write-files (<module 'cloudinit.config.cc_write_files' from '/usr/lib/python3/dist-packages/cloudinit/config/cc_write_files.py'>) failed
util.py[DEBUG]: Running module write-files (<module 'cloudinit.config.cc_write_files' from '/usr/lib/python3/dist-packages/cloudinit/config/cc_write_files.py'>) failed
我需要的实际原因defer
可以在跟踪中找到。这是因为我需要将文件 chown'd 给ecarroll
cloud-init 尚未创建的用户 ( ),
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/cloudinit/util.py", line 1380, in chownbyname
uid = pwd.getpwnam(user).pw_uid
KeyError: "getpwnam(): name not found: 'ecarroll'"
我使用的是 Ubuntu 20.04.4 LTS。
答案1
您需要更新到cloud-init v21.4,该版本为write_files添加了defer属性。
检查您正在运行的版本
cloud-init -v
, 与head -n 1 /var/log/cloud-init.log
请参阅上面布雷特的回答
如果这两者不同意,您可能需要检查一下为什么日志文件中的 cloud-init 版本与二进制文件中的版本冲突?
答案2
下面显示什么?
head -n 1 /var/log/cloud-init.log
如果没有 defer 语句,我会在 Ubuntu 20.04 cloud-init 22.1 上看到类似的异常:
arc~ cat ~/my-user-data
#cloud-config
users:
- default
- name: ecarroll
write_files:
- path: /home/ecarroll/.zshrc
content: |
test content
owner: ecarroll:ecarroll
permissions: '0644'
arc~ lxc init ubuntu:focal ecarroll -c user.user-data="$(cat ~/my-user-data)"
Creating ecarroll
arc~ lxc start ecarroll
arc~ lxc exec ecarroll -- grep -A12 Traceback /var/log/cloud-init.log
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/cloudinit/util.py", line 1430, in chownbyname
uid = pwd.getpwnam(user).pw_uid
KeyError: "getpwnam(): name not found: 'ecarroll'"
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/cloudinit/stages.py", line 976, in _run_modules
ran, _r = cc.run(run_name, mod.handle, func_args,
File "/usr/lib/python3/dist-packages/cloudinit/cloud.py", line 54, in run
return self._runners.run(name, functor, args, freq, clear_on_fail)
File "/usr/lib/python3/dist-packages/cloudinit/helpers.py", line 186, in run
results = functor(*args)
File "/usr/lib/python3/dist-packages/cloudinit/config/cc_write_files.py", line 205, in handle
write_files(name, filtered_files)
File "/usr/lib/python3/dist-packages/cloudinit/config/cc_write_files.py", line 245, in write_files
util.chownbyname(path, u, g)
File "/usr/lib/python3/dist-packages/cloudinit/util.py", line 1434, in chownbyname
raise OSError("Unknown user or group: %s" % (e)) from e
arc~ lxc exec ecarroll -- grep "Additional properties" /var/log/cloud-init.log
arc~ lxc stop ecarroll && lxc rm ecarroll
如果我添加延迟,它会按预期工作:
arc~ cat ~/my-user-data
#cloud-config
users:
- default
- name: ecarroll
write_files:
- path: /home/ecarroll/.zshrc
content: |
test content
owner: ecarroll:ecarroll
permissions: '0644'
defer: true
arc~ lxc init ubuntu:focal ecarroll -c user.user-data="$(cat ~/my-user-data)"
Creating ecarroll
arc~ lxc start ecarroll
arc~ lxc exec ecarroll -- grep -A12 Traceback /var/log/cloud-init.log
arc~ lxc exec ecarroll -- grep -A12 "Additional properties" /var/log/cloud-init.log
arc~ lxc shell ecarroll
root@ecarroll:~# cat /home/ecarroll/.zshrc
test content
root@ecarroll:~# exit
logout
arc~ lxc stop ecarroll && lxc rm ecarroll
请注意,我的日志中不存在“其他属性”警告。此警告表明您在云配置中使用了 jsonschema 验证器不知道的密钥。在这种情况下,这表明正在运行的版本早于defer
关键字。
请注意,您报告的回溯中,getpwname
位于第 1380 行。
20.04 上的最新版本的 cloud-init 是这样的版本21.2。
经过版本21.3,该调用getpwname
已被推回到第 1429 行。
根据以上信息,我假设 cloud-init 运行在不支持的版本上,defer
并随后升级到了 21.4。
如果您想尝试重新运行所有 cloud-init 阶段,您可以随时尝试cloud-init clean --logs
然后重新启动。这应该会删除 cloud-init 工件。您可能会发现lxc
上面的命令对于在云中运行之前在本地尝试模块很有帮助。