What does the following show?
head -n 1 /var/log/cloud-init.log
Without the defer statement I see a similar exception on 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
If I add defer, it works as expected:
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
Note that the "Additional properties" warning does not exist in my log. This warning indicates that you've used a key in your cloud-config that the jsonschema validator doesn't know about. In this case that indicates that the running version predates the defer keyword.
Note that in the traceback that you reported, getpwname is on line 1380.
The most recent version of cloud-init on 20.04 that this is true for is release 21.2.
By release 21.3, that call to getpwname has been pushed back to line 1429.
Based on the above information, I would assume that cloud-init ran at a version that didn't support defer and was upgraded afterwords to 21.4.
If you want to try rerunning all the cloud-init stages, you can always try a cloud-init clean --logs followed by a reboot. This should remove cloud-init artifacts. You may find the lxc commands above are helpful for trying out modules locally prior to running in a cloud.