Import macros using relative path

1,101 views
Skip to first unread message

Tong Pham

unread,
Jan 31, 2017, 10:29:51 AM1/31/17
to Salt-users
Hello everyone,

My team has developed some useful macros that I now would like to make available for new roles I'm writing.
Here is a simple mockup:
 $ tree
.
├── roles
  ├── common
    └── echo.sls
  └── default
      └── init.sls
└── top.sls


$ cat roles
/common/echo.sls
{% macro echo(str) -%}
main
:
    cmd
.run:
       
- name: "echo 'Common echo: {{ str }}'"
{% endmacro %}

$ cat roles
/default/init.sls
{% from slspath + '/../../common/echo.sls' import echo as echo %}

{{ echo(2 + 2) }}

$ cat top
.sls
base:
   
'*':
       
- roles/default

I would like to make use of the macro defined under roles/common/echo.sls from roles/default/init.sls

However, I'm currently getting this error:

$ sudo salt-call state.apply
[WARNING ] Discarded template path 'roles/default/../../common/echo.sls', relative paths are prohibited
[ERROR   ] Rendering exception occurred: Jinja error: roles/default/../../common/echo.sls
Traceback (most recent call last):
 
File "/usr/lib/python2.7/site-packages/salt/utils/templates.py", line 368, in render_jinja_tmpl
    output
= template.render(**decoded_context)
 
File "/usr/lib/python2.7/site-packages/jinja2/environment.py", line 969, in render
   
return self.environment.handle_exception(exc_info, True)
 
File "/usr/lib/python2.7/site-packages/jinja2/environment.py", line 742, in handle_exception
    reraise
(exc_type, exc_value, tb)
 
File "<template>", line 1, in top-level template code
 
File "/usr/lib/python2.7/site-packages/salt/utils/jinja.py", line 102, in get_source
   
raise TemplateNotFound(template)
TemplateNotFound: roles/default/../../common/echo.sls




Salt version: 2016.11.1 (Carbon) I am running salt masterless on this minion node for testing (so I symlinked /srv/salt to the test directory above in my home directory)

I'm still very new to Salt, so I am not sure if this is the way to go. What is the best way to re-use macros in a given repository? I've also heard of modules, but not sure what to use.

Any help would be much appreciated! :")

Regards,
Tong

Tong Pham

unread,
Jan 31, 2017, 10:29:56 AM1/31/17
to Salt-users, Thanh Tong Pham
Hello,

I'm playing with importing macros, and have the following toy setup:


$ tree
.
├── roles
  ├── common
    └── echo.sls
  └── default
      └── init.sls
└── top.sls

Here are the contents of the files:
$ cat top.sls
base:
   
'*':
       
- roles/default


$ cat roles/common/echo.sls
{% macro echo(str) -%}
main
:
    cmd
.run:
       
- name: "echo 'Common echo: {{ str }}'"
{% endmacro %}

$ cat roles/default/init.sls
{% from slspath + '/../../common/echo.sls' import echo as echo %}

{{ echo(2 + 2) }}


I'm running into the following error when testing this config using masterless minion (local file):

$ sudo salt-call state.apply
[WARNING ] Discarded template path 'roles/default/../../common/echo.sls', relative paths are prohibited
[ERROR   ] Rendering exception occurred: Jinja error: roles/default/../../common/echo.sls
Traceback (most recent call last):
 
File "/usr/lib/python2.7/site-packages/salt/utils/templates.py", line 368, in render_jinja_tmpl
    output
= template.render(**decoded_context)
 
File "/usr/lib/python2.7/site-packages/jinja2/environment.py", line 969, in render
   
return self.environment.handle_exception(exc_info, True)
 
File "/usr/lib/python2.7/site-packages/jinja2/environment.py", line 742, in handle_exception
    reraise
(exc_type, exc_value, tb)
 
File "<template>", line 1, in top-level template code
 
File "/usr/lib/python2.7/site-packages/salt/utils/jinja.py", line 102, in get_source
   
raise TemplateNotFound(template)
TemplateNotFound: roles/default/../../common/echo.sls

How should one reuse macros? I have no problem import them when they are in the same directory, but I imagine that can get messy.

Thank you!

Regards,
Tong

Viet Hung Nguyen

unread,
Jan 31, 2017, 10:56:50 AM1/31/17
to salt-...@googlegroups.com, Thanh Tong Pham
Hello, 
Salt template import (https://docs.saltstack.com/en/latest/topics/jinja/index.html#include-and-import - based on Jinja2) does not work with relative path. It uses salt root dir as base, so in your example, you can just:

```
from ‘roles/common/echo.sls’ import echo 
```

I cannot find the doc says that but I’ve never seen anyone use relative path and I couldn’t think of any good reason to do that, too.


Viet Hung Nguyen

--
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/1bb5bcc9-ac13-478e-ac71-bc1102c53e86%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Tong Pham

unread,
Feb 1, 2017, 6:24:35 PM2/1/17
to Salt-users
Thanks a lot Viet! That fixed the issue! :D

Regards,
Tong
Reply all
Reply to author
Forward
0 new messages