puppet之自定义fact(转载)

1、使用环境变量‘FACTERLIB’创建fact

1.1、在自定义目录里面定义一个fact,列出当前系统登录的用户数

[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]#
1.2、将自定义fact路径赋值给变量FACTERLIB
[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添加,这样系统启动的时候便可以自动加载。

2、使用pluginsync进行发布

这种方法比较特殊,节点factpath目录里除了编写好的rb文件之外,还需要在puppet模块中引用,运行一次之后才会转换成fact。通常在puppetmaster端模块里的lib库中添加,然后在puppet.conf中添加选项pluginsync=true即可,格式为ruby文件。

2.1、创建模块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

2.2、应用自定义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}": } #添加一个通知,这里只是测试,没有实际意义
}

2.3、在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    #添加插件选项
...

2.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') %>

2.5、节点运行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

转载自:https://kisspuppet.gitbooks.io/puppet/content/puppet_learning_base10.html

posted @ 2016-02-02 14:25  Dus  阅读(696)  评论(0编辑  收藏  举报