Ed *_*net 6 puppet puppetmaster
我有几台通过 puppet 管理的机器。这些机器位于不同的物理位置。每个位置都有一个特定的位置编号。
我有一个配置文件,需要在其中包含正确的位置编号。
所以/etc/abc.conf的内容是这样的:
this=that
location=$LOCATION_NUMBER
bananas=tasty
Run Code Online (Sandbox Code Playgroud)
当文件被推送到机器时,我需要 $LOCATION_NUMBER 实际上是位置号。
我可以用 puppet 管理这个文件吗?如何管理?
如果我为 LOCATION_NUMBER 导出一个因子,当 puppet 将文件下推到机器时,它可以写出正确的位置号吗?
谢谢。
Kyl*_*ith 11
您要执行的操作称为模板。这是 Puppet 的核心功能,非常有用。您以 eRB 格式编写文件,该文件可以使用 Ruby 代码和 Facter 变量来构建文件。下面是一个例子:
file { '/etc/my_config_file':
ensure => present,
owner => 'root',
group => 'root',
mode => 0700,
content => template('module/my_config_file.erb'),
}
Run Code Online (Sandbox Code Playgroud)
现在,在templates/
( not files/
)下的模块层次结构中,放置一个文件my_config_file.erb
:
this=that
location=<%= LOCATION_NUMBER %>
bananas=tasty
Run Code Online (Sandbox Code Playgroud)
该<%= %>
格式表示执行一些 Ruby 代码并返回结果。在这种情况下,所有事实都可用作局部变量,例如ipaddress
,lsbmajdistrelease
或LOCATION_NUMBER
(您的自定义事实)。您还可以使用任何其他不直接在 内部返回结果的 Ruby 代码<% %>
,例如if
语句:
<% if LOCATION_NUMBER == 7 %>
custom_config=true
<% else %>
custom_config=false
<% end %>
Run Code Online (Sandbox Code Playgroud)
编辑:当我重新阅读此答案时,我建议您不要在 Fact 名称中使用大写字母。在 Ruby 中,以大写字母开头的变量是不可变的。虽然我不确定这是否真的是一个问题,但它违反了惯例。