Skip to content
This repository was archived by the owner on Jun 24, 2024. It is now read-only.

Commit c8570d3

Browse files
author
David Arnold
committed
Add native support for ansible filters
1 parent 26a67e9 commit c8570d3

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,18 @@ To enable the YAML support with [pyyaml](http://pyyaml.org/):
2727
pip install j2cli[yaml]
2828
```
2929

30+
To enable [ansible filters](https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html#filters):
31+
32+
```
33+
pip install j2cli[ansible]
34+
```
35+
36+
Or both with
37+
38+
```
39+
pip install j2cli[all]
40+
```
41+
3042
## Tutorial
3143

3244
Suppose, you want to have an nginx configuration file template, `nginx.j2`:

j2cli/cli.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,20 @@ def render_command(cwd, environ, stdin, argv):
186186
renderer = Jinja2TemplateRenderer(cwd, args.undefined, j2_env_params=customize.j2_environment_params())
187187
customize.j2_environment(renderer._env)
188188

189+
# Ansible filters - if available
190+
try:
191+
from ansible.plugins.filters import core as ansible_core_filters
192+
from ansible.plugins.filters import urls as ansible_urls_filters
193+
from ansible.plugins.filters import urlsplit as ansible_urlslict_filters
194+
from ansible.plugins.filters import mathstuff as ansible_mathstuff_filters
195+
196+
renderer.register_filters(ansible_core_filters.FilterModule().filters())
197+
renderer.register_filters(ansible_urls_filters.FilterModule().filters())
198+
renderer.register_filters(ansible_urlsplit_filters.FilterModule().filters())
199+
renderer.register_filters(ansible_mathstuff_filters.FilterModule().filters())
200+
except ImportError:
201+
pass
202+
189203
# Filters, Tests
190204
renderer.register_filters({
191205
'docker_link': filters.docker_link,

setup.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@
4949
'jinja2 >= 2.7.2',
5050
],
5151
extras_require={
52-
'yaml': [pyyaml_version,]
52+
'yaml': [pyyaml_version,],
53+
'ansible': ['ansible >= 2.9.10',],
54+
'all': [pyyaml_version, 'ansible >= 2.9.10',],
5355
},
5456
include_package_data=True,
5557
zip_safe=False,

0 commit comments

Comments
 (0)