在 Puppet 中将一段文本添加到 /etc/hosts 文件的最佳方式是什么?

ujj*_*ain 8 puppet

我还想保留手动编辑主机文件的能力,至少前 10 行。

#Public IP's - eth0
192.168.1.103   front-01
192.168.1.106   front-02

#Private IP's - eth1
192.169.40.201  priv0-0
192.169.40.202  priv0-1
192.169.40.207  priv1-0
192.169.40.208  priv1-1

#Virtual IP's - eth0:1
192.169.50.202  vip-01
192.169.50.205  vip-02
Run Code Online (Sandbox Code Playgroud)

将这些主机条目放在 /etc/hosts 的底部将是完美的。做这个的最好方式是什么?有没有比编写 8 个主机行清单更好的方法?

# create a simple hostname and ip host entry
host { 'front-01':
    ip => '192.168.1.103',
}
Run Code Online (Sandbox Code Playgroud)

可能有些服务器组在其 /etc/hosts 中需要不同的 IP 的 /hostnames。我会使用模板,但这意味着人们不能再在他们的 /etc/hosts 中进行手动更改,因为他们会被模板覆盖。

asc*_*hil 14

老实说,使用host资源是最简单的方法。您只需要定义希望由 puppet 控制的主机,并且您仍然可以手动编辑文件的其余部分(即使 Puppet 插入了告诉您不要这样做的标题中)。

augeas模块对于主机文件来说太过分了,因为它只是复制了host资源的功能(尽管它没有添加到“不要编辑此文件”标题中)。

如果你真的想要更复杂的东西,或者你想要很好地控制文件中行的位置,请使用concat 模块和片段之一的本地源。concat文档中有一个关于此类事情的示例(使用 motd 文件)。

但实际上,只需将host资源用于您要从 Puppet 定义的主机,并编辑本地主机文件以用于您需要的任何其他内容。

另请注意,您可以在 Puppet 中非常紧凑地编写主机定义:

host {
  # Public IPs - eth0
  'front-01': ip => '192.168.1.103';
  'front-02': ip => '192.168.1.106';

  # Private IPs - eth1
  'priv0-0': ip => '192.169.40.201';
  'priv0-1': ip => '192.169.40.202';
  'priv1-0': ip => '192.169.40.207';
  'priv1-1': ip => '192.169.40.208';

  # Virtual IPs - eth0:1
  'vip-01': ip => '192.169.50.202';
  'vip-02': ip => '192.169.50.205';
}
Run Code Online (Sandbox Code Playgroud)