TemplateWriting
Version 40 (Ohad Levy, 06/19/2012 03:56 am)
| 1 | 25 | Paul Kelly | {{toc}} |
|---|---|---|---|
| 2 | 26 | Paul Kelly | |
| 3 | 1 | h1. Templates |
|
| 4 | 1 | ||
| 5 | 1 | ||
| 6 | 1 | ||
| 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 | ||
| 11 | 1 | ||
| 12 | 8 | Justin Sherrill | h2. Accessing Templates |
| 13 | 1 | ||
| 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 | ||
| 16 | 10 | Justin Sherrill | |
| 17 | 8 | Justin Sherrill | h3. Host-based Rendering |
| 18 | 8 | Justin Sherrill | |
| 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 | ||
| 21 | 13 | Justin Sherrill | 1. Only the templates of the appropriate kind and associated with the hosts operating system are considered |
| 22 | 13 | Justin Sherrill | 2. If a template has a hostgroup/environment combination that matches that of the host, use this template else |
| 23 | 13 | Justin Sherrill | 3. If a template is associated with the environment of the host, use this template, else |
| 24 | 13 | Justin Sherrill | 4. Use the first template found associated with the operating system associated with the host. |
| 25 | 1 | ||
| 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 | 13 | Justin Sherrill | |
| 28 | 13 | Justin Sherrill | |
| 29 | 1 | To access a template of a certain type simply use this url: |
|
| 30 | 1 | ||
| 31 | 13 | Justin Sherrill | @/unattended/KIND_NAME@ |
| 32 | 1 | ||
| 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 | 12 | Justin Sherrill | |
| 36 | 13 | Justin Sherrill | @/unattended/provision?spoof=192.168.0.1@ |
| 37 | 12 | Justin Sherrill | |
| 38 | 12 | Justin Sherrill | 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 | 8 | Justin Sherrill | |
| 40 | 8 | Justin Sherrill | |
| 41 | 17 | Justin Sherrill | |
| 42 | 8 | Justin Sherrill | h3. Hostgroup-based rendering |
| 43 | 9 | Justin Sherrill | |
| 44 | 8 | Justin Sherrill | 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: |
| 45 | 8 | Justin Sherrill | |
| 46 | 8 | Justin Sherrill | /unattended/template/Template Name/Hostgroup Name |
| 47 | 8 | Justin Sherrill | |
| 48 | 8 | Justin Sherrill | For example, a hostgroup of name Finance, and a template named WebServerKickstart could be rendered using the url: |
| 49 | 8 | Justin Sherrill | |
| 50 | 11 | Justin Sherrill | /unattended/template/WebServerKickstart/Finance |
| 51 | 1 | ||
| 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 | 8 | Justin Sherrill | h2. Writing templates |
| 57 | 17 | Justin Sherrill | |
| 58 | 17 | Justin Sherrill | Templates can be written using common ERB style templating. Here's an example on using a variable/function: |
| 59 | 17 | Justin Sherrill | |
| 60 | 17 | Justin Sherrill | @rootpw --iscrypted <%= root_pass %>@ |
| 61 | 17 | Justin Sherrill | |
| 62 | 17 | Justin Sherrill | Using a simple conditional: |
| 63 | 17 | Justin Sherrill | |
| 64 | 17 | Justin Sherrill | @<%= "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 | 17 | Justin Sherrill | |
| 67 | 17 | Justin Sherrill | Another way to do conditional (with if/elsif/else): |
| 68 | 17 | Justin Sherrill | |
| 69 | 18 | Justin Sherrill | <pre> |
| 70 | 17 | Justin Sherrill | <% if @osver == 5 -%> |
| 71 | 17 | Justin Sherrill | echo "uses yum" |
| 72 | 17 | Justin Sherrill | <% elsif @osver == 4 -%> |
| 73 | 17 | Justin Sherrill | echo "uses up2date |
| 74 | 17 | Justin Sherrill | <% else -%> |
| 75 | 17 | Justin Sherrill | echo "I'm not sure" |
| 76 | 17 | Justin Sherrill | <% end -%> |
| 77 | 18 | Justin Sherrill | </pre> |
| 78 | 8 | Justin Sherrill | |
| 79 | 8 | Justin Sherrill | |
| 80 | 8 | Justin Sherrill | |
| 81 | 8 | Justin Sherrill | h2. Functions and macros: |
| 82 | 8 | Justin Sherrill | |
| 83 | 1 | |_.Name|_.Description|_.Example| |
|
| 84 | 1 | |root_pass |The root password configured for the system| | |
|
| 85 | 19 | Justin Sherrill | |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 | |
| 86 | 24 | Justin Sherrill | |snippet(name) | Renders the specified snippet | | |
| 87 | 1 | |foreman_url(kind)| Provides the full URL to a host-rendered template of the given kind | foreman_url("provision") => http://HOST/unattended/provision | |
|
| 88 | 38 | Greg Sutcliffe | |@host.name |The (full) name of the host| | |
| 89 | 38 | Greg Sutcliffe | |@host.shortname |The (short) name of the host| | |
| 90 | 39 | Ben Hagen | |@host.domain |The domain of the host| | |
| 91 | 28 | Howard Jones | |@host.ip |The IP address of the host| | |
| 92 | 32 | Florian Koch | |@host.mac |The HW address of the host| | |
| 93 | 1 | |@host.diskLayout |The disklayout of the host (could come from the operating system)| | |
|
| 94 | 1 | |@host.puppetmaster |The puppetmaster the host should use | | |
|
| 95 | 34 | Frank Sweetser | |@host.architecture |The arch of the host (i.e. x86_64)| | |
| 96 | 21 | Justin Sherrill | |@host.operatingsystem.name|The operating system name| | |
| 97 | 1 | |@host.operatingsystem.major |The major version of the OS|| |
|
| 98 | 1 | |@host.operatingsystem.minor |The minor version of the OS|| |
|
| 99 | 1 | |@host.operatingsystem.family |The OS Family (I.e. redhat, debian, etc.)|| |
|
| 100 | 21 | Justin Sherrill | |@host.url_for_boot(:kernel) |Full url to the kernel associated with this host.|| |
| 101 | 21 | Justin Sherrill | |@host.url_for_boot(:initrd) |Full url to the initrd image associated with this host|| |
| 102 | 1 | ||
| 103 | 21 | Justin Sherrill | h3. Kickstart only variables (Only available for provision templates and "RedHat" Family operating systems): |
| 104 | 1 | ||
| 105 | 1 | |_.Name|_.Description|_.Example| |
|
| 106 | 21 | Justin Sherrill | |@osver |The OS Major Version (Same as @host.operatingsystem.major)|| |
| 107 | 21 | Justin Sherrill | |@arch | The architecture (same as @host.architecture.name) || |
| 108 | 21 | Justin Sherrill | |@mediapath | The full kickstart line to provide the url command.|@mediapath => http://file.example.com/RHEL-5-Server/U5/x86_64/os/| |
| 109 | 21 | Justin Sherrill | |@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'"| |
| 110 | 31 | Joshua Hoblitt | |@dynamic |true if the parition table being used is a %pre script (has #Dynamic as the first line of the table)|<% if @dynamic -%>| |
| 111 | 1 | ||
| 112 | 21 | Justin Sherrill | h3. Preseed attributes (Only available for provision templates and "Debian" Family operating systems):: |
| 113 | 6 | Justin Sherrill | |
| 114 | 1 | |_.Name|_.Description|_.Example| |
|
| 115 | 1 | |@preseed_path ||| |
|
| 116 | 1 | |@preseed_server ||| |
|
| 117 | 27 | Alexander Rusa | |
| 118 | 27 | Alexander Rusa | h3. Host- or Hostgroup parameters |
| 119 | 27 | Alexander Rusa | |
| 120 | 35 | David M. | @host.params['parameter_name'] |
| 121 | 35 | David M. | |
| 122 | 35 | David M. | Hint: You may have to disable Safemode-Rendering in More->Settings. |
| 123 | 36 | Adrian Bridgett | |
| 124 | 36 | Adrian Bridgett | h3. Settings |
| 125 | 36 | Adrian Bridgett | |
| 126 | 36 | Adrian Bridgett | Setting.setting_name |
| 127 | 36 | Adrian Bridgett | |
| 128 | 36 | Adrian Bridgett | You will need to disable Safemode-Rendering in More->Settings. |
