2.4. Templating
for loops and if statements in the kickstart files. This is achieved using the cheetah tool.
- Reusing a particular section of a kickstart, such as a disk partitioning section, between multiple kickstarts.
- Performing some
%postactions consistently across multiple kickstarts. - Defining a snippet across multiple server roles such as DNS server, proxy server, and web server. For example, a web server might have the following snippet defined:
httpd mod_ssl mod_python
httpd mod_ssl mod_pythonCopy to Clipboard Copied! Toggle word wrap Toggle overflow To create a web server profile, include the web server snippet in the%packagesection of the kickstart file. For a profile to be both a web server and a proxy server, include both snippets in the package section. To add another package to the web server snippet,mod_perlfor example, update the snippet, and all profiles that are using that snippet are dynamically updated.
Templating allows the defining of a variable to be used throughout 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. So, if a variable is defined at the system level, it will override the same variable defined at the profile or kickstart tree levels. Likewise, if a variable is defined at the profile level, it will override the same variable defined at the kickstart tree level.
Note
Snippets reuse pieces of code between multiple kickstart templates. They can span many lines, and include variables. They can be included in a kickstart profile by using the text $SNIPPET('snippet_name'). A snippet can be made for a package list, for a %post script, or for any text that would normally be included in a kickstart file.
/var/lib/cobbler/snippets/. There is a template from a wizard-style kickstart located in /var/lib/rhn/kickstarts/wizard/, which explains the different default snippets and how they are used.
redhat_register snippet is a default snippet that is used to register machines to a RHN Satellite server as part of a kickstart. It uses a variable called redhat_management_key to register the machine. To use the snippet, set the redhat_management_key variable at either the system, profile, or distribution level and then add $SNIPPET('redhat_register') to a %post section of the kickstart. Any wizard-style kickstarts that are generated by the RHN Satellite server will already include this snippet in the %post section.
/var/lib/rhn/kickstarts/snippets/ directory. RHN Satellite stores snippets for different organizations in different directories, so custom snippets will be stored with a filename similar to the following, where 1 is the organization ID:
$SNIPPET('spacewalk/1/snippet_name')
$SNIPPET('spacewalk/1/snippet_name')
Note
Figure 2.3. Kickstart Snippets
The $ and # characters are used during templating for specifying variables and control flow. If these characters are needed for any other purpose in a script, they will need to be escaped so that they are not recognized as a variable. This can be achieved in several ways:
- Placing a backslash character (
\) before every instance of$or#that needs to be ignored during templating. - Wrap the entire script in
#raw ... #end rawAll%preand%postscripts created using the wizard-style kickstarts are wrapped with#raw...#end rawby default. This can be toggled using the Template checkbox available when editing a%postor%prescript. - Include
#errorCatcher Echoin the first line of the snippet.
Example 2.1. Escaping Special Characters in templates
%post section:
%post echo $foo > /tmp/foo.txt
%post
echo $foo > /tmp/foo.txt
$ being escaped, the templating engine will try to find a variable named $foo and would fail because foo does not exist as a variable.
$ is by using a backslash character (\):
%post echo \$foo > /tmp/foo.txt
%post
echo \$foo > /tmp/foo.txt
\$foo to be rendered as $foo.
#raw ... #end raw, as follows
%post #raw echo $foo > /tmp/foo.txt #end raw
%post
#raw
echo $foo > /tmp/foo.txt
#end raw
#errorCatcher Echo in the first line of the kickstart template. 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, and can be included in any raw kickstarts created manually.