TemplateWriting » History » Revision 20
Revision 19 (Justin Sherrill, 02/02/2011 05:17 PM) → Revision 20/110 (Justin Sherrill, 02/02/2011 05:17 PM)
h1. Templates
The following functions and macros can be used within templates. These are guaranteed to work via the safemode rendering, to ensure a template can do nothing harmful. With safemode disabled other macros may work, but are not supported at this time.
To ensure that safemode is enabled, set ":safemode_render = true" in settings.yml. Safemode rendering prevents templates from reading and writing files on the file system or modifying application data within foreman.
h2. Accessing Templates
There are two ways to render a template, based on a single host, or based on a Hostgroup. The host or hostgroup provides all the details with which to render the template.
h3. Host-based Rendering
Only a single template of each type may be rendered for a system. Foreman determines which template to use following these rules:
1. Only the templates of the appropriate kind and associated with the hosts operating system are considered
2. If a template has a hostgroup/environment combination that matches that of the host, use this template else
3. If a template is associated with the environment of the host, use this template, else
4. Use the first template found associated with the operating system associated with the host.
So essentially, the hostgroup/environment combination is used first, then just the environment, and finally just the operating system of the host.
To access a template of a certain type simply use this url:
@/unattended/KIND_NAME@
For example: @/unattended/provision@
would render the provisioning template. The host will be based on the IP Address it is accessed from. To spoof a template simply access the url in this manner:
@/unattended/provision?spoof=192.168.0.1@
where 192.168.0.1 is the ip address of the system you want to spoof. This allows you to view a template for a particular system from anywhere.
h3. Hostgroup-based rendering
Allows any template to be rendered for any Hostgroup. When rendering using a hostgroup, @host is actually the hostgroup instead of a defined host. The default values for the hostgroup are used for templated values. This means if a value is not set in the hostgroup, you may get an error when rendering the template. To access a template using a Hostgroup to render, simply use this URL:
/unattended/template/Template Name/Hostgroup Name
For example, a hostgroup of name Finance, and a template named WebServerKickstart could be rendered using the url:
/unattended/template/WebServerKickstart/Finance
h4. PXE Menus
Pxe Menus can be deployed to smart proxies from the Config Templates list page (/config_templates). All provision templates will be added with each of their hostgroup combinations. For example if the template "WebServerKickstart" is associated to the Hostgroup1/Production, Hostgroup2/Production, and Hostgroup2/Testing combinations, the template would only be added twice. Once for WebServerKickstart-Hostgroup1 and WebServerKickstart-Hostgroup2.
h2. Writing templates
Templates can be written using common ERB style templating. Here's an example on using a variable/function:
@rootpw --iscrypted <%25= root_pass %25>@
Using a simple conditional:
@<%25= "selinux --disabled\n" if @osver != 3 -%25>@
This would include a line to disable selinux if the operating system version is not 3 (since Selinux isn't supported on RHEL 3)
Another way to do conditional (with if/elsif/else):
<pre>
<%25 if @osver == 5 -%25>
echo "uses yum"
<%25 elsif @osver == 4 -%25>
echo "uses up2date
<%25 else -%25>
echo "I'm not sure"
<%25 end -%25>
</pre>
h2. Functions and macros:
|_.Name|_.Description|_.Example|
|root_pass |The root password configured for the system| |
|ks_console |A string assembled using the port and baud of the host which can be added to a kernel line | ks_console => console=ttyS1,9600 |
|snippets(name) | Renders the specified snippet | |
|foreman_url(kind)| Provides the full URL to a host-rendered template of the given kind | foreman_url("provision") => http://HOST/unattended/provision |
|@host |The name of the host| |
|@host.name |The name of the host| |
|@host.diskLayout |The disklayout of the host (could come from the operating system)| |
|@host.puppetmaster |The puppetmaster the host should use | |
|@host.architecture |The arch of the host (i.e. x86_64)| |
|@host.operatingsystem|The operating system name| |
|@host.operatingsystem.media_url |||
|@host.operatingsystem.major |The major version of the OS||
|@host.operatingsystem.minor |The minor version of the OS||
|@host.operatingsystem.family |The OS Family (I.e. redhat, debian, etc.)||
|@host.url_for_boot(:kernel) |||
|@host.url_for_boot(:initrd) |||
h3. Kickstart only variables:
|_.Name|_.Description|_.Example|
|@dynamic |||
|@osver |||
|@arch |||
|@mediapath |||
|@epel |||
|@yumrepo |||
|@static |||
h3. Preseed attributes:
|_.Name|_.Description|_.Example|
|@preseed_path |||
|@preseed_server |||