Skip to content

[bug] cloud-init-local.service crashes with AttributeError on failed jinja template render #7007

Description

@rajibade

Bug report

Trying to render a jinja template file with nested instance-data attributes that isn't provided by the active DataSource fails the init stage and prevent cloud-init from starting up.

render_jinja_payload_from_file() returns a None value when it can't render a template that isn't syntax related. util.read_conf() doesn't happen to have any checks for this None return and instead passes it to the load_yaml() function which tries to run decode_binary(None) and raises the AttributeError.

Tracing back. This seems this was removed in #5350 due to mypy warnings as the render functions had no return annotations.

Steps to reproduce the problem

Have a 'None' data source and inspect cloud-init-main.service

Environment details

  • Cloud-init version: 26.1 (Reproduced on main branch as well)
  • Operating System Distribution: Any fedora-derived distro (But running a custom Amazon Linux KVM image here)
  • Cloud provider, platform or installer type: None
  1. Create /etc/cloud/cloud.cfg.d/10_test.cfg:
     ## template: jinja
     #cloud-config
     write_files:
       - content: |
           {{ ds.meta_data.placement.region | default("fallback") }}
         path: /tmp/out
  1. Boot on a platform with NoCloud/None datasource where ds.meta_data.placement isn't available. I thought the default() would help but it didn't get triggered.

  2. Check the logs.

cloud-init logs

2026-08-16 12:32:28,652 - jinja_template.py[WARNING]: Ignoring jinja template for /etc/cloud/cloud.cfg.d/10_test.cfg: 'dict object' has no attribute 'placement'
2026-08-16 12:32:28,652 - main.py[ERROR]: failed stage init
Traceback (most recent call last):
  File "/usr/lib/python3.14/site-packages/cloudinit/cmd/main.py", line 967, in status_wrapper
    ret = functor(name, args)
  File "/usr/lib/python3.14/site-packages/cloudinit/cmd/main.py", line 582, in main_init
    iid = init.instancify()
  File "/usr/lib/python3.14/site-packages/cloudinit/stages.py", line 568, in instancify
    return self._reflect_cur_instance()
           ~~~~~~~~~~~~~~~~~~~~~~~~~~^^
... <traces>
  File "/usr/lib/python3.14/site-packages/cloudinit/util.py", line 1142, in read_conf_with_confd
    confd_cfg = read_conf_d(confd, instance_data_file=instance_data_file)
  File "/usr/lib/python3.14/site-packages/cloudinit/util.py", line 1086, in read_conf_d
    read_conf(
    ~~~~~~~~~^
        path,
        ^^^^^
        instance_data_file=instance_data_file,
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "/usr/lib/python3.14/site-packages/cloudinit/util.py", line 359, in read_conf
    return load_yaml(config_file, default={})  # pyright: ignore
  File "/usr/lib/python3.14/site-packages/cloudinit/util.py", line 976, in load_yaml
    blob = decode_binary(blob)
  File "/usr/lib/python3.14/site-packages/cloudinit/util.py", line 143, in decode_binary
    return blob if isinstance(blob, str) else blob.decode(encoding=encoding)
                                              ^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'decode'
failed run of stage init
------------------------------------------------------------
...
<Journal output>
Aug 16 12:32:28 localhost cloud-init[3025]: 2026-08-16 12:32:28,773 - socket.py[CRITICAL]: AttributeError("'NoneType' object has no attribute 'decode'") in <frame at 0x7f1f8b628440, file '/usr/lib/python3.14/site-packages/cloudinit/cmd/main.py', line 1340, code all_stages>
Aug 16 12:32:28 localhost systemd[1]: cloud-init-main.service: Main process exited, code=exited, status=1/FAILURE
Aug 16 12:32:28 localhost systemd[1]: cloud-init-main.service: Failed with result 'exit-code'.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working correctlynewAn issue that still needs triage

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions