Puppet言語にforとかwhileなどのループはありません(最新版ではeachがあるみたいなので、ループにつかるかもしれません)。ループはないですが、リソース定義などで、タイトルに配列(それともリスト?)を設定するとまとめて設定できます。
file{ [ '/home/hoge1', '/home/hoge2', '/home/hoge3' ] :
... } |
さて、以下の3つのNFSマウントの設定を、Hieraでまとめたい場合、どいういう方法があるでしょうか。似たパターンなのでまとめたくなります。
class nfs {
mount { "/mnt/host1" : device => "192.0.2.1:/", fstype => "nfs", options => "defaults", ensure => "mounted", } mount { "/mnt/host2" : device => "192.0.2.2:/", fstype => "nfs", options => "defaults", ensure => "mounted", } mount { "/mnt/host3" : device => "192.0.2.3:/", fstype => "nfs", options => "defaults", ensure => "mounted", } } |
1つの解決策は以下のとおりです。まずは、Hieraの設定です。
classes:
- nfs nfs::mount_list: - '192.0.2.1 host1' - '192.0.2.2 host2' - '192.0.2.3 host3' |
モジュールの設定です。
class nfs(
$mount_list, ) { nfs::def1 { $mount_list : } } define nfs::def1 { $mp = split($name,' ') nfs::def2 { "${mp[1]}" : nfsdevice => "${mp[0]}:/", mountpoint => "/mnt/${mp[1]}", } } define nfs::def2 ( $nfsdevice, $mountpoint, ){ mount { "${mountpoint}" : device => "${nfsdevice}", fstype => "nfs", options => "defaults", ensure => "mounted", } } |
splitを使うあたり、あまりきれいな解ではないですが。