Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Usage: jinja2 [options] <input template> <input data>
Options:
--version show program's version number and exit
-h, --help show this help message and exit
--format=FORMAT format of input variables: auto, ini, json,
--format=FORMAT format of input variables: auto, env, ini, json,
querystring, yaml, yml
-e EXTENSIONS, --extension=EXTENSIONS
extra jinja2 extensions to load
Expand All @@ -26,8 +26,16 @@ Options:
Use only this section from the configuration
--strict Disallow undefined variables to be used within the
template
-a, --ansible Enable support for Ansible filters
-o FILE, --outfile=FILE
File to use for output. Default is stdout.
```

## Optional Ansible support
If `Ansible Core` is present, you can use Ansible filters within your templates.

`$ pip install jinja2-cli[ansible]`

## Optional YAML support
If `PyYAML` is present, you can use YAML as an input data source.

Expand Down
19 changes: 17 additions & 2 deletions jinja2cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,13 @@ def _parse_env(data):
}


def render(template_path, data, extensions, strict=False):
def _load_ansible_filters():
from ansible.plugins.filter.core import FilterModule

return FilterModule().filters()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if we try...except ImportError and print a more useful message to say that ansible-core is missing? Just so we can suggest how to resolve the issue.



def render(template_path, data, extensions, strict=False, ansible=False):
from jinja2 import (
__version__ as jinja_version,
Environment,
Expand All @@ -246,6 +252,8 @@ def render(template_path, data, extensions, strict=False):
)
if strict:
env.undefined = StrictUndefined
if ansible:
env.filters.update(_load_ansible_filters())

# Add environ global
env.globals["environ"] = lambda key: force_text(os.environ.get(key))
Expand Down Expand Up @@ -336,7 +344,7 @@ def cli(opts, args):

out = codecs.getwriter("utf8")(out)

out.write(render(template_path, data, extensions, opts.strict))
out.write(render(template_path, data, extensions, opts.strict, opts.ansible))
out.flush()
return 0

Expand Down Expand Up @@ -422,6 +430,13 @@ def main():
dest="strict",
action="store_true",
)
parser.add_option(
"-a",
"--ansible",
help="Enable support for Ansible filters",
dest="ansible",
action="store_true",
)
parser.add_option(
"-o",
"--outfile",
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
license="BSD",
install_requires=install_requires,
extras_require={
"ansible": install_requires + ["ansible-core"],
"yaml": install_requires + ["pyyaml"],
"toml": install_requires + ["toml"],
"xml": install_requires + ["xmltodict"],
Expand Down