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

时间:2015-05-10 23:29:51

标签: puppet

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

define ffnord::mesh(
  $mesh_if_id = "low",
  $mesh_mtu_low = 1280,
  $fastd_low_port = 11280, # fastd port
) {
  ffnord::fastd { "fastd_${mesh_code}":
    mesh_if_id => $mesh_if_id,
    mesh_mtu_low  => $mesh_mtu_low,
    fastd_low_port   => $fastd_low_port,
  }
}

define ffnord::fastd( $mesh_if_id
                     , $mesh_code
                     , $mesh_mtu_low = 1280
                     , $fastd_low_port
                     ) {

  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');
  } 
}

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

  $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/内创建文件夹和文件?

(我想解决这个问题:https://github.com/ffnord/ffnord-puppet-gateway/pull/116#issuecomment-100619610

1 个答案:

答案 0 :(得分:2)

在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, }

注意我稍微修改了定义ffnord :: fastd ,你有一个$ mesh_if_id参数我把它变成了 ffnord的$ namevar :: fastd

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

有关详细信息,请参阅documentation on the create_resources function

在Puppet 4中,您可以使用每个函数来获得类似的结果。