Skip to content

fix: prevent traceback in util.get_hostname_fqdn when hostname is numeric - #7014

Open
nabil-rady wants to merge 1 commit into
canonical:mainfrom
nabil-rady:fix/fix-issue-with-integer-hostname
Open

fix: prevent traceback in util.get_hostname_fqdn when hostname is numeric#7014
nabil-rady wants to merge 1 commit into
canonical:mainfrom
nabil-rady:fix/fix-issue-with-integer-hostname

Conversation

@nabil-rady

@nabil-rady nabil-rady commented Aug 19, 2026

Copy link
Copy Markdown

Proposed Commit Message

fix: prevent traceback in util.get_hostname_fqdn when hostname is numeric

Catch the AttributeError thrown in util.get_hostname_fqdn and return empty hostname and fqdn to prevent cloud-init from crashing due to bad schema and log a warning instead.

Fixes GH-7006

Additional Context

Reproduced the error using QEMU and the following user-data file

#cloud-config
hostname: 123456
password: password
chpasswd:
  expire: False

Fixes #7007

Test Steps

tox -e py3

Merge type

  • Squash merge using "Proposed Commit Message"
  • Rebase and merge unique commits. Requires commit messages per-commit each referencing the pull request number (#<PR_NUM>)

@nabil-rady
nabil-rady force-pushed the fix/fix-issue-with-integer-hostname branch from 5cfc3f4 to e7ee608 Compare August 19, 2026 22:24
Comment thread cloudinit/util.py Outdated
hostname = get_cfg_option_str(cfg, "hostname", fqdn.split(".")[0])
else:
if "hostname" in cfg and cfg["hostname"].find(".") > 0:
hostname = str(cfg["hostname"]) if "hostname" in cfg else None

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like a weird place to address the error. The schema defines this key as a string - how did it get interpreted as an integer in the first place?

I'm concerned that by trying to fix it here it misses bugs in other parts of the code.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I looked at this discussion, the fqdn fix makes sense and it has context to it, unlike this fix; as quoting the hostname fixes the issue. Would you suggest ignoring this issue or maybe moving the _maybe_set_hostname to the try/except block ?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better if cloud-init produced an error log rather than a traceback, but this is the expected behavior: if you pass an integer, cloud-init just doesn't know what to do with it. The cloud-init schema command shows that this configuration isn't valid.

@nabil-rady nabil-rady Aug 22, 2026

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@holmanb returned empty hostname and fqdn when hostname is numeric. Running cloud-init status --long produced these warnings.

I am concerned whether we should fallback to cloud in case of a numeric hostname or not.

WARNING:
	- cloud-config failed schema validation! You may run 'sudo cloud-init schema --system' to check the details.
	- Invalid hostname type: expected string, got int
	- Failed to non-persistently adjust the system hostname to 
	- Invalid hostname type: expected string, got int
	- cloud-config failed schema validation! You may run 'sudo cloud-init schema --system' to check the details.
	- Invalid hostname type: expected string, got int
	- Failed to non-persistently adjust the system hostname to 
	- Invalid hostname type: expected string, got int
	- cloud-config failed schema validation! You may run 'sudo cloud-init schema --system' to check the details.
	- Invalid hostname type: expected string, got int
	- Failed to non-persistently adjust the system hostname to 
	- Invalid hostname type: expected string, got int
	- cloud-config failed schema validation! You may run 'sudo cloud-init schema --system' to check the details.
	- Invalid hostname type: expected string, got int
	- Failed to non-persistently adjust the system hostname to 
	- Invalid hostname type: expected string, got int

@nabil-rady
nabil-rady force-pushed the fix/fix-issue-with-integer-hostname branch from e7ee608 to 1fa2973 Compare August 22, 2026 13:31
@nabil-rady nabil-rady changed the title fix: treat numeric hostname as string fix: prevent traceback in util.get_hostname_fqdn when hostname is numeric Aug 22, 2026
@nabil-rady
nabil-rady force-pushed the fix/fix-issue-with-integer-hostname branch from 1fa2973 to 2c06c86 Compare August 22, 2026 13:35
Catch the AttributeError thrown in util.get_hostname_fqdn and return empty hostname and fqdn to prevent cloud-init from crashing due to schema error and log a warning instead
@nabil-rady
nabil-rady force-pushed the fix/fix-issue-with-integer-hostname branch from 2c06c86 to 334e13d Compare August 22, 2026 15:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

error handling issue: cloud-init produces a traceback on invalid user configuration

2 participants