4.1 使用pluginsync進行發佈nginx
這種方法比較特殊,節點factpath目錄裏除了編寫好的rb文件以外,還須要在puppet模塊中引用,運行一次以後纔會轉換成fact。一般在puppetmaster端模塊裏的lib庫中添加,而後在puppet.conf中添加選項pluginsync=true便可,格式爲ruby文件。web
4.2 建立模塊factsvim
[root@puppetmaster1 ~]# cd /etc/puppet/environments/jqprd/environment/modules/
[root@puppetmaster1 modules]# tree facts/ #目錄結構
facts/
└── lib
└── facter
└── hwclock.rb
2 directories, 1 file
備註:也能夠放在其餘已經編寫好的模塊中ruby
[root@puppetmaster1 facter]# vim hwclock.rb #自定義fact:hwclock,顯示節點硬件時間
Facter.add(:hwclock) do
setcode do
%x{/usr/sbin/hwclock}.chomp
end
end
4.3 應用自定義fact至motd模塊中app
[root@puppetmaster1 jqprd]# 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://$puppetmaster1/modules/motd/etc/motd",
require => Package['setup'],
}
notify { " Hardware-Clock: ${::hwclock}": } #添加一個通知,這裏只是測試,沒有實際意義
}
在puppetmaster端的puppet.conf中添加選項pluginsync測試
[root@puppetmaster1 jqprd]# vim /etc/puppet/puppet.conf
[main]
logdir = /var/log/puppet
rundir = /var/run/puppet
ssldir = $vardir/ssl
pluginsync = true
...
在全部節點puppet.conf中添加pluginsync(經過在puppet模塊中添加實現)ui
[root@puppetmaster jqprd]# 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::puppetmaster1') %>
certname = <%= scope.lookupvar('puppet::params::certname') %>
節點運行puppet agent進行測試插件
[root@ag1 ~]# facter -p hwclock #沒有這個fact,自定義fact須要加上-p參數才能顯示
[root@ag1 ~]# puppet agent -t --environment=jqprd #運行一次
[root@ag1 yum.repos.d]# puppet agent -t --environment=jqprd
Info: Retrieving pluginfacts
Info: Retrieving plugin
Notice: /File[/var/lib/puppet/lib/facter/hwclock.rb]/ensure: defined content as '{md5}d8cc9fe2b349a06f087692763c878e28'
Info: Loading facts
Info: Loading facts
Info: Caching catalog for ag1_cert.jq.com
Info: Applying configuration version '1419414521'
[root@ag1 ~]# facter -p hwclock #自定義的hwclock生效
hwclock => Sun 30 Mar 2014 04.一、建立模塊facts
[root@puppetmaster ~]# cd /etc/puppet/environments/jqprd/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
應用自定義fact至motd模塊中
[root@puppetmaster jqprd]# 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://$puppetmaster1/modules/motd/etc/motd",
require => Package['setup'],
}
notify { " Hardware-Clock: ${::hwclock}": } #添加一個通知,這裏只是測試,沒有實際意義
}
在puppetmaster端的puppet.conf中添加選項pluginsync
[root@puppetmaster jqprd]# vim /etc/puppet/puppet.conf
[main]
logdir = /var/log/puppet
rundir = /var/run/puppet
ssldir = $vardir/ssl
pluginsync = true #添加插件選項
...
在全部節點puppet.conf中添加pluginsync(經過在puppet模塊中添加實現)
[root@puppetmaster jqprd]# 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::puppetmaster1') %>
certname = <%= scope.lookupvar('puppet::params::certname') %>
節點運行puppet agent進行測試
[root@ag1 ~]# facter -p hwclock #沒有這個fact,自定義fact須要加上-p參數才能顯示
[root@ag1 ~]# puppet agent -t --environment=jqprd #運行一次
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 ag1_cert.jqpuppet.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@ag1 ~]# facter -p hwclock #自定義的hwclock生效
hwclock => Sun 30 Mar 2014 05:06:25 PM CST -0.567090 seconds
[root@ag1 ~]# 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@ag1 ~]# puppet --genconfig | grep factpath
factpath = /var/lib/puppet/lib/facter:/var/lib/puppet/facts5:06:25 PM CST -0.567090 seconds
[root@ag1 ~]# 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@ag1 ~]# puppet --genconfig | grep factpath
factpath = /var/lib/puppet/lib/facter:/var/lib/puppet/facts
在3.7版本中,hirea不須要單獨安裝,在安裝puppet的時候就已經安裝。rest
默認hiera.yaml主配置文件在/etc目錄下,爲告終合後期版本控制系統集中管理,建議將此文件copy到/etc/puppet目錄下,而後建立軟鏈接指向/etc/hiera.yaml便可。
[root@puppetmaster ~]# mv /etc/hiera.yaml /etc/puppet/
[root@puppetmaster ~]# ln -s /etc/puppet/hiera.yaml /etc/hiera.yaml
[root@puppetmaster ~]# ll /etc/hiera.yaml
lrwxrwxrwx 1 root root 22 Apr 20 20:05 /etc/hiera.yaml -> /etc/puppet/hiera.yaml
5.1 編輯hiera.yaml
備註: 以上變量其實就是fact變量。
[root@puppetmaster ~]# vim /etc/puppet/hiera.yaml
---
:backends:
- yaml
:hierarchy:
# - defaults
# - "%{clientcert}"
- common
- "%{environment}"
- "%{osfamily}"
- "%{hostname}"
# - global
:yaml:
:datadir:"/etc/puppet/environments/%{environment}/hiera"
hiera主配置文件編寫完成以後,須要重啓puppetmaster後方可生效。
[root@puppetmaster hiera]# /etc/init.d/puppetmaster restart
Stopping puppetmaster: [ OK ]
Starting puppetmaster: [ OK ]
5.2 Facter自定義變量
建立變量common對應的文件
[root@puppetmaster1 jqprd]# pwd
/etc/puppet/environments/jqprd
[root@puppetmaster1 jqprd]# mkdir hiera
[root@puppetmaster1 hiera]# vim common.yaml
---
puppetmaster1:
- 'puppetmaster1.jq.com'
建立變量osfamily對應的文件
[root@ag1 ~]# facter osfamily
RedHat
[root@puppetmaster hiera]# vim RedHat.yaml
---
classes:
- 'puppet'
- 'yum'
經過hiera命令測試
[root@puppetmaster hiera]# hiera classes environment=jqprd
nil
[root@puppetmaster hiera]# hiera classes environment=jqprd osfamily=RedHat
["motd", "puppet", "yum"]
[root@puppetmaster hiera]# hiera classes environment=jqprd osfamily=SLES
nil
經過以上命令能夠得在環境爲jqprd,系統爲RedHat的狀況下,classes的變量爲三個值(puppet、yum)。
建立變量hostname對應的全部節點文件
[root@ag1 ~]# facter hostname
ag1
[root@puppetmaster hiera]# vim ag1.yaml
---
classes:
- 'motd'
certname:
- 'ag1_cert.jq.com'
[root@puppetmaster hiera]# vim ag1.yaml
---
classes:
- 'motd'
certname:
- 'ag1_cert.jq.com'
[root@puppetmaster hiera]# vim agent3.yaml
---
certname:
- 'agent3_cert.jq.com'
經過hiera命令測試
[root@puppetmaster hiera]# hiera classes environment=jqprd hostname=agent
1
["motd"]
[root@puppetmaster hiera]# hiera classes environment=jqprd hostname=agent
2
["motd"]
[root@puppetmaster hiera]# hiera classes environment=jqprd hostname=agent
3
nil
[root@puppetmaster hiera]# hiera certname environment=jqprd hostname=ag1
["ag1_cert.jq.com"]
[root@puppetmaster hiera]# hiera certname environment=jqprd hostname=ag1
["ag1_cert.jq.com"]
[root@puppetmaster hiera]# hiera certname environment=jqprd hostname=agent3
["agent3_cert.jq.com"]
經過以上命令測試能夠得知,系統fact變量hostname爲ag1和ag1的狀況下,hiera變量classes爲motd。certname變量爲各自的certname變量。
5.3 應用hiera變量於Puppetmaster
在現有facts模塊中直接添加
以前facts模塊中的結構
[root@puppetmaster1 facts]# pwd
/etc/puppet/environments/jqprd/environment/modules/facts
[root@puppetmaster1 facts]# mkdir -p {files,manifests,templates}
[root@puppetmaster modules]# tree facts/
facts/
├── files
├── lib
│ └── facter
│ └── hwclock.rb #經過pluginsync模式發佈的自定義fact變量,無需修改
├── manifests
└── templates
5 directories, 1 file
添加管理file資源的pp文件
[root@puppetmaster manifests]# vim config.pp #定義file資源
class facts::config{
file{ "/etc/facter/facts.d/$hostname.txt": #文件名稱經過變量hostname獲取
owner => "root",
group => "root",
mode => 0400,
source => "puppet:///modules/facts/facts.d/$hostname.txt", #文件名稱經過節點變量hostname獲取
require => Class['facts::exec'],
}
}
[root@puppetmaster manifests]# vim exec.pp #定義可執行資源保證目錄 /etc/facter/facts.d 存在
class facts::exec{
exec {"create fact external":
command => "mkdir -p /etc/facter/facts.d ",
path => ["/usr/bin","/usr/sbin","/bin","/sbin"],
creates => "/etc/facter/facts.d",
}
}
[root@puppetmaster manifests]# vim init.pp
class facts{
include facts::config,facts::exec
}
[root@puppetmaster manifests]# vim init.pp
class facts{
include facts::config,facts::exec
}
建立file資源對應的下載文件
[root@puppetmaster facts.d]# pwd
/etc/puppet/environments/jqprd/environment/modules/facts/files/facts.d
[root@puppetmaster facts.d]# vim ag1.txt
env=prd
app=weblogic
[root@puppetmaster facts.d]# vim ag1.txt
env=qa
app=db2
[root@puppetmaster facts.d]# vim agent3.txt
env=prd
app=nginx
5.4 應用模塊facts至hiera中
因爲模塊facts屬於全局的,應用於common.ymal或者RedHat.ymal中便可。
[root@puppetmaster hiera]# vim RedHat.yaml
---
classes:
- 'puppet'
- 'yum'
- 'facts'
節點測試
[root@ag1 ~]# ll /etc/facter/facts.d
ls: cannot access /etc/facter/facts.d: No such file or directory
[root@agent3 ~]# puppet agent -t --environment=jqprd
[root@agent3 ~]# cat /etc/facter/facts.d/ag1.txt
env=prd
app=weblogic
[root@ag1 ~]# facter env
prd
[root@ag1 ~]# facter app
weblogic本系統puppet均根據kisspuppet的博客(http://kisspuppet.com/)進行實驗,很是感謝!!!