jinja validator

5,780 views
Skip to first unread message

Zippy Zeppoli

unread,
Jan 8, 2013, 11:40:31 PM1/8/13
to salt-...@googlegroups.com
Hi List,
Is there a simple jinja validator one can use to check syntax?

Thanks.

Tor Hveem

unread,
Jan 9, 2013, 5:39:04 AM1/9/13
to salt-...@googlegroups.com
I came up with the following command to do simple syntax check of jinja.

python -c 'from jinja2 import Environment as j; print j().from_string(file("init.sls").read()).render(func=lambda:None)'

Or if you want a simple script:
>>> from jinja2 import Template
>>>
>>> x = '''
... {% for username in 'foo','bar','baz' %}
... {{ username }}:
...   user.present:
...     - shell: /bin/zsh
...     - home: /home/{{ username }}
... {% endfor %}
...
... {% if 'foo' == 'bar' %}
... /tmp/bar:
...   file.touch
... {% elif 'foo' == 'foo' %}
... /tmp/foo:
...   file.touch
... {% endif %}
... '''
>>> tmpl = Template(x)
>>> print tmpl.render()

Where x is your context / variables.


2013/1/9 Zippy Zeppoli <zippyz...@gmail.com>
Hi List,
Is there a simple jinja validator one can use to check syntax?

Thanks.

--
 
 

Thomas S Hatch

unread,
Jan 9, 2013, 12:13:23 PM1/9/13
to salt-...@googlegroups.com
Thanks Tor. I think that a Jinja validator would be a great addition, I am thinking that it might be a good thing to make as a runner


--
 
 

Paul Traylor

unread,
Jan 9, 2013, 2:15:43 PM1/9/13
to salt-...@googlegroups.com
2013/1/9 Thomas S Hatch <that...@gmail.com>:
> Thanks Tor. I think that a Jinja validator would be a great addition, I am
> thinking that it might be a good thing to make as a runner

I briefly thought about this but haven't had time to test out the idea.

My thought was that you could use cp.list_master and then filter on
.sls to get the list of the state files.

Then you can try to render them with state.show_sls to look for errors.


--
Paul Traylor
https://github.com/kfdm

Thomas S Hatch

unread,
Jan 10, 2013, 11:28:33 AM1/10/13
to salt-...@googlegroups.com
Yes, that would work, just doing a state.show_sls will display any errors in rendering the sls formula


--



Sean Channel

unread,
Jan 10, 2013, 5:34:23 PM1/10/13
to salt-...@googlegroups.com
There is a Vim syntax plug-in that comes with jinja2 in /usr/share/vim/addons/syntax/jinja.vim [ubuntu precise]. You can copy that to ~/.vim/syntax in addition to the salt-vim plugins. The salt-vim plugins can be installed with:

  cd ~/.vim
  git clone https://github.com/saltstack/salt-vim.git

_S.
Reply all
Reply to author
Forward
0 new messages