Project

General

Profile

TemplateWriting » History » Version 71

Lukas Zapletal, 01/22/2015 08:38 AM
Changes for http://projects.theforeman.org/issues/2948

1 25 Paul Kelly
{{toc}}
2 26 Paul Kelly
3 1 Justin Sherrill
h1. Templates
4
5
6
7 16 Justin Sherrill
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.
8 15 Justin Sherrill
9 30 Martin Hovmöller
To enable safemode, set "safemode_render" to "true" in Settings -> Foreman settings.  Safemode rendering prevents templates from reading and writing files on the file system or modifying application data within foreman.  
10 1 Justin Sherrill
11
12 8 Justin Sherrill
h2. Accessing Templates
13 1 Justin Sherrill
14 10 Justin Sherrill
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.  
15 1 Justin Sherrill
16 10 Justin Sherrill
17 8 Justin Sherrill
h3. Host-based Rendering
18
19 13 Justin Sherrill
Only a single template of each type may be rendered for a system.  Foreman determines which template to use following these rules:
20 1 Justin Sherrill
21 13 Justin Sherrill
1. Only the templates of the appropriate kind and associated with the hosts operating system are considered
22
2. If a template has a hostgroup/environment combination that matches that of the host, use this template else
23
3. If a template is associated with the environment of the host, use this template, else
24
4. Use the first template found associated with the operating system associated with the host.
25 1 Justin Sherrill
26 13 Justin Sherrill
So essentially, the hostgroup/environment combination is used first, then just the environment, and finally just the operating system of the host.
27
28
29 1 Justin Sherrill
To access a template of a certain type simply use this url:
30
31 13 Justin Sherrill
@/unattended/KIND_NAME@
32 1 Justin Sherrill
33 13 Justin Sherrill
For example:    @/unattended/provision@
34 12 Justin Sherrill
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:
35
36 13 Justin Sherrill
@/unattended/provision?spoof=192.168.0.1@
37 12 Justin Sherrill
38 70 Dominic Cleal
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.
39 1 Justin Sherrill
40 17 Justin Sherrill
h3. Hostgroup-based rendering
41 9 Justin Sherrill
42 70 Dominic Cleal
Allows any template to be rendered for any host group.  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:
43 8 Justin Sherrill
44
/unattended/template/Template Name/Hostgroup Name
45 1 Justin Sherrill
46
For example, a hostgroup of name Finance, and a template named WebServerKickstart could be rendered using the url:
47 8 Justin Sherrill
48
/unattended/template/WebServerKickstart/Finance
49 70 Dominic Cleal
50
When building the default PXE template (from the Provisioning Templates page), any host group with the operating system, installation media and architecture set, plus provisioning templates with the host group set on the Associations tab will be listed at the bottom of the menu.
51 1 Justin Sherrill
52 33 Ohad Levy
h3.  PXE Menus
53 10 Justin Sherrill
54 33 Ohad Levy
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(see above).  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.
55 8 Justin Sherrill
56
h2. Writing templates
57 17 Justin Sherrill
58
Templates can be written using common ERB style templating.  Here's an example on using a variable/function:
59
60 46 Tomas Edwardsson
@rootpw --iscrypted <%= root_pass %>@
61 17 Justin Sherrill
62
Using a simple conditional:
63
64 46 Tomas Edwardsson
@<%= "selinux --disabled\n" if @osver != 3 -%>@
65 17 Justin Sherrill
This would include a line to disable selinux if the operating system version is not 3 (since Selinux isn't supported on RHEL 3)
66
67
Another way to do conditional (with if/elsif/else):
68
69 18 Justin Sherrill
<pre>
70 46 Tomas Edwardsson
<% if @osver == 5 -%>
71 17 Justin Sherrill
 echo "uses yum"
72 46 Tomas Edwardsson
<% elsif @osver == 4 -%>
73 17 Justin Sherrill
 echo "uses up2date
74 46 Tomas Edwardsson
<% else -%>
75 17 Justin Sherrill
 echo "I'm not sure"
76 46 Tomas Edwardsson
<% end -%>
77 18 Justin Sherrill
</pre>
78 8 Justin Sherrill
79
h2. Functions and macros:
80
81 1 Justin Sherrill
|_.Name|_.Description|_.Example|
82
|root_pass  |The root password configured for the system| |
83
|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 |
84 19 Justin Sherrill
|snippet(name)   | Renders the specified snippet | |
85 24 Justin Sherrill
|foreman_url(kind)| Provides the full URL to a host-rendered template of the given kind  | foreman_url("provision") =>  http://HOST/unattended/provision |
86 1 Justin Sherrill
|@host.name                |The (full) name of the host| |
87 38 Greg Sutcliffe
|@host.shortname           |The (short) name of the host| |
88 46 Tomas Edwardsson
|@host.certname            |The SSL certificate name of the host| |
89 38 Greg Sutcliffe
|@host.domain           |The domain of the host| |
90 39 Ben Hagen
|@host.ip                |The IP address of the host| |
91 71 Lukas Zapletal
|@host.hostgroup       |The hostgroup of the host| <% if @host.hostgroup.to_s == "Base/Application Servers" -%>...<% end -%>|
92 28 Howard Jones
|@host.mac                |The HW address of the host| |
93 32 Florian Koch
|@host.diskLayout     |The disklayout of the host (could come from the operating system)| |
94 71 Lukas Zapletal
|@host.ptable        |The disklayout name | |
95 1 Justin Sherrill
|@host.puppetmaster   |The puppetmaster the host should use | |
96 71 Lukas Zapletal
|@host.environment  |The environment the host should use | |
97 59 Arnold Bechtoldt
|@host.location    |The location of the host | |
98 34 Frank Sweetser
|@host.architecture   |The arch of the host (i.e. x86_64)| |
99 21 Justin Sherrill
|@host.operatingsystem.name|The operating system name| |
100 1 Justin Sherrill
|@host.operatingsystem.major |The major version of the OS||
101
|@host.operatingsystem.minor |The minor version of the OS||
102 65 Dominic Cleal
|@host.operatingsystem.family |The OS Family (I.e. Redhat, Debian, etc.)||
103 42 Trey Dockendorf
|@host.subnet.mask |The subnet mask of the host||
104
|@host.subnet.gateway |The gateway of the host||
105
|@host.subnet.dns_primary |The primary DNS of the host||
106
|@host.subnet.dns_secondary |The secondary DNS of the host||
107 58 Benjamin Papillon
|@host.subnet.dhcp|Boolean that render true if a dhcp proxy is configured for this host|<% if @host.subnet.dhcp %>...<% else %>...<% end %>|
108 21 Justin Sherrill
|@host.url_for_boot(:kernel) |Full url to the kernel associated with this host.||
109
|@host.url_for_boot(:initrd) |Full url to the initrd image associated with this host||
110 44 Siert Z.
|@host.sp_name|The name of the BMC interface ||
111
|@host.sp_ip|The IP address of the BMC interface ||
112
|@host.sp_mac|The HW address of the BMC interface ||
113
|@host.sp_subnet|Subnet of the BMC network ||
114 56 Stefan Lasiewski
|@host.interfaces| Contains an array of all available secondary and BMC interfaces except primary (only works if safemode_render=false) ||
115
|@host.interfaces.managed| Limit the array to managed (non-BMC) interfaces only (only works if safemode_render=false) ||
116
|@host.interfaces.bmc| Limit the array to BMC (non-managed) interfaces only (only works if safemode_render=false) ||
117 1 Justin Sherrill
|@host.interfaces.size| Number of additional interfaces (either type interface or bmc) (only works if safemode_render=false) |size=1 means a host has two interfaces|
118
|@host.interfaces.empty|If true host has only one interface (only works if  safemode_render=false) |use !@host.interfaces.empty in test to do something only if host has two or more interfaces|
119 71 Lukas Zapletal
|@host.facts| Contains a hash of facts from Facter/Ohai etc. (only works if safemode_render=false) ||
120
|@host.facts['ipaddress']| Accessing the 'ipaddress' fact for the host (only works if safemode_render=false) ||
121 66 Dominic Cleal
|@host.image_build?| Returns true if the host is provisioned using an image (1.5+)| <% if @host.image_build? %> |
122
|@host.pxe_build?| Returns true if the host is provisioned using the network/PXE (1.5+)| <% if @host.pxe_build? %> |
123 67 Dominic Cleal
|@provisioning_type|Equal to 'host' or 'hostgroup' depending on type of provisioning (1.6+)| <% if @provisioning_type == 'hostgroup' %> |
124 57 Benjamin Papillon
|@static|Boolean value to determine if the network configuration is static or dynamic|Use <%= @static ? "static" : "dynamic" %> in templates|
125 69 Dominic Cleal
|@template_name|Name of the template being rendered (1.7+)| <%= @template_name %> |
126 1 Justin Sherrill
127
*NOTE*: For *PXELinux* templates in 0.4 or older _only_ leave off the '@host.' from the above as it is currently generated slightly different from everything else. 
128
129 55 Stefan Lasiewski
*NOTE*: The interface array can be parsed this way: <pre><% @host.interfaces.each do |i| %> key is <%= i.ip %> <% end %></pre>. To print information for the first interface in the array, try something like: <pre><% myinterface = @host.interfaces.first %> IP: <%= myinterface.ip %> Netmask: <%= myinterface.subnet.mask %> Gateway: <%= myinterface.subnet.gateway %></pre>
130 53 Dominic Cleal
131 1 Justin Sherrill
*NOTE*: Some variables can't be accessed while safemode is enabled.  Disable via safemode_render in _More > Settings_, though be aware this enables arbitrary code to be added to templates.  Some are being whitelisted via #2948.
132 57 Benjamin Papillon
133 68 Dominic Cleal
*NOTE*: You may need to add "&static=yes" to the foreman_url to use the @static parameter in templates (see [[KickstartStatic]]).
134 1 Justin Sherrill
135 21 Justin Sherrill
h3. Kickstart only variables (Only available for provision templates and "RedHat" Family operating systems):
136 1 Justin Sherrill
137
|_.Name|_.Description|_.Example|
138 21 Justin Sherrill
|@osver |The OS Major Version (Same as @host.operatingsystem.major)||
139
|@arch | The architecture (same as @host.architecture.name)  ||
140
|@mediapath | The full kickstart line to provide the url command.|@mediapath => http://file.example.com/RHEL-5-Server/U5/x86_64/os/|
141 46 Tomas Edwardsson
|@epel |A command which will automatically install the correct version of the epel-release rpm.  Use full in a %post script.|@epel => "su -c 'rpm -Uvh http://download.fedora.redhat.com/pub/epel/5/x86_64/epel-release-5-4.noarch.rpm'"|
142
|@dynamic |true if the parition table being used is a %pre script (has #Dynamic as the first line of the table)|<% if @dynamic -%>|
143 1 Justin Sherrill
144 21 Justin Sherrill
h3. Preseed attributes (Only available for provision templates and "Debian" Family operating systems)::
145 6 Justin Sherrill
146 1 Justin Sherrill
|_.Name|_.Description|_.Example|
147
|@preseed_path |||
148
|@preseed_server |||
149 27 Alexander Rusa
150
h3. Host- or Hostgroup parameters
151
152 35 David M.
@host.params['parameter_name']
153
154 45 Mikael Fridh
Hint: For Foreman older than 1.0 you have to disable Safemode-Rendering in More->Settings.
155 36 Adrian Bridgett
156 41 Ohad Levy
h3. Compute Resource Specific functions
157
158
see [[Compute Resources#Templates]]
159
160 36 Adrian Bridgett
h3. Settings
161
162
Setting.setting_name
163
164
You will need to disable Safemode-Rendering in More->Settings.