Product SiteDocumentation Site

5.7. Templating

One of the more powerful new features in Satellite 5.3 is Cheetah based kickstart templating. With this new capability, you can include variables, snippets (see below), and flow control statements such as for loops and if statements in your kickstart files.

5.7.1. Use Cases

There are a variety of reasons a user may want to use templating, such as:
  • You might want to reuse a particular section of a kickstart, such as a disk partitioning section, between multiple kickstarts.
  • If you have, for example, multiple kinds of server roles such as DNS server, proxy server, and web server, all with their own package set. You could define a snippet for each role. For example web server might have the following snippet defined:
    httpd
    mod_ssl
    mod_python
    
    If you want to create a web server profile, include the web server snippet in the %package section of your Kickstart file. If you wanted a profile to be both a web server and a proxy server, you could include both snippets in the package section. Then if you wanted to add another package to the web server snippet, mod_perl for example, by updating the snippet all profiles that are using that snippet would be updated as well.
  • You might want to perform certain actions in %post consistently across multiple kickstarts.

5.7.2. Variables

Templating allows for variables such as foo to be defined, and the value of those variables replaced wherever $foo is seen in a kickstart file.
Variables are subject to a form of inheritance that allows them to be set at one level and overridden at levels below them — the hierarchy is defined by Cobbler:
  • Kickstart tree (distro in cobbler) parameters come first
  • Kickstart Profile parameters override kickstart tree parameters
  • System parameters override Profile parameters
If a variable is defined at the system level, it will override the same variable defined at the Profile or Distro levels. Likewise, if a variable is defined at the Profile level, it will override the same variable if defined at the kickstart tree (distro) level.

Note

Note that kickstart tree (distro) variables cannot be defined for non-custom (automatically generated) kickstart trees such as the ones you get when you do a satellite sync.

5.7.3. Snippets

Snippets are similar to variables but can span many lines and can include variables in them. They can be included in a kickstart profile by using the text $SNIPPET('snippet_name'). You may make a snippet for a certain package list, one for a particular %post script, or for any text that would normally be included in a kickstart file.
The main purpose of snippets are to be able to reuse pieces of code between multiple kickstart templates and thus make each template easy to understand.
To manage snippets navigate to the Systems => Kickstart => Kickstart Snippets page. From here you can see Default Snippets that may not be edited, but may be used by any organization. These default snippets are provided to help make large tasks easier. For an explanation of common default snippets see the Default Snippet section below. From this page you may also view Snippets created just for your organization on the Custom Snippets tab. You may also create a custom Snippet by clicking on the create new snippet link. Note, default snippets are stored on the Satellite server's file system in /var/lib/cobbler/snippets/ while custom snippets are stored in the /var/lib/rhn/kickstarts/snippets/ directory. Since Satellite stores its snippets for different orgs in different directories, any custom snippets will be used like the following:
$SNIPPET('spacewalk/1/snippet_name')
The 1 in this case is the organization id. If you are not sure what text to insert in the kickstart in order to use your custom snippet, look for the Snippet Macro column on the snippet list, or on the snippet details page.
Snippets exist at a global level and do not share the same inheritance structure as variables. You may use variables within the snippets to change the way they behave depending on which system is requesting the kickstart.
Kickstart Snippets
Kickstart Snippets
Figure 5.3. Kickstart Snippets

5.7.3.1. Default Snippets

There are many snippets that ship by default and may be used in kickstarts written on or uploaded to the Satellite server. You may want to look at a template from a wizard style kickstart located in /var/lib/rhn/kickstarts/wizard/ and see what default snippets are used and how they are used. One of the most useful ones is redhat_register.
The redhat_register snippet can be used to register machines to a Satellite server as part of the kickstart. It uses a special variable called redhat_management_key to register it to the server. Simply set that variable at either the system, profile, or distro level and then add $SNIPPET('redhat_register') to a %post section of your kickstart. Any wizard style kickstarts that are generated by the Satellite server will already include this snippet in it's pre-made %post section.

5.7.3.2. Escaping Special Characeters

Since the $ and # characters are used during templating for specifying variables and control flow, you should not use these characters within scripts without escaping them.
So for example, if you were writing a bash script in a %post section:
%post 
echo $foo > /tmp/foo.txt
The templating engine would try to find a variable named $foo and would fail if foo did not exist as a variable. There are a few ways to escape the $ symbol so it shows up as a bash variable. The simplest is with a backslash:
%post 
echo \$foo > /tmp/foo.txt
\$foo will be rendered as $foo within the kickstart.
A second method is to wrap the entire script in #raw ... #endraw :
%post 
#raw  
echo $foo > /tmp/foo.txt 
#endraw
All %pre and %post scripts created using the wizard style kickstarts are wrapped with #raw...#endraw by default. This can be toggled using the Template checkbox available when editing a %post or %pre script.
The final method is simply by including #errorCatcher Echo in the first line of your kickstart. This instructs the templating engine to ignore any variables that do not exist and print out the text as is. This option is already included in the wizard style kickstarts, but you may want to include it in the raw kickstarts you create yourself.
If you would like more information about Cheetah and the constructs that can be used for writing kickstart templates, the Cheetah User's Guide should be very helpful: