In my Ansible project, I have installed libraries using pip
that allow me to use additional jinja2 filters.
Among those, I have installed the netaddr
library that allows me to use the ipaddr
filter, like this: <netmask>{{ interface.IP | ipaddr('netmask') }}</netmask>
(this works in my Ansible playbook)
The problem comes when I try to test my templates without having to run my Ansible playbook by creating a quick Python script. For instance:
from jinja2 import Template
print Template('{{ ip | ipaddr("netmask") }}').render(ip='10.10.10.0/24')
Throws me the following exception:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/jinja2/environment.py", line 945, in __new__
return env.from_string(source, template_class=cls)
File "/usr/local/lib/python2.7/dist-packages/jinja2/environment.py", line 880, in from_string
return cls.from_code(self, self.compile(source), globals, None)
File "/usr/local/lib/python2.7/dist-packages/jinja2/environment.py", line 591, in compile
self.handle_exception(exc_info, source_hint=source_hint)
File "/usr/local/lib/python2.7/dist-packages/jinja2/environment.py", line 780, in handle_exception
reraise(exc_type, exc_value, tb)
File "<unknown>", line 1, in template
jinja2.exceptions.TemplateAssertionError: no filter named 'ipaddr'
I can see in that last line that jinja2 can't find the 'ipaddr' filter, which is
installed in /usr/lib/python2.7/dist-packages
, but I can't find how to use packages installed via pip directly in my Python scripts.