根据事实将条目添加到主机文件

add entries to hosts file based on facts

在 Puppet 中,我想为一大组服务器中的所有主机文件创建条目。

256.344.987.776     6.fqn.mycompany.info my-hosts-hostname6
256.344.987.777     7.fqn.mycompany.info my-hosts-hostname7
256.344.987.778     8.fqn.mycompany.info my-hosts-hostname8
256.344.987.779     9.fqn.mycompany.info my-hosts-hostname9
256.344.987.780     10.fqn.mycompany.info my-hosts-hostname10

IP 取自 eth2 fact,fqn 取自 fact hostname 到 domain,简称为 fact: hostname。

我不确定如何最好地解决这个问题。

听起来您想从您的所有主机收集信息,整理它,并将其提供给所有主机。这是导出资源的 classic 用例之一。当然,Puppet 提供了一个内置的 Host 资源类型来管理各个条目。处理此类工作的最小 class 可能如下所示:

class site::hosts {

  # Export *this* host's entry for all machines to pick up
  @@host { "${hostname}.${domain}":
    ensure => 'present',
    ip => $ipaddress_eth2,
    host_aliases => ${hostname}
  }

  # Apply *all* machines' hosts entries to this machine
  Host<<| |>>
}

您需要在您的主机上启用导出资源才能运行。在您首次将其安装到位后,可能需要几个周期才能稳定下来,就像在任何给定的 运行 上一样,每个主机将仅接收由已经收到目录 class 的机器提供的条目在他们里面。