Cloud-init with jinja template not rendering variables correct on debian/11/cloud image

This may be more of a cloud-init bug, but I thought I would ask if anyone has run into this issue.

I run lxd profiles with cloud-init (#cloud-config) configurations, and I use jinja2 templating (## template: jinja) so I can use one profile that I can apply to many distros. It works great, except on some images I notice ignore the jinja2 template directives.

When I launch a new debian/11/cloud image, I noticed it does not adhere to the jinja2 template.

My profile begins like:

  user.user-data: |
    ## template: jinja
{% if distro == 'centos' or 'rocky' -%}
    ~Do some stuff.....
{%- endif %}
{% if distro == 'debian' or 'ubuntu' -%}
    ~Do some stuff.....
{%- endif %}

On the debian/11/cloud image, it just runs the first cloud-init block below user.user-data: |
and ignores the jinja directives.

The image has cloud-init 20.4.1, and has the jinja2 engine installed. So I don’t understand why it would not adhere to the jinja directives in the profile like all other distros do.

As a work around, I just have a rudimentary cloud-init config in a separate profile that I assign to these debian containers.

When I query cloud-init with:

cloud-init query --format "$(sudo cloud-init query userdata)"

I see ALL cloud-init settings apply, even ones for other distros, as if it just runs all directives.

If anyone has seen this, and knows the fix, please share.


Probably worth looking in /var/lib/cloud/seed to see exactly what was passed to cloud-init in the first place.

Thanks Stéphane.

I checked, the user-data file in /var/lib/cloud/seed/nocloud-net/ is exactly as it is in the profile, so that seems to pass correctly.

If I issue: cloud-init query -f {{distro}} - I get the correct metadata variable of “debian”.

If I issue: cloud-init devel render /var/lib/cloud/seed/nocloud-net/user-data -d

it renders the entire user-data config, not just the jinja template variable like I normally see. It just seems to ignore the variable.

Ok, so that’s most likely a cloud-init bug if the data in nocloud-net is correct.

We’re in the process of moving to using a native cloud-init datasource for LXD which may change things a bit more, but we’re rolling it out slowly as not all distros have cloud-init versions supporting this yet.

1 Like

Update: Turned out to be some bad formatting the varibles. Got it working using the cloud-init query function to test different directives until it all worked.

1 Like