Add native support for ansible filters

This commit is contained in:
David Arnold 2020-07-16 18:40:54 -05:00
parent 26a67e9419
commit 04611ae45d
No known key found for this signature in database
GPG Key ID: 6D6A936E69C59D08
4 changed files with 41 additions and 1 deletions

View File

@ -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`:

View File

@ -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,

View File

@ -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`:

View File

@ -49,7 +49,9 @@ setup(
'jinja2 >= 2.7.2',
],
extras_require={
'yaml': [pyyaml_version,]
'yaml': [pyyaml_version,],
'ansible': ['ansible >= 2.9.10',],
'all': [pyyaml_version, 'ansible >= 2.9.10',],
},
include_package_data=True,
zip_safe=False,