Lxd profile cloud-init jinja errors

Hello,

I am getting an error in one of my projects, creating an lxd profile from a templated cloud-init (user-data) file. I am on ubuntu 20.04, with lxd 4.0.9 for reference.

Here is the error:

2022-11-17 01:57:19,902 - util.py[WARNING]: Failed loading yaml blob. Invalid format at line 2 column 1: "while parsing a block mapping
  in "<unicode string>", line 2, column 1:
    users:
    ^
expected <block end>, but found '<block mapping start>'
  in "<unicode string>", line 18, column 5:
        repo_update: true
        ^"
2022-11-17 01:57:19,903 - cloud_config.py[WARNING]: Failed at merging in cloud config part from part-001: empty cloud config

If I use the cloud-init.cfg files separate, they work fine, just seems to be an issue on my syntax perhaps? I have checked my indentation on a few rounds with the cloud-init config files.

Here is the cloud-init cfg file that I import to lxd profile.

## template: jinja
#cloud-config
{% if v1.distro == 'ubuntu' -%}
    users:
      - name: jonny
        groups: sudo
        lock_passwd: false
        passwd: $6$exyxyxyxzzzzABC/     
        ssh_authorized_keys:
          - ssh-ed25519 XYZ-some key RJ-ed25519-ansible
        shell: /bin/bash
      - name: ansible
        groups: sudo
       lock_passwd: false
       passwd: $6xxxxxxxx/        
       ssh_authorized_keys:
         - ssh-ed25519 xyz123   AN-ed25519-ansible
       shell: /bin/bash
    package_update: true
    package_upgrade: true
    packages:
      - build-essential
    locale: en_US.UTF-8
    timezone: America/New_York
    runcmd:
      - mkdir /mnt/v-nas
     - date > /etc/birth_certificate
    packages:
      - htop
{%- endif %}
{% if v1.distro == 'centos' -%}
    users:
      - name: mike
        groups: wheel
        lock_passwd: false
        passwd: $6$e123ABCxxx/     
        ssh_authorized_keys:
          - ssh-ed25519 xyzABC..    DJ-ed25519-ansible
        shell: /bin/bash
      - name: ansible
        gecos: Ansible User
        groups: users,admin,wheel
        sudo: ALL=(ALL) NOPASSWD:ALL
        shell: /bin/bash
        lock_passwd: true
        ssh_authorized_keys:
          - ssh-ed25519 xxxxxxxxxxxxxxxxxxxxx  XX-ed25519-ansible
    repo_update: true
    repo_upgrade: all
    yum_repos:
      epel-release:
        name: Extra Packages for Enterprise Linux 9 - Release
        baseurl: http://download.fedoraproject.org/pub/epel/9/Everything/$basearch
        enabled: true
        failovermethod: priority
        gpgcheck: true
        gpgkey: http://download.fedoraproject.org/pub/epel/RPM-GPG-KEY-EPEL-9
    package_upgrade: true
    packages:
      - htop
    locale: en_US.UTF-8
    timezone: America/New_York
    runcmd:
      - mkdir /mnt/v-nas
      - date > /etc/birth_certificate
{%- endif %}

When I remove the lines at “repo_update…” and below, the config applies fine.

I am adding these cfg files to the lxd profile with:
lxc profile set profilename user.user-data - < cloud-init.cfg

Is there some special formatting we need when using jinja2 templating? I don’t understand why the same formatting works fine in a different profile without the jinja strings. Maybe my syntax is wrong, but when I was only adding the user accounts, all profiles worked fine across all my projects. It’s when I added the package directives it started giving errors in /var/log/cloud-init.log. Can share the logs if it will help.

Thank you, hoping someone has run into this, and can school me on my ignorance. :slight_smile:

Update: Looks like whitespace in my cfg files got me again. After checking my formatting a few times, I finally have success.

‘cloud-init devel render’ would keep giving error about the first line issue (must begin with #cloud-config), as well as ‘cloud-init devel schema --system --annotate’ giving same error so it confused me for a bit, but it seemed to just be an issue with indentation (as usual). Have a great day!