Re: [ansible-project] jinja json template adds an extra comma breaking json structure

342 views
Skip to first unread message

Lucas, Sascha

unread,
Aug 13, 2014, 2:15:34 AM8/13/14
to ansible...@googlegroups.com
Hi,

From: David Neudorfer
Date: Wed, 13. Aug 2014 07:51

>      "interval": 5,
>      "standalone": true
>    },
> }

I'm not sure if I understand your issue correctly... but with jinja2 you can check if you are at the end of a loop:

{% if loop.last %}some text at the end.{% else %}some text in between,{% endif %}

So your last loop should insert "," only if not at the end...?

HTH, Sascha.

Stellvertretender Vorsitzender des Aufsichtsrates: Detlef Hillebrand
Geschäftsführung: Michael Krüger
Sitz der Gesellschaft: Halle/Saale
Registergericht: Amtsgericht Stendal | Handelsregister-Nr. HRB 208414
UST-ID-Nr. DE 158253683

Diese E-Mail enthält vertrauliche und/oder rechtlich geschützte Informationen. Wenn Sie nicht der richtige Empfänger sind oder diese E-Mail irrtümlich erhalten haben, informieren Sie bitte sofort den Absender und vernichten Sie diese Mail. Das unerlaubte Kopieren sowie die unbefugte Weitergabe dieser Mail oder des Inhalts dieser Mail sind nicht gestattet. Diese Kommunikation per E-Mail ist nicht gegen den Zugriff durch Dritte geschützt. Die GISA GmbH haftet ausdrücklich nicht für den Inhalt und die Vollständigkeit von E-Mails und den gegebenenfalls daraus entstehenden Schaden. Sollte trotz der bestehenden Viren-Schutzprogramme durch diese E-Mail ein Virus in Ihr System gelangen, so haftet die GISA GmbH - soweit gesetzlich zulässig - nicht für die hieraus entstehenden Schäden.

David Neudorfer

unread,
Aug 13, 2014, 2:42:40 AM8/13/14
to ansible...@googlegroups.com
Thanks Lucas!

I was able to accomplish my goal with 

{% if not loop.last %}
   
,
{% endif %}

Michael DeHaan

unread,
Aug 13, 2014, 8:42:31 AM8/13/14
to ansible...@googlegroups.com
I'd possibly recommend *not* building it this way, but instead building your datastructure out programatically using Jinja2 statements.
This way you're basically iterating in Python, and setting things in Python.

# fake example follows
{% for x in y %}
    {% datastructure[x] = 'some expression' %}
{% endfor %}

At the end of the template, just dump your datastructure with:

{{ datastructure | to_nice_json }}

Templating a JSON file seems like pain to me, and that would be a lot easier.




--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/f6fef5c4-4134-4906-bb73-287a1687a2d8%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages