今回はFacterを拡張して、
Facterとは?
Facterとは、
たとえば、
$ntp_package = $operatingsystem ? {
freebsd => 'openntpd',
default => 'ntp',
}
package { $ntp_package: ensure => present }
operatingsystemやipaddressなどFacterによって取得できる項目を"fact"と呼びます。
カスタムfactの作成
Facterを拡張するための準備として、
$ mkdir -p ~/lib/ruby/facter
$ export RUBYLIB=~/lib/ruby
カスタムfact用ファイルの内容は以下のようになり、
Facter.add("custom_fact") do
setcode do
# カスタムfactの値を返す
end
end
たとえば、
Facter.add("home") do
setcode do
ENV['HOME']
end
end
この状態でfacterコマンドを実行すると、
$ facter home
/home/mizzy
特定の条件のみで値を返したい場合には、
Facter.add("custom_fact") do
confine :kernel => :linux
setcode do
# カスタムfactの値を返す
end
end
Puppetでのカスタムfactの配布
FacterはPuppetクライアント上で実行されるため、
Puppetサーバ側では以下の様な設定を/etc/
[facts]
allow *
path /var/puppet/facts
Puppetクライアント側では、
[puppetd]
factsync = true
上記の様な設定を行い、
$ sudo puppetd --server puppet.example.org --verbose
notice: Starting Puppet client version 0.22.4
info: Retrieving facts
notice: /fact_collector/File[/var/puppet/facts]/ensure: created
notice: /fact_collector/File[/var/puppet/facts/home.rb]/ensure: created
info: Loading fact home
info: Facts have changed; recompiling
...
ここで説明したカスタムfactの配布方法は、
カスタムfactサンプル
eth1のIPアドレスを取得するためのカスタムfact
eth0にグローバルなIPアドレスを、
そこで、
Facter.add(:privateipaddress) do
confine :kernel => :linux
setcode do
ip = nil
output = %x{/sbin/ifconfig}
output.split(/\n\n/).each { |str|
if str !~ /eth1/
next
end
if str =~ /inet addr:([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/
tmp = $1
unless tmp =~ /127\./
ip = tmp
break
end
end
}
ip
end
end
これを利用して、
PORT="11211"
USER="nobody"
MAXCONN="1024"
CACHESIZE="64"
OPTIONS="-l <%= privateipaddress %> "
XenのDom0かDomUかを区別するためのカスタムfact
もうひとつのサンプルは、
Facter.add("virtual") do
confine :kernel => :linux
ENV["PATH"]="/bin:/sbin:/usr/bin:/usr/sbin"
result = "physical"
setcode do
if FileTest.exists?("/proc/xen/capabilities")
txt = File.read("/proc/xen/capabilities")
if txt =~ /control_d/i
result = "xen0"
else
result = "xenu"
end
end
result
end
end
これを利用して、
case $virtual {
'xenu': {
package { 'autofs':
ensure => installed;
}
}
}
PuppetのオフィシャルWikiにも、
次回は独自のリソースタイプを作成して、