适用范围 SUSE Linux Enterprise Server 15 SP6
8 合并 ERB 模板和脚本 #
第 4.32.1 节 “前脚本”已介绍了如何使用前脚本修改当前配置文件。简而言之,如果该脚本创建了 /tmp/profile/modified.xml 文件,则 AutoYaST 会导入该配置文件并忽略最初的那个文件。
这种方法非常灵活,唯一的局限是,您需要依赖于安装媒体中提供的语言和库。
8.1 在脚本中嵌入 ERB #
可以将 ERB 模板与脚本合并,这与规则不同。AutoYaST 将会在运行脚本前评估您在脚本中包含的任何 ERB 标记。此行为仅适用于在配置文件内定义的脚本,不适用于外部脚本。
下面的示例中的脚本会下载一个名称基于 MAC 地址的配置文件。将该文件保存为 /tmp/profile/modified.xml 会使 AutoYaST 下载并使用下载的配置文件。
例 8.1︰ 使用 MAC 地址获取配置文件 #
<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 通过 Ruby 脚本访问 ERB 帮助程序 #
可以在 Ruby 脚本中使用 ERB 帮助程序要使用这些帮助程序,您需要 require yast 和 autoinstall/y2erb 库,并使用 Y2Autoinstall::Y2ERB::TemplateEnvironment 类访问它们。
例 8.2︰ 通过 Ruby 脚本访问 ERB 帮助程序 #
<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>