opened
afischer-opentext-com
username_0
### Description of Issue/Question

Per [this comment](https://github.com/saltstack/salt/issues/47461#issuecomment-386719845) from @gtmanfred it should be possible to define and use custom jinja filters. However, I do not see them loaded. 

I have the following very simple snippet (to deserialize a json string). 

```
import salt.utils.json
from salt.utils.decorators.jinja import jinja_filter

log = logging.getLogger(__name__)


@jinja_filter('to_json')
def to_json(obj):
    try:
        return salt.utils.json.dumps(obj)
    except Exception:
        log.exception("Failed to serialize object '%s'", obj)
    return '{}'


@jinja_filter('from_json')
def from_json(serialized):
    try:
        return salt.utils.json.loads(serialized)
    except Exception:
        log.exception("Failed to deserialize json '%s'", serialized)
    return {}
```

and following the code, there should be a log message `log.debug('Marking \'%s\' as a jinja filter', name)` which I see for jinja filters defined per default. But I do not see it for any custom filter I tried. I can confirm the custom filter code is synced to the minion properly but then just not picked up. 

### Setup
Vanilla Windows with vanilla salt 2018.3.4 py3 AMD 64

### Steps to Reproduce Issue
```
salt 'minion' saltutil.sync_all
```

```
2019-03-18 04:04:13,362 [salt.fileclient  :236 ][INFO    ][2868] Caching directory '_utils/' for environment 'mysaltenv'
2019-03-18 04:04:13,408 [salt.fileclient  :1085][DEBUG   ][2868] In saltenv 'mysaltenv', looking at rel_path '_utils/jinja_filter_json.py' to resolve 'salt://_utils/jinja_filter_json.py'
2019-03-18 04:04:13,408 [salt.fileclient  :1093][DEBUG   ][2868] In saltenv 'mysaltenv', ** considering ** path 'c:\salt\var\cache\salt\minion\files\mysaltenv\_utils\jinja_filter_json.py' to resolve 'salt://_utils/jinja_filter_json.py'
2019-03-18 04:04:13,408 [salt.utils.extmods:109 ][DEBUG   ][2868] Local cache dir: 'c:\salt\var\cache\salt\minion\files\mysaltenv\_utils'
2019-03-18 04:04:13,408 [salt.utils.extmods:119 ][INFO    ][2868] Copying 'c:\salt\var\cache\salt\minion\files\mysaltenv\_utils\jinja_filter_json.py' to 'c:\salt\var\cache\salt\minion\extmods\utils\jinja_filter_json.py'
```

```
salt 'minion' state.show_sls test_custom_jinja
```
with
```
test_from_json:
  test.configurable_test_state:
    - changes: False
    - result: {{ "{'foo': 'bar'}"|from_json|to_bool }}
    - comment: "Result is expected to be true"

test_from_json:
  test.configurable_test_state:
    - changes: False
    - result: {{ "{}"|from_json|to_bool }}
    - comment: "Result is expected to be false"
```
Results in 
```
minion:
    - Rendering SLS 'mysaltenv:test_custom_jinja' failed: Jinja syntax error: no filter named 'from_json'; line 4

      ---
      test_from_json_true:
        test.configurable_test_state:
          - changes: False
          - result: {{ "{'foo': 'bar'}"|from_json|to_bool }}    <======================
          - comment: "Result is expected to be true"

      test_from_json_false:
        test.configurable_test_state:
          - changes: False
      [...]
      ---
```


### Versions Report

```
Salt Version:
           Salt: 2018.3.4
 
Dependency Versions:
           cffi: 1.10.0
       cherrypy: 10.2.1
       dateutil: 2.6.1
      docker-py: Not Installed
          gitdb: 2.0.5
      gitpython: 2.1.3
          ioflo: Not Installed
         Jinja2: 2.9.6
        libgit2: Not Installed
        libnacl: 1.6.1
       M2Crypto: Not Installed
           Mako: 1.0.6
   msgpack-pure: Not Installed
 msgpack-python: 0.4.8
   mysql-python: Not Installed
      pycparser: 2.17
       pycrypto: 2.6.1
   pycryptodome: Not Installed
         pygit2: Not Installed
         Python: 3.5.3 (v3.5.3:1880cb95a742, Jan 16 2017, 16:02:32) [MSC v.1900 64 bit (AMD64)]
   python-gnupg: 0.4.1
         PyYAML: 3.12
          PyZMQ: 16.0.3
           RAET: Not Installed
          smmap: 2.0.5
        timelib: 0.2.4
        Tornado: 4.5.1
            ZMQ: 4.1.6
 
System Versions:
           dist:   
         locale: cp1252
        machine: AMD64
        release: 2008ServerR2
         system: Windows
        version: 2008ServerR2 6.1.7601 SP1 Multiprocessor Free
```
Custom Jinja Filters not working?
issue
created
dwoz
username_1
@username_0

A couple thing's I notice here. You're putting the extension in `salt://_utils` and trying to use the filters with a pipe `|`.

Try putting the module into `salt://_modules` instead of `salt://_utils` and then call the functions directly as the documentation states [here](https://docs.saltstack.com/en/latest/topics/jinja/index.html#custom-jinja-filters).


```
test_from_json_true:
  test.configurable_test_state:
    - changes: False
    - result: {{ salt.jinja_filter_json("{'foo': 'bar'}")|to_bool }}
    - comment: "Result is expected to be true"

test_from_json_false:
  test.configurable_test_state:
    - changes: False
    - result: {{ salt.jinja_filter.json.from_json("{}")|to_bool }}
    - comment: "Result is expected to be false"
```

I've tested the above and it seems to work. Let me know if this works for you so I can close the ticket.
comment
created
afischer-opentext-com
username_0
Yeah, thank you. I knew that this works. I hoped the notation via pipe would work as it is more flexible, less verbose and better readable under circumstances. Also it would not require insight of the module organisation. Means when refactoring a module name or method name not all places would need to be modified. 

I conclude that this is not expected to work as the comment indicated. In this case feel free to close this issue (or mark as feature request 🥇)
comment