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

Commit

Permalink
Add native support for ansible filters
Browse files Browse the repository at this point in the history
  • Loading branch information
David Arnold committed Jul 16, 2020
1 parent 26a67e9 commit c8570d3
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`:
Expand Down
14 changes: 14 additions & 0 deletions j2cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.filters import core as ansible_core_filters
from ansible.plugins.filters import urls as ansible_urls_filters
from ansible.plugins.filters import urlsplit as ansible_urlslict_filters
from ansible.plugins.filters 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,
Expand Down
4 changes: 3 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@
'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,
Expand Down

0 comments on commit c8570d3

Please sign in to comment.