Jump to contentJump to page navigation: previous page [access key p]/next page [access key n]
Applies to openSUSE Leap 15.6

8 Combining ERB templates and scripts Edit source

Section 4.31.1, “Pre-scripts” already describes how to use a pre-script to modify the current profile. In a nutshell, if the script creates a /tmp/profile/modified.xml file, AutoYaST imports that profile and forgets about the initial one.

This is a pretty flexible approach and the only limitation is that you need to rely on the languages and libraries that are available in the installation media.

8.1 Embedding ERB in your scripts Edit source

Unlike with rules, it is possible to combine ERB templates with scripts. AutoYaST will evaluate any ERB tag that you include in your script before running it. This behavior only applies to the scripts defined inside the profile and not to the external ones.

The script in the example below downloads a profile whose name is based on the MAC address. Saving the file as /tmp/profile/modified.xml will cause AutoYaST to load and use the downloaded profile.

Example 8.1: Using the MAC address to get the profile
<scripts>
  <pre-scripts config:type="list">
    <script>
      <interpreter>shell</interpreter>
      <filename>load_profile.sh</filename>
      <% mac = network_cards.first >
      <source><![CDATA[#!/bin/bash
wget -O /tmp/profile/modified.xml http://myserver/<%= network_cards.first[:mac] %>.xml
]]></source>
    </script>
  </pre-scripts>
</scripts>

8.2 Accessing ERB helpers from Ruby scripts Edit source

It is possible to use the ERB helpers in Ruby scripts. To use those helpers, you need to require the yast and autoinstall/y2erb libraries and use the Y2Autoinstall::Y2ERB::TemplateEnvironment class to access them.

Example 8.2: Accessing ERB helpers from a Ruby script
<scripts>
  <pre-scripts config:type="list">
    <script>
      <interpreter>/usr/bin/ruby</interpreter>
      <filename>load_profile.rb</filename>
      <source><![CDATA[#!/usr/bin/env ruby
require "yast"
require "autoinstall/y2erb"
helpers = Y2Autoinstallation::Y2ERB::TemplateEnvironment.new
# Now you can use the TemplateEnvironment instance to call the helpers
disk_devices = helpers.disks.map { |d| d[:device] }
File.write("/root/disks.txt", disk_devices.join("\n"))
]]></source>
    </script>
  </pre-scripts>
</scripts>
Print this page