Search code examples
jinja2salt-project

Salt: Using salt state in jinja if-else state


I'm trying to deploy a django project with saltstack.

I wrote a sls file and It installs packages and run some commands.

It installs django, nginx, etc and I want to run manage.py collectstatic for nginx.

but when I re-apply this formula, It returns an error that /static directory is already exists.

so I modified the sls file

collect_static_files:
{% if not salt['file.exists'][BASEDIR,'myproject/static']|join('') %}
  cmd.run:
    - name: '~~~ collectstatic;'
    - cwd: /path/to/venv/bin
{% else %}
  cmd.run:
    - name: echo "Static directory exists."
{% endif %}

but when I run salt '*' state.apply myformula,

It says:

minion:
 Data failed to compile:
----------
    Rendering SLS 'base:myproj' failed: Jinja variable 'salt.utils.templates.AliasedLoader object' has no attribute 'file.exists'

How can I solve this problem? Thank you.


Solution

  • I was a fool... {% if not salt['file.directory_exists'](BASEDIR + 'myproject/static') %} worked well. The problem was I used the state module not execution module of salt. Now I understand that state module describes "state" and execution modules act like a function.