ARTICLE PREREQUISITES: Familiarity with creating Chef cookbook.
Anyone familiar with change configuration tools knows generating configuration files using templates is the bread and butter of these types of tools. Ansible and SaltStack use Jinja, while Chef and Puppet have used ERB (Embedded Ruby).
In most cases a simple static ERB is all you want, with most of the defaults statically coded, and few variables that are set outside of the ERB.
If the default is not an empty string, some extra attention is needed:
The cookbook recipe now creates the uses the template resource and override variables where the default is not desired:
As said before, most cases you would never want to do this. There may be a few reasons for using this:
- broad target audience, such as a large organization or public cookbook
- re-use of output, such as doing RESTful API with JSON picked up from a file.
I originally came across this ruby hash as ERB idea from:
- Heavy Water Operations Cookbook’s aptly configuration template.