Add native support for ansible filters
This commit is contained in:
parent
26a67e9419
commit
04611ae45d
12
README.md
12
README.md
|
@ -27,6 +27,18 @@ To enable the YAML support with [pyyaml](http://pyyaml.org/):
|
|||
pip install j2cli[yaml]
|
||||
```
|
||||
|
||||
To enable [ansible filters](https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html#filters):
|
||||
|
||||
```
|
||||
pip install j2cli[ansible]
|
||||
```
|
||||
|
||||
Or both with
|
||||
|
||||
```
|
||||
pip install j2cli[all]
|
||||
```
|
||||
|
||||
## Tutorial
|
||||
|
||||
Suppose, you want to have an nginx configuration file template, `nginx.j2`:
|
||||
|
|
14
j2cli/cli.py
14
j2cli/cli.py
|
@ -186,6 +186,20 @@ def render_command(cwd, environ, stdin, argv):
|
|||
renderer = Jinja2TemplateRenderer(cwd, args.undefined, j2_env_params=customize.j2_environment_params())
|
||||
customize.j2_environment(renderer._env)
|
||||
|
||||
# Ansible filters - if available
|
||||
try:
|
||||
from ansible.plugins.filter import core as ansible_core_filters
|
||||
from ansible.plugins.filter import urls as ansible_urls_filters
|
||||
from ansible.plugins.filter import urlsplit as ansible_urlsplit_filters
|
||||
from ansible.plugins.filter import mathstuff as ansible_mathstuff_filters
|
||||
|
||||
renderer.register_filters(ansible_core_filters.FilterModule().filters())
|
||||
renderer.register_filters(ansible_urls_filters.FilterModule().filters())
|
||||
renderer.register_filters(ansible_urlsplit_filters.FilterModule().filters())
|
||||
renderer.register_filters(ansible_mathstuff_filters.FilterModule().filters())
|
||||
except ImportError:
|
||||
pass
|
||||
|
||||
# Filters, Tests
|
||||
renderer.register_filters({
|
||||
'docker_link': filters.docker_link,
|
||||
|
|
|
@ -27,6 +27,18 @@ To enable the YAML support with [pyyaml](http://pyyaml.org/):
|
|||
pip install j2cli[yaml]
|
||||
```
|
||||
|
||||
To enable [ansible filters](https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html#filters):
|
||||
|
||||
```
|
||||
pip install j2cli[ansible]
|
||||
```
|
||||
|
||||
Or both with
|
||||
|
||||
```
|
||||
pip install j2cli[all]
|
||||
```
|
||||
|
||||
## Tutorial
|
||||
|
||||
Suppose, you want to have an nginx configuration file template, `nginx.j2`:
|
||||
|
|
Loading…
Reference in New Issue