A few years ago, I wrote a patch to fix this problem. Upstream reported it was fixed in 22.2 but the code is actually still the same buggy line it was before. Please consider applying the patch provided on Launchpad/Github. Reproducible: Always
Added comment on upstream Github issue but given how many years it took them to do anything about it in the first place, I don't have high hopes it will be noticed.
This issue was tracked in GitHub here: ttps://github.com/canonical/cloud-init/issues/3260 The corresponding PR: https://github.com/canonical/cloud-init/pull/4422 Was applied to 23.4.