Jump to contentJump to page navigation: previous page [access key p]/next page [access key n]
Applies to SUSE Linux Enterprise Micro 5.0

7 ERB templates Edit source

ERB templates allow embedding some Ruby code within the profile to modify it during the installation. With this approach, you can inspect the system and adjust the profile by setting values, adding or skipping sections, etc.

To activate the ERB processing, the profile must have the extension .erb (for example, autoyast.xml.erb). Hence, it is not possible to combine rules/classes and ERB templates.

7.1 What is ERB? Edit source

ERB stands for Embedded Ruby and allows using the power of the Ruby programming language to generate different kind of contents. With ERB, you can include some Ruby code in your profiles to adapt them at runtime depending on the installation system.

When using ERB, the Ruby code is enclosed between <% and > signs. If you want the output of the command to be included in the resulting profile, you simply need to add an equal sign (=).

Example 7.1: Including a file using ERB
<bootloader>
  <% require "open-uri" %>
  <%= URI.open("http://192.168.1.1/profiles/bootloader-common.xml").read %>
</bootloader>

AutoYaST offers a small set of helper functions to retrieve information from the underlying system, like disks or network_cards. You can check the list of helpers and their values in the Section 7.2, “Template helpers” section.

7.2 Template helpers Edit source

Template helpers are sets of Ruby methods that can be used in the profiles to retrieve information about the installation system.

7.2.1 disks Edit source

The disks helper returns a list of the detected disks. Each element of the list contains some basic information like the device name or the size.

Key

Type

Value

:device

String

Device kernel name (for example, sda).

:model

String

Disk model

:serial

String

Serial number

:size

Integer

Disk size (in bytes)

:udev_names

Array<String>

List of disk udev names. You can use any of them to refer to the device.

:vendor

String

Disk vendor's name

The profile in the example below installs the system on the largest disk. It sorts the list of existing disks by size and takes the last one. Then it uses the :device key as value for the device element.

Example 7.2: Using the largest disk
<partitioning t="list">
  <drive>
    <% disk = disks.sort_by { |d| d[:size] }.last %> <!-- find the largest disk -->
    <device><%= disk[:device] %></device> <!-- print the disk device name -->
    <initialize t="boolean">true</initialize>
    <use>all</use>
  </drive>
</partitioning>

7.2.2 network_cards Edit source

The network_cards helper returns a list of network cards, including their names, status information (for example, if they are connected or not).

Key

Type

Value

:device

String

Device name (for example, eth0 or enp3s0)

:mac

String

MAC address

:active

Boolean

Whether the device is active or not

:link

Boolean

Whether the device is connected or not

:vendor

String

Disk vendor's name

The following example finds the first network card that is connected to the network and configures it to use DHCP.

Example 7.3: Configure the connected network cards
<interfaces t="list">
  <% with_link = netword_cards.sort_by { |n| n[:name] }.find { |n| n[:link] } %>
  <% if with_link %>
    <interface>
      <device><%= with_link[:device] %></device>
      <startmode>auto</startmode>
      <bootproto>dhcp</bootproto>
      </interface>
  <% end >
</interfaces>

7.2.3 os_release Edit source

The os_release helper returns the operating system information, which is included in the /etc/os-release file.

Key

Type

Value

:id

String

Distribution ID (for example, sles, opensuse-tumbleweed)

:name

String

Distribution name (for example, SLES or openSUSE Tumbleweed)

:version

String

Distribution version (for example, 15.2)

You might use this information to decide which product to install, using pretty much the same profile for all of them (SLE or openSUSE distributions).

Example 7.4: Reusing the same profile for different distributions
<products t="list">
  <% if os_release[:id] == 'sle' %>
  <product>SLES</product>
  <% else %<
  <product>openSUSE</product>
  <% end %<
</products>
Print this page