From 04611ae45db186bfac7ce0ace7374f0b6af4f5aa Mon Sep 17 00:00:00 2001 From: David Arnold Date: Thu, 16 Jul 2020 18:40:54 -0500 Subject: [PATCH] Add native support for ansible filters --- README.md | 12 ++++++++++++ j2cli/cli.py | 14 ++++++++++++++ misc/_doc/README.md.j2 | 12 ++++++++++++ setup.py | 4 +++- 4 files changed, 41 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index b34a436..ccd78a5 100644 --- a/README.md +++ b/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`: diff --git a/j2cli/cli.py b/j2cli/cli.py index fae229a..3777194 100644 --- a/j2cli/cli.py +++ b/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, diff --git a/misc/_doc/README.md.j2 b/misc/_doc/README.md.j2 index d068d7f..460db9d 100644 --- a/misc/_doc/README.md.j2 +++ b/misc/_doc/README.md.j2 @@ -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`: diff --git a/setup.py b/setup.py index 71410d6..666f61f 100755 --- a/setup.py +++ b/setup.py @@ -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,