Python imports with the py state renderer

11 views
Skip to first unread message

Chris Howells

unread,
Aug 15, 2017, 11:46:56 AM8/15/17
to salt-...@googlegroups.com
Hi,

I'm re-writing a yaml/jinja state using the python state renderer,
because the amount of logic involved is a little clumsy with jinja. Salt
2017.7.0.

I was using:

{%- from "xxx.sls" import aaa, bbb %}

But I've been unable to figure out how to replicate this with the py
renderer. I've tried:

1 #!py
2
3 import re
4 import pprint
5 import json
6 import ipaddress
7 import hashlib
8
9 from xxx.sls import aaa, bbb

With this it's only searching system directories rather than files
relative to the state. Do I need to play about with sys.path.append? If
anyone has a working example that would be great.

Furthermore, I'm not sure if I should be using 'from xxx' or 'from
xxx.sls' or something else - do I need to rename the file to get rid of
the sls extension? (Not sure what impact this will have on the rest of
salt)

From strace'ing the salt master:

[pid 61] open("/srv/pillar/yyy.slsc",
O_WRONLY|O_CREAT|O_EXCL|O_TRUNC, 0100644) = -1 EROFS (Read-only file
system)
[pid 61] stat("/usr/bin/xxx", 0x7ffdf30585e0) = -1 ENOENT (No such
file or directory)
[pid 61] open("/usr/bin/xxx.x86_64-linux-gnu.so", O_RDONLY) = -1
ENOENT (No such file or directory)
[pid 61] open("/usr/bin/xxx.so", O_RDONLY) = -1 ENOENT (No such file
or directory)
[pid 61] open("/usr/bin/xxxmodule.so", O_RDONLY) = -1 ENOENT (No such
file or directory)
[pid 61] open("/usr/bin/xxx.py", O_RDONLY) = -1 ENOENT (No such file
or directory)
[pid 61] open("/usr/bin/xxx.pyc", O_RDONLY) = -1 ENOENT (No such file
or directory)
[pid 61] stat("/usr/lib/python2.7/xxx", 0x7ffdf30585e0) = -1 ENOENT
(No such file or directory)
[pid 61] open("/usr/lib/python2.7/xxx.x86_64-linux-gnu.so", O_RDONLY)
= -1 ENOENT (No such file or directory)
[pid 61] open("/usr/lib/python2.7/xxx.so", O_RDONLY) = -1 ENOENT (No
such file or directory)
[pid 61] open("/usr/lib/python2.7/xxxmodule.so", O_RDONLY) = -1
ENOENT (No such file or directory)
[pid 61] open("/usr/lib/python2.7/xxx.py", O_RDONLY) = -1 ENOENT (No
such file or directory)
[pid 61] open("/usr/lib/python2.7/xxx.pyc", O_RDONLY) = -1 ENOENT (No
such file or directory)

Many thanks.

Seth House

unread,
Aug 15, 2017, 1:18:24 PM8/15/17
to salt users list
Jinja imports are different than Python imports. Salt has customized
the Jinja renderer to use Salt's file server. You can replicate that
in the py renderer using a couple execution modules. The following two
threads should be helpful:

https://groups.google.com/d/msg/salt-users/p_cmbvuPk3A/BOoK3Nw_AQAJ
https://groups.google.com/d/msg/salt-users/E-DDD-eoCIM/9IDfb7J6AgAJ

We should add these to the py renderer docs...
> --
> 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/fb1d86f627a6013f3efd5f75fe5d1f7c%40chrishowells.co.uk.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages