puppet插件fact和hiera(puppet自动化系列3)
四、Fact插件4.1 使用pluginsync进行发布
这种方法比较特殊,节点factpath目录里除了编写好的rb文件之外,还需要在puppet模块中引用,运行一次之后才会转换成fact。通常在puppetmaster端模块里的lib库中添加,然后在puppet.conf中添加选项pluginsync=true即可,格式为ruby文件。
4.2 创建模块facts
# cd /etc/puppet/environments/jqprd/environment/modules/
# tree facts/#目录结构
facts/
└── lib
└── facter
└── hwclock.rb
2 directories, 1 file
备注:也可以放在其他已经编写好的模块中
# vim hwclock.rb#自定义fact:hwclock,显示节点硬件时间
Facter.add(:hwclock) do
setcode do
%x{/usr/sbin/hwclock}.chomp
end
end
4.3 应用自定义fact至motd模块中
# 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
# vim /etc/puppet/puppet.conf
logdir = /var/log/puppet
rundir = /var/run/puppet
ssldir = $vardir/ssl
pluginsync = true
...
在所有节点puppet.conf中添加pluginsync(通过在puppet模块中添加实现)
# vim environment/modules/puppet/templates/puppet.conf.erb
### config bypuppet ###
logdir = /var/log/puppet
rundir = /var/run/puppet
ssldir = $vardir/ssl
pluginsync = true
classfile = $vardir/classes.txt
localconfig = $vardir/localconfig
server = <%= scope.lookupvar('puppet::params::puppetmaster1') %>
certname = <%= scope.lookupvar('puppet::params::certname') %>
节点运行puppet agent进行测试
# facter -p hwclock#没有这个fact,自定义fact需要加上-p参数才能显示
# puppet agent -t --environment=jqprd#运行一次
# puppet agent -t --environment=jqprd
Info: Retrieving pluginfacts
Info: Retrieving plugin
Notice: /File/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'
# facter -phwclock #自定义的hwclock生效
hwclock => Sun 30 Mar 2014 04.1、创建模块facts
# cd /etc/puppet/environments/jqprd/environment/modules/
# tree facts/#目录结构
facts/
└── lib
└── facter
└── hwclock.rb
2 directories, 1 file
备注:也可以放在其他已经编写好的模块中
# vim hwclock.rb#自定义fact:hwclock,显示节点硬件时间
Facter.add(:hwclock) do
setcode do
%x{/usr/sbin/hwclock}.chomp
end
end
应用自定义fact至motd模块中
# 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
# vim /etc/puppet/puppet.conf
logdir = /var/log/puppet
rundir = /var/run/puppet
ssldir = $vardir/ssl
pluginsync = true #添加插件选项
...
在所有节点puppet.conf中添加pluginsync(通过在puppet模块中添加实现)
# vim environment/modules/puppet/templates/puppet.conf.erb
### config bypuppet ###
logdir = /var/log/puppet
rundir = /var/run/puppet
ssldir = $vardir/ssl
pluginsync = true#添加插件选项
classfile = $vardir/classes.txt
localconfig = $vardir/localconfig
server = <%= scope.lookupvar('puppet::params::puppetmaster1') %>
certname = <%= scope.lookupvar('puppet::params::certname') %>
节点运行puppet agent进行测试
# facter -p hwclock#没有这个fact,自定义fact需要加上-p参数才能显示
# puppet agent -t --environment=jqprd#运行一次
info: Retrieving plugin
notice: /File/ensure: removed
notice: /File/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/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
# facter -phwclock #自定义的hwclock生效
hwclock => Sun 30 Mar 2014 05:06:25 PM CST-0.567090 seconds
# ll /var/lib/puppet/lib/facter/#插件已经下载到本地
total 4
-rw-r--r-- 1 root root 79 Mar 30 17:06 hwclock.rb
关于factpath默认路径可通过以下命令查看,当然也可以在puppet.conf中进行修改
# puppet --genconfig | grep factpath
factpath = /var/lib/puppet/lib/facter:/var/lib/puppet/facts5:06:25 PM CST-0.567090 seconds
# ll /var/lib/puppet/lib/facter/#插件已经下载到本地
total 4
-rw-r--r-- 1 root root 79 Mar 30 17:06 hwclock.rb
关于factpath默认路径可通过以下命令查看,当然也可以在puppet.conf中进行修改
# 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即可。
# mv /etc/hiera.yaml /etc/puppet/
# ln -s /etc/puppet/hiera.yaml /etc/hiera.yaml
# 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变量。
# vim /etc/puppet/hiera.yaml
---
:backends:
- yaml
:hierarchy:
#- defaults
#- "%{clientcert}"
- common
- "%{environment}"
- "%{osfamily}"
- "%{hostname}"
#- global
:yaml:
:datadir:"/etc/puppet/environments/%{environment}/hiera"
hiera主配置文件编写完成之后,需要重启puppetmaster后方可生效。
# /etc/init.d/puppetmaster restart
Stopping puppetmaster:
Starting puppetmaster:
5.2 Facter自定义变量
创建变量common对应的文件
# pwd
/etc/puppet/environments/jqprd
# mkdir hiera
# vim common.yaml
---
puppetmaster1:
- 'puppetmaster1.jq.com'
创建变量osfamily对应的文件
# facter osfamily
RedHat
# vim RedHat.yaml
---
classes:
- 'puppet'
- 'yum'
通过hiera命令测试
# hiera classes environment=jqprd
nil
# hiera classes environment=jqprd osfamily=RedHat
["motd", "puppet", "yum"]
# hiera classes environment=jqprd osfamily=SLES
nil
通过以上命令可以得在环境为jqprd,系统为RedHat的情况下,classes的变量为三个值(puppet、yum)。
创建变量hostname对应的所有节点文件
# facter hostname
ag1
# vim ag1.yaml
---
classes:
- 'motd'
certname:
- 'ag1_cert.jq.com'
# vim ag1.yaml
---
classes:
- 'motd'
certname:
- 'ag1_cert.jq.com'
# vim agent3.yaml
---
certname:
- 'agent3_cert.jq.com'
通过hiera命令测试
# hiera classes environment=jqprd hostname=agent
1
["motd"]
# hiera classes environment=jqprd hostname=agent
2
["motd"]
# hiera classes environment=jqprd hostname=agent
3
nil
# hiera certname environment=jqprd hostname=ag1
["ag1_cert.jq.com"]
# hiera certname environment=jqprd hostname=ag1
["ag1_cert.jq.com"]
# 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模块中的结构
# pwd
/etc/puppet/environments/jqprd/environment/modules/facts
# mkdir -p {files,manifests,templates}
# tree facts/
facts/
├── files
├── lib
│ └── facter
│ └── hwclock.rb #通过pluginsync模式发布的自定义fact变量,无需修改
├── manifests
└── templates
5 directories, 1 file
添加管理file资源的pp文件
# 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'],
}
}
# 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",
}
}
# vim init.pp
class facts{
include facts::config,facts::exec
}
# vim init.pp
class facts{
include facts::config,facts::exec
}
创建file资源对应的下载文件
# pwd
/etc/puppet/environments/jqprd/environment/modules/facts/files/facts.d
# vim ag1.txt
env=prd
app=weblogic
# vim ag1.txt
env=qa
app=db2
# vim agent3.txt
env=prd
app=nginx
5.4 应用模块facts至hiera中
由于模块facts属于全局的,应用于common.ymal或者RedHat.ymal中即可。
# vim RedHat.yaml
---
classes:
- 'puppet'
- 'yum'
- 'facts'
节点测试
# ll /etc/facter/facts.d
ls: cannot access /etc/facter/facts.d: No such file or directory
# puppet agent -t --environment=jqprd
# cat /etc/facter/facts.d/ag1.txt
env=prd
app=weblogic
# facter env
prd
# facter app
weblogic
本系统puppet均根据kisspuppet的博客(http://kisspuppet.com/)进行实验,非常感谢!!!
页:
[1]