Double quotes in pillar list gets converted to single quote in jinja template

2,315 views
Skip to first unread message

KIRAN PRANEETH

unread,
Dec 8, 2016, 7:08:49 PM12/8/16
to Salt-users
Hello People,

I am trying to understand the encoding/string interpolation aspects from salt pillar to jinja template. All I am trying to do is specify double quotes in pillar for a list of strings but they end up with single quote when rendered in jinja template. I tried adding extra double quotes by escaping them but it still ends up in single quotes. Could you let me know what would be a immediate remediation?

Pillar:
blahblah:
cluster:
    servers: ["10.xx.xx.xx", "10.xx.xx.xx"]

Sls:
{% for host, settings in salt["pillar.get"]("blahblah:cluster", {}).iteritems() %}

servers: {{settings['servers']}}

Jinja:

{% if servers is defined %}
    "start_join": {{servers}}

Result:

"start_join": ['10.xx.xx.xx', '10.xx.xx.xx']

Seth House

unread,
Dec 8, 2016, 7:32:21 PM12/8/16
to salt users list
The workflow goes roughly like this:

1. You create a list of strings in YAML syntax.
2. That is parsed into a Python list of strings and shipped to the Minion.
3. The Jinja templating engine's job is to produce a string and so it
casts that list object to a string using the normal Python mechanism
which uses single quotes. Example:

% python -c 'print ["foo", "bar"]'
['foo', 'bar']

You can use Jinja to manually build a string of double-quoted,
comma-separated strings from your list by looping over it or possibly
by using Python's ''.join() method. But a quicker way for this
use-case is to just take advantage of the fact that JSON also uses
double-quotes when it serializes a data structure to a string. :-)

This should result in what you want:

"start_join": {{ servers | json() }}
> --
> You received this message because you are subscribed to the Google Groups
> "Salt-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to salt-users+...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

KIRAN PRANEETH

unread,
Dec 8, 2016, 8:09:00 PM12/8/16
to Salt-users, se...@eseth.com
| json() worked like a charm. Thanks Seth

Nicholas Capo

unread,
Dec 8, 2016, 8:19:20 PM12/8/16
to Salt-users, se...@eseth.com

I would also recommend using `file.serialize` with `- formatter: json` instead of `file.managed`.

You can still do you jinja conditionals, just in the state file instead of the template.

Nicholas

Reply all
Reply to author
Forward
Message has been deleted
Message has been deleted
Message has been deleted
Message has been deleted
Message has been deleted
Message has been deleted
Message has been deleted
Message has been deleted
Message has been deleted
Message has been deleted
Message has been deleted
Message has been deleted
Message has been deleted
Message has been deleted
Message has been deleted
Message has been deleted
Message has been deleted
Message has been deleted
0 new messages