四、Fact插件
4.1 使用pluginsync进行发布
这种方法比较特殊,节点factpath目录里除了编写好的rb文件之外,还需要在puppet模块中引用,运行一次之后才会转换成fact。通常在puppetmaster端模块里的lib库中添加,然后在puppet.conf中添加选项pluginsync=true即可,格式为ruby文件。
4.2 创建模块facts
[root@puppetmaster1 ~]# cd /etc/puppet/environments/jqprd/environment/modules/
[root@puppetmaster1 modules]# tree facts/ #目录结构
facts/
└── lib
└── facter
└── hwclock.rb
2 directories, 1 file
备注:也可以放在其他已经编写好的模块中
[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模块中
[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模块中添加实现)
[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.1、创建模块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
五、自定义fact结合hirea
在3.7版本中,hirea不需要单独安装,在安装puppet的时候就已经安装。
默认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
- 添加全局变量common,注释掉defaults、global和clientcert。
- 添加系统类型变量osfamily
- 添加主机名变量hostname
- 添加datadir路径位置,中间用了puppet环境变量,这里的环境变量和puppet应用的环境变量是一致的。如果你只有一种环境,只需要将其中变量去掉即可。
备注: 以上变量其实就是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/)进行实验,非常感谢!!!