Generate yaml templates

1,274 views
Skip to first unread message

Alfredo Palhares

unread,
Jul 2, 2014, 2:00:28 PM7/2/14
to salt-users
Hello fellow salt users,

I have an application that is configurated by yaml, well salt pillar
data is yaml, how can I quickly generate a yaml file from the pillar
data to a configuration file.
Basically this.
pillar/myapp.sls

myapp:
option1:
- looolol
- lololol2

And a state with something like.
/etc/myapp/config
file:
- managed
- source???

Any ideas ?

--
Regards,
Alfredo Palhares

Seth House

unread,
Jul 2, 2014, 3:00:45 PM7/2/14
to salt-...@googlegroups.com
On Wed, Jul 2, 2014 at 12:00 PM, Alfredo Palhares
<maste...@masterkorp.net> wrote:
> I have an application that is configurated by yaml, well salt pillar
> data is yaml, how can I quickly generate a yaml file from the pillar
> data to a configuration file.

Take a look at the yaml() Jinja filter. Just keep in mind the nested
templating that's going on here. If you're using any multi-line
strings or multi-line (pretty) YAML it's easy to mess up the
indentation.

http://docs.saltstack.com/en/latest/ref/renderers/all/salt.renderers.jinja.html#salt.utils.jinja.SerializerExtension

Something like this should be pretty safe:

/etc/myapp/config
file:
- managed
- contents: |
{{ salt['pillar.get']('myapp') | yaml() | indent(8) }}

The flow_control parameter to output pretty YAML is new in the
upcoming Salt feature release (Helium). In the 2014.1 release the YAML
will be output on a single line, I think.

Alfredo Palhares

unread,
Jul 7, 2014, 10:42:36 AM7/7/14
to salt-users
Hello,

First of all thanks for your input.

> /etc/myapp/config
> file:
> - managed
> - contents: |
> {{ salt['pillar.get']('myapp') | yaml() | indent(8) }}
The yaml() method seems to be ignored, the template just gets the pillar
data as a python dictionary.

Do I need to enable the serializer extension[1] in some way ? I am using
salt 2014.1.5

[1] https://sourcegraph.com/github.com/saltstack/salt/symbols/python/salt/utils/jinja/SerializerExtension


--
Regards,
Alfredo Palhares

Seth House

unread,
Jul 8, 2014, 2:53:13 AM7/8/14
to salt-...@googlegroups.com
On Mon, Jul 7, 2014 at 7:42 AM, Alfredo Palhares
<maste...@masterkorp.net> wrote:
> Do I need to enable the serializer extension[1] in some way?

No, it's on by default and it's been in Salt for two major releases
now. I can't think of a reason it wouldn't work in the above example.
Does the ``json()`` filter produce JSON?

Alfredo Palhares

unread,
Jul 8, 2014, 5:56:08 AM7/8/14
to salt-users
Hello Seth,

> No, it's on by default and it's been in Salt for two major releases
> now. I can't think of a reason it wouldn't work in the above example.
Ok, Is there any way i can prove its really there ?


> Does the ``json()`` filter produce JSON?
No, Doing:
- contents: {{ pillar["myapp"]["mydata"] | json() }}

Still produces a ordered dictonary on the template.
Any susgestions ?

--
Regards,
Alfredo Palhares

Seth House

unread,
Jul 9, 2014, 8:55:52 PM7/9/14
to salt-...@googlegroups.com
On Tue, Jul 8, 2014 at 2:56 AM, Alfredo Palhares
<maste...@masterkorp.net> wrote:
> Ok, Is there any way i can prove its really there ?

What is output when running this state?

http://paste.fedoraproject.org/116836/04953642/

(Formatting of inline version below may be wrong.)

test_if_has_yaml_filter:
cmd:
- run
- name: |
echo 'The statement "I can see the YAML filter," is {{ yaml is
callable }}'.

Seth House

unread,
Jul 9, 2014, 9:16:41 PM7/9/14
to salt-...@googlegroups.com
I ran a quick test on v2014.1.5. Below are the files & output:

http://paste.fedoraproject.org/116837/54858140/

I'm can't think of a reason the OrderedDict would be showing through
in your template. Could you pastebin what you're seeing?

Alfredo Palhares

unread,
Jul 10, 2014, 10:18:02 AM7/10/14
to salt-users
Hello Seth,

I ran your statement, even without a succesful run, the {{ yaml is
caoable }} return true[1]

The the rendering still does not work. I am really out of opinions :(

[1] http://pastie.org/private/ij41bgiqwsvewgfdp0og


Thank you so much for your help so far!

--
Regards,
Alfredo Palhares

Alfredo Palhares

unread,
Jul 10, 2014, 10:28:37 AM7/10/14
to salt-users
While being true, here is an echo "{{ pillar["myapp"] | yaml() }}"
command[1]
The renderer is there, maybe the syntax is wrong?

[1] http://pastie.org/private/puykt9au4lkd0s1lhe4giq


--
Regards,
Alfredo Palhares

Daniel Jagszent

unread,
Jul 10, 2014, 2:02:16 PM7/10/14
to salt-...@googlegroups.com
Hi Alfredo ,

I think you have to add quotes:
- contents: '{{ pillar["myapp"]["mydata"] | json() }}'

Anyways, if I find myself juggling around Jinja filters just to produce valid YAML
, I mostly opt for writing that particular state in pydsl instead.
http://docs.saltstack.com/en/latest/ref/renderers/all/salt.renderers.pydsl.html

#!pydsl

import json

state('/my/file/name').file.managed(
  contents=json.dumps(__pillar__["myapp"]["mydata"])
)

Seth House

unread,
Jul 10, 2014, 8:41:37 PM7/10/14
to salt-...@googlegroups.com
On Thu, Jul 10, 2014 at 7:28 AM, Alfredo Palhares
<maste...@masterkorp.net> wrote:
> While being true, here is an echo "{{ pillar["myapp"] | yaml() }}"
> command[1]
> The renderer is there, maybe the syntax is wrong?

It looks like there may be a rogue newline in the Pillar contents
somewhere that is messing things up when it's inserted into the state.
Try using a multi-line YAML string and the Jinja intent filter:

your_state:
cmd:
- run
- name: |
echo "{{ pillar["myapp"] | yaml() | indent(8) }}"

Alfredo Palhares

unread,
Jul 11, 2014, 8:00:02 AM7/11/14
to salt-users
Hello Daniel,

> Hi Alfredo ,
>
> I think you have to add quotes:
>
> - contents: '{{ pillar["myapp"]["mydata"] | json() }}'
>
>
> Anyways, if I find myself juggling around Jinja filters just to produce
> valid YAML, I mostly opt for writing that particular state in pydsl instead.
> http://docs.saltstack.com/en/latest/ref/renderers/all/salt.renderers.pydsl.html
>
> #!pydsl
>
> import json
>
> state('/my/file/name').file.managed(
> contents=json.dumps(__pillar__["myapp"]["mydata"])
> )
Actually its YAML, but I tried that renderer, just to see it happening.

But I was greeted by this error.

[1] http://pastie.org/private/xadwa5jwsibnrv1hggclq


--
Regards,
Alfredo Palhares

Daniel Jagszent

unread,
Jul 11, 2014, 8:30:14 AM7/11/14
to salt-...@googlegroups.com
Hi Alfredo,

please read http://docs.saltstack.com/en/latest/ref/renderers/index.html .

The shebang "#!pydsl" is important.

------ 8< ------
#!pydsl

import yaml

state('/etc/salt/cloud.yaml').file.managed(contents=yaml.dump(__pillar__["salt"]["cloud"]))

------ >8 ------
Reply all
Reply to author
Forward
0 new messages