What can I do with py renderers?

73 views
Skip to first unread message

Herbert Fischer

unread,
Jun 7, 2017, 11:59:22 AM6/7/17
to Salt-users
Hi,

I am heavily considering writing my states in py renderers because of python flexibility, but I am afraid that some stuff might not work the same as with the default jinja renderer.

I wish to be able to load yamls, merge stuff with pillar, etc.

What SaltStack has available by default for this matters? Do I need to import python modules for this stuff? Can I?

best,

Herbert

--

Seth House

unread,
Jun 7, 2017, 12:42:23 PM6/7/17
to salt users list
The only differences you're likely to notice are:

* No access to Jinja filters. In practice this doesn't matter because
you'll have Python instead. ;-)

* Since you're also not using YAML you won't have the implicit,
source-order (top-down) execution order. We have a custom YAML parser
that keeps track of the order that it parses .sls files. Without that
you'll need to use requisites everywhere to enforce ordering.
> --
> 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.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/salt-users/CALbgzt_KksxWNeyXuKiXDVx_g5W1dNAJmsV%3DNS%3D_dAk5AvSDqg%40mail.gmail.com.
> For more options, visit https://groups.google.com/d/optout.

Herbert Fischer

unread,
Jun 8, 2017, 5:06:20 AM6/8/17
to Salt-users
How do you load YAML inside a state?

Does something like this works?

import yaml

with open("defaults.yaml", 'r') as stream:
    try:
        print(yaml.load(stream))
    except yaml.YAMLError as exc:
        print(exc)
If so, what about the file path? How can I use paths like "salt://" ?

best

On Wed, Jun 7, 2017 at 6:42 PM, Seth House <se...@eseth.com> wrote:
The only differences you're likely to notice are:

* No access to Jinja filters. In practice this doesn't matter because
you'll have Python instead. ;-)

* Since you're also not using YAML you won't have the implicit,
source-order (top-down) execution order. We have a custom YAML parser
that keeps track of the order that it parses .sls files. Without that
you'll need to use requisites everywhere to enforce ordering.


On Wed, Jun 7, 2017 at 9:59 AM, Herbert Fischer
<herbert.fischer@crossengage.io> wrote:
> Hi,
>
> I am heavily considering writing my states in py renderers because of python
> flexibility, but I am afraid that some stuff might not work the same as with
> the default jinja renderer.
>
> I wish to be able to load yamls, merge stuff with pillar, etc.
>
> What SaltStack has available by default for this matters? Do I need to
> import python modules for this stuff? Can I?
>
> best,
>
> Herbert
>
> --
>
> --
> 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
--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/salt-users/CABsXarGsHWcQDyMr8%3DGTdpxHz28snDSt7UY%2ByK9wVJG%2B3dkAfw%40mail.gmail.com.

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



--
Herbert Fischer | Senior IT Architect
CrossEngage GmbH | Bertha-Benz Straße 5 | 10557 Berlin

E-Mail: herbert...@crossengage.io

Amtsgericht Berlin-Charlottenburg | HRB 169537 B
Geschäftsführer: Dr. Markus Wübben, Manuel Hinz | USt-IdNr.: DE301504202

Daniel Wallace

unread,
Jun 8, 2017, 10:57:47 AM6/8/17
to Salt-users
what you would do is load it in a run() function, and return the dictionary.
>> > email to salt-users+...@googlegroups.com.
>> > To view this discussion on the web visit
>> >
>> > https://groups.google.com/d/msgid/salt-users/CALbgzt_KksxWNeyXuKiXDVx_g5W1dNAJmsV%3DNS%3D_dAk5AvSDqg%40mail.gmail.com.
>> > For more options, visit https://groups.google.com/d/optout.
>>
>> --
>> 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.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/salt-users/CABsXarGsHWcQDyMr8%3DGTdpxHz28snDSt7UY%2ByK9wVJG%2B3dkAfw%40mail.gmail.com.
>> For more options, visit https://groups.google.com/d/optout.
>
>
>
>
> --
> Herbert Fischer | Senior IT Architect
> CrossEngage GmbH | Bertha-Benz Straße 5 | 10557 Berlin
>
> E-Mail: herbert...@crossengage.io
> Web: www.crossengage.io
>
> Amtsgericht Berlin-Charlottenburg | HRB 169537 B
> Geschäftsführer: Dr. Markus Wübben, Manuel Hinz | USt-IdNr.: DE301504202
>
> --
> 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.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/salt-users/CALbgzt_cNV%3Do5bf_KXB0KJVQtUMJq-wQ8MjuXpW5uZcMX_WUDQ%40mail.gmail.com.

Seth House

unread,
Jun 8, 2017, 2:23:59 PM6/8/17
to salt users list
To grab other files from the Master you can make use of the
`cp.cache_file` execution function. This is the same functionality
that the Jinja renderer makes use of in some of it's custom Salt tags.
It returns the full path to the cached file on the Minion so you can
pass that to `yaml.safe_load()` (highly recommended over
`yaml.load()`.)

In addition there's a function you may find useful called
`slsutil.renderer` which allows mix-and-matching different renderers
when building out a state. It can be used to load YAML or even other
SLS files. (There's an open feature request to make it support salt://
paths natively.)

https://docs.saltstack.com/en/latest/ref/modules/all/salt.modules.cp.html#salt.modules.cp.cache_file
https://docs.saltstack.com/en/latest/ref/modules/all/salt.modules.slsutil.html#salt.modules.slsutil.renderer

#!py
def run():
path = __salt__.cp.cache_file('salt://test.yaml')
contents = __salt__.slsutil.renderer(path, renderer='yaml')
return {
'echo': {
'test.configurable_test_state': [
{
'comment': 'The contents of the file are:
{0}'.format(contents)
}
]
>> > email to salt-users+...@googlegroups.com.
>> > To view this discussion on the web visit
>> >
>> > https://groups.google.com/d/msgid/salt-users/CALbgzt_KksxWNeyXuKiXDVx_g5W1dNAJmsV%3DNS%3D_dAk5AvSDqg%40mail.gmail.com.
>> > For more options, visit https://groups.google.com/d/optout.
>>
>> --
>> 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.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/salt-users/CABsXarGsHWcQDyMr8%3DGTdpxHz28snDSt7UY%2ByK9wVJG%2B3dkAfw%40mail.gmail.com.
>> For more options, visit https://groups.google.com/d/optout.
>
>
>
>
> --
> Herbert Fischer | Senior IT Architect
> CrossEngage GmbH | Bertha-Benz Straße 5 | 10557 Berlin
>
> E-Mail: herbert...@crossengage.io
> Web: www.crossengage.io
>
> Amtsgericht Berlin-Charlottenburg | HRB 169537 B
> Geschäftsführer: Dr. Markus Wübben, Manuel Hinz | USt-IdNr.: DE301504202
>
> --
> 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.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/salt-users/CALbgzt_cNV%3Do5bf_KXB0KJVQtUMJq-wQ8MjuXpW5uZcMX_WUDQ%40mail.gmail.com.

Herbert Fischer

unread,
Jun 20, 2017, 11:17:33 AM6/20/17
to Salt-users
Thanks Seth!


>> > To view this discussion on the web visit
>> >
>> > https://groups.google.com/d/msgid/salt-users/CALbgzt_KksxWNeyXuKiXDVx_g5W1dNAJmsV%3DNS%3D_dAk5AvSDqg%40mail.gmail.com.
>> > For more options, visit https://groups.google.com/d/optout.
>>
>> --
>> 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

>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/salt-users/CABsXarGsHWcQDyMr8%3DGTdpxHz28snDSt7UY%2ByK9wVJG%2B3dkAfw%40mail.gmail.com.
>> For more options, visit https://groups.google.com/d/optout.
>
>
>
>
> --
> Herbert Fischer | Senior IT Architect
> CrossEngage GmbH | Bertha-Benz Straße 5 | 10557 Berlin
>
> E-Mail: herbert...@crossengage.io
> Web: www.crossengage.io
>
> Amtsgericht Berlin-Charlottenburg | HRB 169537 B
> Geschäftsführer: Dr. Markus Wübben, Manuel Hinz | USt-IdNr.: DE301504202
>
> --
> 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

> To view this discussion on the web visit
>
> For more options, visit https://groups.google.com/d/optout.

--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/salt-users/CABsXarGRoGQ5JBytS5FTJ57ZMNOjrNES6jEwgyX32T1y5VbKOg%40mail.gmail.com.

For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages