Ubuntu

cloud-init.log 顯示 write_files.0:不允許附加屬性(“延遲”是意外的)

  • March 17, 2022

我的 cloud-init 中有一個非常簡單的 stansa,

write_files:
 - path: /home/ecarroll/.zshrc
   content: |
     source ~/powerlevel10k/powerlevel10k.zsh-theme
   owner: ecarroll:ecarroll
   permissions: '0644'
   defer: true

defer標誌記錄在Cloud Init: Write Flags文件中,

defer: (boolean) 推遲寫入文件直到“最終”階段,在創建使用者和安裝包之後。預設值: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 給ecarrollcloud-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 上。

您需要更新到 cloud-init v21.4,這是將 defer 屬性添加到 write_files 的版本。

檢查您正在執行的版本

  • cloud-init -v, 與
  • head -n 1 /var/log/cloud-init.log見上面布雷特的回答

如果這兩個不一致,您可能需要查看為什麼日誌文件中的 cloud-init 版本與二進製文件中的版本衝突?

下面顯示什麼?

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

請注意,我的日誌中不存在“其他屬性”警告。此警告表明您在 cloud-config 中使用了 jsonschema 驗證器不知道的密鑰。在這種情況下,這表明正在執行的版本早於defer關鍵字。

請注意,在您報告的回溯中,getpwname位於第 1380 行。

20.04 上最新的 cloud-init 版本是21.2 版本

21.3 版,該呼叫getpwname已被推回第 1429 行。

基於以上資訊,我假設 cloud-init 執行的版本不支持defer,後來升級到 21.4。

如果您想嘗試重新執行所有 cloud-init 階段,您始終可以嘗試 acloud-init clean --logs然後重新啟動。這應該刪除 cloud-init 工件。您可能會發現上述lxc命令有助於在雲中執行之前在本地試用模組。

引用自:https://unix.stackexchange.com/questions/692871