Red Hat Training

A Red Hat training course is available for Red Hat Satellite

Appendix A. Template Writing Reference

Embedded Ruby (ERB) is a tool for generating text files based on templates that combine plain text with Ruby code. Red Hat Satellite uses ERB syntax in provisioning templates (Section 9.3.9, “Provisioning Templates” and Creating Provisioning Templates in the Red Hat Satellite Provisioning Guide), remote execution job templates (Chapter 12, Running Jobs on Satellite Hosts), templates for partition tables (Section 9.3.8, “Partition Tables”), and smart parameters (Section 9.2.2, “Configuring Smart Variables”). This section provides an overview of Satellite specific functions and variables that can be used in ERB templates along with some usage examples. Note that the default templates provided by Red Hat Satellite (HostsProvisioning templates, HostsJob templates) also provide a good source of ERB syntax examples.
When provisioning a host or running a remote job, the code in the ERB is executed and the variables are replaced with the host specific values. This process is referred to as rendering. The Satellite Server has the safemode rendering option enabled by default, which prevents any harmful code being executed from templates.

A.1. Writing ERB Templates

The following points summarize the ERB syntax:
  • <% %> – marks enclosing Ruby code within the ERB template. The code is executed when the template is rendered. It can contain Ruby control flow structures as well as Satellite specific functions and variables. For example:
    <% if @host.operatingsystem.family == "Redhat" && @host.operatingsystem.major.to_i > 6 %>
    systemctl <%= input("action") %> <%= input("service") %>
    <% else %>
    service <%= input("service") %> <%= input("action") %>
    <% end -%>
  • <%= %> – the code output is inserted into the template. This is useful for variable substitution, for example:
    echo <%= @host.name %>
  • <% -%>, <%= -%> – by default, a newline character is inserted after a Ruby block if it is closed at the end of a line. To suppress this behavior, modify the enclosing mark. For example, the following template:
    curl <%= @host.ip -%>
    /mydir
    is rendered the same as:
    curl <%= @host.ip %>/mydir
    In practice, this is used to reduce the number of lines in rendered templates (where Ruby syntax permits).
  • <%# %> – marks enclosing a comment that will be ignored during template rendering:
    <%# A comment %>