This page documents the TGenshi plugin. This plugin works with version 0.4 and newer of the genshi library.

The TGenshi plugin allows you to use the Genshi templating system to create files, instead of the various diff-based methods offered by the Cfg plugin. It also allows you to include the results of probes executed on the client in the created files.

To begin, you will need to download and install the Genshi templating engine.

To install on CentOS or RHEL 5, run:

sudo yum install python-genshi

Once it is installed, you can enable it by adding TGenshi to the generators line in /etc/bcfg2.conf on your Bcfg server. For example:

generators = SSHbase,Cfg,Pkgmgr,Svcmgr,Rules,TGenshi

The TGenshi plugin makes use of a Cfg-like directory structure located in in a TGenshi subdirectory of your repository, usually /var/lib/bcfg2/TGenshi. Each file has a directory containing two file types, template and info. Templates are named according to the genshi format used; template.txt uses the genshi text format, and template.xml uses the XML format.

If used with Genshi 0.5 or later the plugin also supports the new style text template format for files named template.newtxt. One of the advantages of the new format is that it does not use # as a command delimiter, making it easier to utilize for configuration files that use # as a comment character.

Only one template format may be used per file served. Info files are identical to those used in Cfg, and info.xml files are supported.

Inside of templates

  • metadata is the client’s metadata
  • is an xml document of unstructured data
  • name is the path name specified in bcfg
  • path is the path to the TGenshi template

See the genshi documentation for examples of Genshi syntax.

Examples: Old Genshi Syntax

Genshi’s web pages recommend against using this syntax, as it may disappear from future releases.

Group Negation

Templates are also useful for cases where more sophisticated boolean operations than those supported by Cfg are needed. For example, the template:

#if "ypbound" in metadata.groups and "workstation" in metadata.groups
client is ypbound workstation
#if "ubuntu" not in metadata.groups and "desktop" in metadata.groups
client is a desktop, but not an ubuntu desktop


<Path type="file" name="/bar.conf" owner="root" perms="0644" group="root">client is ypbound workstation
client is a desktop, but not an ubuntu desktop

This flexibility provides the ability to build much more compact and succinct definitions of configuration contents than Cfg can.

File permissions

File permissions for entries handled by TGenshi are controlled via the use of Info files. Note that you cannot use both a Permissions entry and a Path entry to handle the same file.

Error handling

Situations may arrise where a templated file cannot be generated due to missing or incomplete information. A TemplateError can be raised to force a bind failure and prevent sending an incomplete file to the client. For example, this template:

{% python
    from genshi.template import TemplateError
    grp = None
    for g in metadata.groups:
        if g.startswith('ganglia-gmond-'):
            grp = g
        raise TemplateError, "Missing group"

will fail to bind if the client is not a member of a group starting with “ganglia-gmond-“. The syslogs on the server will contain this message:

bcfg2-server[5957]: Genshi template error: Missing group
bcfg2-server[5957]: Failed to bind entry: Path /etc/ganglia/gmond.conf

indicating the bind failure and message raised with the TemplateError.



How do I escape the $ (dollar sign) in a TGenshi text template? For example, if I want to include SVN (subversion) keywords like $Id$ or $HeadURL$ in TGenshi-generated files, or am templating a bourne shell (sh/bash) script or Makefile (make).


Use $$ (double dollar sign) to output a literal $ (dollarsign) in a TGenshi text template. So instead of $Id$, you’d use $$Id$$. See also Genshi tickets #282: Document $$ escape convention and #283: Allow for redefinition of template syntax per-file.

Table Of Contents

Previous topic


Next topic


This Page