零基礎學習Puppet自動化配置管理系列文檔
自定義fact可讓節點增長更多的標籤
在使用puppet做爲配置管理工具的同時,facter是一個很是有用的系統盤點工具,這個工具能夠經過一些預先設定好變量定位一臺主機,好比能夠經過變量lsbdistrelease即可以知道當前系統的版本號,經過osfamily即可以知道系統是RedHat仍是SLES,仍是其它等等。可是這些預先設定好的變量畢竟有限,在整個自動化運維過程當中,因爲系統應用的多樣性,更多須要經過應用的名稱、角色的名稱進行標示,這樣就須要自定義一些fact並賦值到每一個節點上去,至關於給節點打上標籤。html
1、自定義(custom)fact的四種方法
一、定義到facter軟件自己的lib庫中
這種方法是直接在安裝facter的lib庫裏面直接建立,至關於擴充facter軟件的lib庫。mysql
能夠經過如下方法找到facter包的lib庫路徑爲/usr/lib/ruby/site_ruby/1.8/facter
web
[root@agent1 facter]# rpm -ql facter
/usr/bin/facter
/usr/lib/ruby/site_ruby/1.8/facter
/usr/lib/ruby/site_ruby/1.8/facter.rb
/usr/lib/ruby/site_ruby/1.8/facter/Cfkey.rb
/usr/lib/ruby/site_ruby/1.8/facter/application.rb
/usr/lib/ruby/site_ruby/1.8/facter/architecture.rb
/usr/lib/ruby/site_ruby/1.8/facter/augeasversion.rb
/usr/lib/ruby/site_ruby/1.8/facter/blockdevices.rb
/usr/lib/ruby/site_ruby/1.8/facter/domain.rb
/usr/lib/ruby/site_ruby/1.8/facter/ec2.rb
/usr/lib/ruby/site_ruby/1.8/facter/facterversion.rb
/usr/lib/ruby/site_ruby/1.8/facter/filesystems.rb
/usr/lib/ruby/site_ruby/1.8/facter/fqdn.rb
/usr/lib/ruby/site_ruby/1.8/facter/hardwareisa.rb
/usr/lib/ruby/site_ruby/1.8/facter/hardwaremodel.rb
1.一、在facter的lib庫中建立fact,名稱爲rpms,能夠顯示當前安裝了多少rpm包sql
[root@agent1 ~]# cd /usr/lib/ruby/site_ruby/1.8/facter/
[root@agent1 facter]# vim rpms.rb
Facter.add(:rpms) do
setcode do
%x{/bin/rpm -qa | wc -l}.chomp #定義一個shell命令
end
end
1.二、經過facter命令進行測試shell
[root@agent1 facter]# facter | grep rpms
rpms => 918
[root@agent1 facter]# facter rpms
918
備註:這種方法至關於給facter軟件打補丁,過多的使用可能會破壞facter自己軟件的完整性,不建議使用。json
二、使用環境變量‘FACTERLIB’建立fact
這種方法也很是簡單,在一個目錄下定義一個fact,而後export便可,方法以下vim
2.一、在自定義目錄裏面定義一個fact,列出當前系統登陸的用戶數windows
[root@agent1 ~]# vim /var/lib/puppet/kiss_fact/users.rb
Facter.add(:users) do
setcode do
%x{/usr/bin/who |wc -l}.chomp
end
end
[root@agent1 kiss_fact]# facter users #無顯示結果,須要設置FACTERLIB
[root@agent1 kiss_fact]#
2.二、將自定義fact路徑賦值給變量FACTERLIBruby
[root@agent1 kiss_fact]# export FACTERLIB=/var/lib/puppet/kiss_fact
[root@agent1 kiss_fact]# facter users
2
[root@agent1 kiss_fact]# facter | grep users
users => 2
備註:這種方法是對第一種方法的擴展,能夠本身定義目錄,不過須要將路徑加到變量FACTERLIB中,能夠在/etc/profile添加,這樣系統啓動的時候即可以自動加載。bash
三、添加外部的(external)fact
這種方式支持txt、yaml、json、sh四種格式,內容也比較固定,默認狀況下須要在目錄/etc/facter/facts.d/下建立,使用也很是方便。關於這個路徑實際上是能夠修改的,不過修改起來並非很方便,須要修改facter軟件代碼。
[root@agent1 ~]# vim /usr/lib/ruby/site_ruby/1.8/facter/util/config.rb
32 def self.external_facts_dirs
33 if Facter::Util::Root.root?
34 windows_dir = windows_data_dir
35 if windows_dir.nil? then
36 ["/etc/facter/facts.d", "/etc/puppetlabs/facter/facts.d"] #external路徑位置
37 else
38 [File.join(windows_dir, 'PuppetLabs', 'facter', 'facts.d')]
39 end
40 else
41 [File.expand_path(File.join("~", ".facter", "facts.d"))]
42 end
43 end
特殊說明:只能用於1.7.3版本以上
3.一、經過txt文件建立
3.1.一、建立roles.txt文件
文件內容格式必須爲「key=value」
[root@agent1 ~]# mkdir /etc/facter/facts.d -p
[root@agent1 facts.d]# vim roles.txt
web=http
db=mysql
3.1.二、測試
[root@agent1 facts.d]# facter web1
http1
[root@agent1 facts.d]# facter db1
mysql1
[root@agent1 facts.d]# facter | egrep 'web1|db1'
db1 => mysql1
web1 => http1
3.二、經過yaml文件建立
3.2.一、建立yaml文件
[root@agent1 facts.d]# vim roles.yaml
---
web2:
- http2
db2:
- mysql2
3.2.二、測試
[root@agent1 facts.d]# facter | egrep 'web2|db2'
db2 => mysql2
web2 => http2
3.三、經過json文件建立
3.3.一、建立json文件
[root@agent1 facts.d]# vim roles.json
{
"web3": "http3",
"db3": "mysql3"
}
備註:提供一個在線編輯json的網站http://www.bejson.com/go.html?u=http://www.bejson.com/jsonview2/
3.3.二、安裝rubygem-json包(json文件須要它的支持)
[root@agent1 facts.d]# facter | egrep 'web3|db3'
Cannot parse JSON data file /etc/facter/facts.d/roles.json without the json library.
Suggested next step is `gem install json` to install the json library. #缺乏json包
[root@agent1 facts.d]# rpm -ivh rubygem-json-1.5.5-2.el5.x86_64.rpm #安裝rubygem-json包,找不到安裝包的同志可在羣共享裏面查找,羣號碼在文章最後面。
3.3.三、測試
[root@agent1 facts.d]# facter | egrep 'web3|db3'
db3 => mysql3
web3 => http3
3.四、經過sh腳本建立
3.4.一、建立shell腳本
[root@agent1 facts.d]# vim roles.sh
#!/bin/bash
echo "web4=http4"
echo "db4=mysql4"
3.4.二、設置文件具備可執行權限
[root@agent1 facts.d]# chmod a+x roles.sh
3.4.三、測試
[root@agent1 facts.d]# facter web4 db4
db4 => mysql4
web4 => http4
思考:那麼如何作到全部節點批量部署呢,能夠看到以上四種方式都是基於文件編輯的,可在puppetmaster端經過file資源實現部署。
四、使用pluginsync進行發佈
這種方法比較特殊,節點factpath目錄裏除了編寫好的rb文件以外,還須要在puppet模塊中引用,運行一次以後纔會轉換成fact。一般在puppetmaster端模塊裏的lib庫中添加,而後在puppet.conf中添加選項pluginsync=true便可,格式爲ruby文件。
4.一、建立模塊facts
[root@puppetmaster ~]# cd /etc/puppet/environments/kissprd/environment/modules/
[root@puppetmaster modules]# tree facts/ #目錄結構
facts/
└── lib
└── facter
└── hwclock.rb
2 directories, 1 file
備註:也能夠放在其餘已經編寫好的模塊中
[root@puppetmaster facter]# vim hwclock.rb #自定義fact:hwclock,顯示節點硬件時間
Facter.add(:hwclock) do
setcode do
%x{/usr/sbin/hwclock}.chomp
end
end
4.二、應用自定義fact至motd模塊中
[root@puppetmaster kissprd]# vim application/modules/motd/manifests/init.pp
class motd{
package{ 'setup':
ensure => present,
}
file{ '/etc/motd':
ensure => present,
owner => 'root',
group => 'root',
mode => '0644',
source => "puppet://$puppetserver/modules/motd/etc/motd",
require => Package['setup'],
}
notify { " Hardware-Clock: ${::hwclock}": } #添加一個通知,這裏只是測試,沒有實際意義
}
4.三、在puppetmaster端的puppet.conf中添加選項pluginsync
[root@puppetmaster kissprd]# vim /etc/puppet/puppet.conf
[main]
logdir = /var/log/puppet
rundir = /var/run/puppet
ssldir = $vardir/ssl
pluginsync = true #添加插件選項
...
4.四、在全部節點puppet.conf中添加pluginsync(經過在puppet模塊中添加實現)
[root@puppetmaster kissprd]# vim environment/modules/puppet/templates/puppet.conf.erb
### config by puppet ###
[main]
logdir = /var/log/puppet
rundir = /var/run/puppet
ssldir = $vardir/ssl
pluginsync = true #添加插件選項
[agent]
classfile = $vardir/classes.txt
localconfig = $vardir/localconfig
server = <%= scope.lookupvar('puppet::params::puppetserver') %>
certname = <%= scope.lookupvar('puppet::params::certname') %>
4.五、節點運行puppet agent進行測試
[root@agent1 ~]# facter -p hwclock #沒有這個fact,自定義fact須要加上-p參數才能顯示
[root@agent1 ~]# puppet agent -t --environment=kissprd #運行一次
info: Retrieving plugin
notice: /File[/var/lib/puppet/lib/facter/historys.rb]/ensure: removed
notice: /File[/var/lib/puppet/lib/facter/hwclock.rb]/ensure: defined content as '{md5}d8cc9fe2b349a06f087692763c878e28'
info: Loading downloaded plugin /var/lib/puppet/lib/facter/hwclock.rb #下載插件至節點factpath指定的目錄
info: Loading facts in /var/lib/puppet/lib/facter/hwclock.rb
info: Caching catalog for agent1_cert.kisspuppet.com
info: Applying configuration version '1396170375'
notice: Hardware-Clock: Sun 30 Mar 2014 05:06:16 PM CST -0.055086 seconds
notice: /Stage[main]/Motd/Notify[ Hardware-Clock: Sun 30 Mar 2014 05:06:16 PM CST -0.055086 seconds]/message: defined 'message' as ' Hardware-Clock: Sun 30 Mar 2014 05:06:16 PM CST -0.055086 seconds' #應用
notice: Finished catalog run in 0.51 seconds
[root@agent1 ~]# facter -p hwclock #自定義的hwclock生效
hwclock => Sun 30 Mar 2014 05:06:25 PM CST -0.567090 seconds
[root@agent1 ~]# ll /var/lib/puppet/lib/facter/ #插件已經下載到本地
total 4
-rw-r--r-- 1 root root 79 Mar 30 17:06 hwclock.rb
關於factpath默認路徑可經過如下命令查看,固然也能夠在puppet.conf中進行修改
[root@agent1 ~]# puppet --genconfig | grep factpath
factpath = /var/lib/puppet/lib/facter:/var/lib/puppet/facts
資料參考:
那麼在生產環境當中應當如何應用自定義的fact呢,下一章節會介紹一種方法,結合ENC(hiera)實現節點分類。
交流方式:
微信公衆號:puppet2014,可微信搜索加入,也能夠掃描如下二維碼進行加入
QQ交流羣:296934942