I cannot add a new entry to the object dictionary when using the jinja2 template.
For example, here I use the jinja2 template and created the data variable, which is a dictionary. And after checking some if conditions, I want to add a location attribute to the data object, e.g.
{%- set data = { 'name' : node.Name, 'id' : node.id, } -%} {% if node.location !="" %} data.append({'location': node.location}) {% endif %}
However, I could not find a way to achieve this and get an UndefinedError:
jinja2.exceptions.UndefinedError: 'dict object' has no attribute 'append'
Has anyone encountered this problem or can provide a link to solve it?
I searched on the Internet, but could not find a solution, that is, how to achieve adding an entry to a dict object in Jinja.
I referenced the following and other web resources:
- http://cewing.imtqy.com/training.codefellows/assignments/day22/jinja2_walkthrough.html
- In Jinja2, what is the easiest way to set all keys to dictionary values?
- https://github.com/saltstack/salt/issues/27494
source share