使用 Puppet 仅管理配置文件的一部分(多行 file_line)

Ale*_*lex 6 puppet

有一个配置文件,例如 /etc/network/interfaces。我只想使用 Puppet 管理该文件的某个部分。

例子:

# At the beginning at least is some dynamic content that can be different
# from machine to machine and changed by an admin manually
auto eth0
iface eth0 inet static
    address 192.168.108.5
    netmask 255.255.255.0

# BEGIN PUPPET WLAN0
allow-hotplug wlan0
iface wlan0 inet static
    address 192.168.109.5
    netmask 255.255.255.0
# END PUPPET WLAN0

# Potentially more stuff that I do not want to touch through Puppet
# But could be static if it makes it more easy. E.g. simply force sections to end of file.
Run Code Online (Sandbox Code Playgroud)

stdlib file_line 资源非常接近我需要的资源。用于替换匹配项的正则表达式和内容。然而,不幸的是,没有办法让 file_line 匹配多行并替换为新内容。

备选方案 1:创建一个 bash 脚本:丑陋的 Exec 资源和临时文件。

备选方案 2:使用 Augeas:对于简单的搜索和替换来说似乎有点过分。

除了 stdlib 之外,还有其他模块允许我在多行上进行正则表达式匹配和替换吗?

有没有比兼顾临时 bash 脚本和 Exec 资源更好的手工解决方案?

Jos*_* R. 0

如果你能保证其他文件内容是静态的,你可以将该文件制作成模板并保持静态部分不变。例子:

auto eth0
iface eth0 inet static
address 192.168.108.5
netmask 255.255.255.0

# BEGIN PUPPET WLAN0
allow-hotplug wlan0
iface wlan0 inet static
    address <%= wlan0_address %>
    netmask <%= wlan0_netmask %>
# END PUPPET WLAN0

# More stuff that I do not want to touch through Puppet
# Static content here
Run Code Online (Sandbox Code Playgroud)

此示例假设您已在其他地方定义了$wlan0_address变量$wlan0_netmask

然后您可以使用文件资源进行部署:

file{'interfaces':
        ensure  => file,
        path    => '/etc/network/interfaces',
        content => template('/path/to/template'),
}
Run Code Online (Sandbox Code Playgroud)