jinja-template doesn't support 'do' extension.

Bug #1962759 reported by paul bruno
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
cloud-init
Fix Released
Medium
paul bruno

Bug Description

example user-data file with jinja

## template: jinja
#!/bin/sh

{% set data_result = [] %}
{% set data_input = [1,2,3] %}
{% for i in data_input %}
  {% do data_result.append(i) %}
{% endfor %}
echo results: {{data_result}} >>results.out

The following exception is thrown when using jinja2 'do' statement.

jinja2.exceptions.TemplateSyntaxError: Encountered unknown tag 'do'. Jinja was looking for the following tags: 'endfor' or 'else'. The innermost block that needs to be closed is 'for'.

I'm using cloud-init from a 64bitencoded file passed into terraform azure provider custom_data.

Revision history for this message
paul bruno (thetoolsmith) wrote :
Revision history for this message
Chad Smith (chad.smith) wrote :

Thank you for filing this bug and making Ubuntu and cloud-init better.

In investigation on this issue, I can see we don't load the jinja extension `ExprStmtExtension` or "do" extension.
In testing yesterday, I think we can just provide extensions=["jinja2.ext.do"] to our JTemplate in cloudinit.templater.

For versions of cloud-init that don't yet have a fix for this, the workaround in the absence of the "do" extension, they can use jinja default filter to return an empty string instead of printing None.

{% set data_result = [] %}
{% set data_input = [1,2,3] %}
{% for i in data_input %}
  {{ data_result.append(i)|default("",True) }}
{% endfor %}
{{data_result}}

Changed in cloud-init:
status: New → Triaged
status: Triaged → In Progress
importance: Undecided → Medium
assignee: nobody → paul bruno (thetoolsmith)
Revision history for this message
Chad Smith (chad.smith) wrote :

Upstream PR accepted which will resolve this bug in cloud-init
https://github.com/canonical/cloud-init/pull/1314

Thank you Paul.

Revision history for this message
Chad Smith (chad.smith) wrote :
Changed in cloud-init:
status: In Progress → Fix Committed
Revision history for this message
Brett Holman (holmanb) wrote : Fixed in cloud-init version 22.2.

This bug is believed to be fixed in cloud-init in version 22.2. If this is still a problem for you, please make a comment and set the state back to New

Thank you.

Changed in cloud-init:
status: Fix Committed → Fix Released
Revision history for this message
James Falcon (falcojr) wrote :
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.