使用puppet为每个定义的块创建一个文件

使用puppet为每个定义的块创建一个文件,puppet,Puppet,我有一个正在运行的清单,其中我通过设置(exerpt)创建了一个文件夹和一个文件: 及 如何定义这些配置的可变数量: $mesh_if_id = "low", $mesh_mtu_low = 1280, $fastd_low_port = 11280, # fastd port $mesh_if_id = "something", $mesh_mtu_low = 12345, $fastd_low_port = 112345, # fastd port ... 然

我有一个正在运行的清单,其中我通过设置(exerpt)创建了一个文件夹和一个文件:

如何定义这些配置的可变数量:

  $mesh_if_id = "low",
  $mesh_mtu_low = 1280,
  $fastd_low_port = 11280, # fastd port

  $mesh_if_id = "something",
  $mesh_mtu_low = 12345,
  $fastd_low_port = 112345, # fastd port
  ...
然后在这些块中循环,自动为每个块在
ffnord/etc/fastd/
中创建文件夹和文件

(我想解决这个问题:)

在Puppet 3.x中没有“循环”,但有一些技巧。您可以传递表示N个ffnord::fastd实例的散列数据:

define define ffnord::mesh($fastd_hash) {
  create_resources('ffnord::fastd', $fastd_hash)
}

define ffnord::fastd($mesh_code, $fastd_low_port, $mesh_mtu_low = 1280) {
  file {
    "/etc/fastd/${mesh_code}-mesh-low-vpn/":
      ensure =>directory,
             require => Package[ffnord::resources::fastd];
    "/etc/fastd/${mesh_code}-mesh-low-vpn/fastd.conf":
      ensure => file,
             notify => Service[ffnord::resources::fastd],
             content => template('ffnord/etc/fastd/fastd-low.conf.erb');
  } 
}

$hash_of_fastds = {
  "low_id" => {
    mesh_code      => 'low,
    mesh_mtu_low   => 1280,
    fastd_low_port => 11280,
  },
  "some_id" => {
    mesh_code      => 'something',
    mesh_mtu_low   => 12345,
    fastd_low_port => 112345,
  },
}

ffnord::mesh { 'foo': fastd_hash => $hash_of_fastds, }
注意我稍微修改了define ffnord::fastd,其中有一个$mesh\u if\u id参数,我将其转换为ffnord::fastd的$namevar

第一级$hash_of_fastds转换为ffnord::fastd实例的名称,第二级hash是每个ffnord::fastd的参数

有关更多信息,请参阅


在Puppet 4中,您可以使用each函数来实现类似的结果。

我肯定遗漏了一些东西:您不能只声明多个网格吗?fastd守护进程只能有多个实例。因此,您可以选择要连接到的。但这不是问题,好的想法。但向OP提出的问题是:您确定应该这样授权
定义ffnord::fastd
?在我看来,用户首先创建此定义的多个实例可能更有意义。我们希望定义一个包含端口、名称和MTU的接口哈希,所有接口都将创建为配置文件。此定义将被提取并放入每个社区的站点文件中。但是它们都将使用相同的puppet scriptsFelix是正确的,如果上述定义是您的全部需求,那么就不需要为了创建ffnord::fastd而将散列数据传递到ffnord::mesh。您的ffnord::fastd定义接受创建自身所需的所有参数,并且不需要来自封闭的ffnord::mesh定义类型的任何信息。这两种类型目前独立于傀儡的观点。@LukeBigum:不,上面的定义只是一个练习。
ffnord::fastd
的定义很长,我的原始示例有帮助吗?
  $mesh_if_id = "low",
  $mesh_mtu_low = 1280,
  $fastd_low_port = 11280, # fastd port

  $mesh_if_id = "something",
  $mesh_mtu_low = 12345,
  $fastd_low_port = 112345, # fastd port
  ...
define define ffnord::mesh($fastd_hash) {
  create_resources('ffnord::fastd', $fastd_hash)
}

define ffnord::fastd($mesh_code, $fastd_low_port, $mesh_mtu_low = 1280) {
  file {
    "/etc/fastd/${mesh_code}-mesh-low-vpn/":
      ensure =>directory,
             require => Package[ffnord::resources::fastd];
    "/etc/fastd/${mesh_code}-mesh-low-vpn/fastd.conf":
      ensure => file,
             notify => Service[ffnord::resources::fastd],
             content => template('ffnord/etc/fastd/fastd-low.conf.erb');
  } 
}

$hash_of_fastds = {
  "low_id" => {
    mesh_code      => 'low,
    mesh_mtu_low   => 1280,
    fastd_low_port => 11280,
  },
  "some_id" => {
    mesh_code      => 'something',
    mesh_mtu_low   => 12345,
    fastd_low_port => 112345,
  },
}

ffnord::mesh { 'foo': fastd_hash => $hash_of_fastds, }