The TemplateHelper plugin is a connector plugin that adds Python classes and methods to client metadata instances for use in templates. This allows you to easily reuse code that is common amongst multiple templates and add convenience methods.
First, mkdir /var/lib/bcfg2/TemplateHelper and add TemplateHelper to your plugins line in /etc/bcfg2.conf. Restart bcfg2-server.
Now, any .py file placed in /var/lib/bcfg2/TemplateHelper/ will be read and added to matching client metadata objects. See Writing Helpers below for more information on how to write TemplateHelper scripts.
TemplateHelper does not support group- or host-specific helpers. All helpers will be available to all clients.
A helper module is just a Python module with three special conditions:
See examples/TemplateHelper for examples of helper modules.
Specific helpers can be referred to in templates as metadata.TemplateHelper[<modulename>]. That returns a HelperModule object which will have, as attributes, all symbols listed in __export__. For example, consider this helper module:
__export__ = ["hello"]
def hello(metadata):
return "Hello, %s!" % metadata.hostname
To use this in a Genshi template, we could do:
${metadata.TemplateHelper['hello'].hello(metadata)}
The template would produce:
Hello, foo.example.com!
Note that the client metadata object is not passed to a helper module in any magical way; if you want to access the client metadata object in a helper function or class, you must pass the object to the function manually.