for loop sorted output in a template

13 views
Skip to first unread message

Spike

unread,
Mar 12, 2017, 11:52:14 PM3/12/17
to Ansible Project
Hi,

I've had this problem came up a few times and hope someone here has a solution for it.

I have a basic for loop in a template where multiple lines are printed per loop. The problem is that I'd like similar lines to be grouped.

Example:

myvar:
  - {name: one, value: 1} 
  - {name: two, value: 2}

{% for v in myvar %}
name is {{v.name}}
val is {{v.value}}
{% endfor %}

That generates:

name is one
value is one
name is two
value is two

What I would like:

name is one
name is two
value is two
value is one

is this possible without a double loop each one containing a line?

thanks,

Spike
Reply all
Reply to author
Forward
0 new messages