设为首页 收藏本站
查看: 1230|回复: 0

[经验分享] puppet插件fact和hiera(puppet自动化系列3)

[复制链接]

尚未签到

发表于 2015-9-16 10:14:26 | 显示全部楼层 |阅读模式
四、Fact插件

4.1 使用pluginsync进行发布

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


[iyunv@puppetmaster1 ~]# cd /etc/puppet/environments/jqprd/environment/modules/
[iyunv@puppetmaster1 modules]# tree facts/  #目录结构
facts/
└── lib
    └── facter
        └── hwclock.rb

2 directories, 1 file
  备注:也可以放在其他已经编写好的模块中

[iyunv@puppetmaster1 facter]# vim hwclock.rb  #自定义fact:hwclock,显示节点硬件时间
Facter.add(:hwclock) do
  setcode do
    %x{/usr/sbin/hwclock}.chomp
  end
end
4.3 应用自定义fact至motd模块中


[iyunv@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

[iyunv@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模块中添加实现)

[iyunv@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进行测试

[iyunv@ag1 ~]# facter -p hwclock  #没有这个fact,自定义fact需要加上-p参数才能显示
[iyunv@ag1 ~]# puppet agent -t --environment=jqprd  #运行一次
[iyunv@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'
[iyunv@ag1 ~]# facter -p  hwclock #自定义的hwclock生效
hwclock => Sun 30 Mar 2014 04.1、创建模块facts

[iyunv@puppetmaster ~]# cd /etc/puppet/environments/jqprd/environment/modules/
[iyunv@puppetmaster modules]# tree facts/  #目录结构
facts/
└── lib
    └── facter
        └── hwclock.rb

2 directories, 1 file
备注:也可以放在其他已经编写好的模块中

[iyunv@puppetmaster facter]# vim hwclock.rb  #自定义fact:hwclock,显示节点硬件时间
Facter.add(:hwclock) do
  setcode do
    %x{/usr/sbin/hwclock}.chomp
  end
end
应用自定义fact至motd模块中
[iyunv@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

[iyunv@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模块中添加实现)

[iyunv@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进行测试

[iyunv@ag1 ~]# facter -p hwclock  #没有这个fact,自定义fact需要加上-p参数才能显示
[iyunv@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
[iyunv@ag1 ~]# facter -p  hwclock #自定义的hwclock生效
hwclock => Sun 30 Mar 2014 05:06:25 PM CST  -0.567090 seconds

[iyunv@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中进行修改

[iyunv@ag1 ~]# puppet --genconfig | grep factpath
    factpath = /var/lib/puppet/lib/facter:/var/lib/puppet/facts5:06:25 PM CST  -0.567090 seconds

[iyunv@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中进行修改
[iyunv@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即可。

[iyunv@puppetmaster ~]# mv /etc/hiera.yaml /etc/puppet/
[iyunv@puppetmaster ~]# ln -s /etc/puppet/hiera.yaml /etc/hiera.yaml
[iyunv@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变量。

[iyunv@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后方可生效。

[iyunv@puppetmaster hiera]# /etc/init.d/puppetmaster restart
Stopping puppetmaster:                                     [  OK  ]
Starting puppetmaster:                                     [  OK  ]
5.2 Facter自定义变量

  创建变量common对应的文件
  [iyunv@puppetmaster1 jqprd]# pwd
  /etc/puppet/environments/jqprd
  [iyunv@puppetmaster1 jqprd]# mkdir hiera
  [iyunv@puppetmaster1 hiera]# vim common.yaml
  ---
  puppetmaster1:
  - 'puppetmaster1.jq.com'
  
  创建变量osfamily对应的文件

[iyunv@ag1 ~]# facter osfamily
RedHat

[iyunv@puppetmaster hiera]# vim RedHat.yaml
---
classes:
  - 'puppet'
  - 'yum'
  通过hiera命令测试

[iyunv@puppetmaster hiera]# hiera classes environment=jqprd
nil
[iyunv@puppetmaster hiera]# hiera classes environment=jqprd osfamily=RedHat
["motd", "puppet", "yum"]
[iyunv@puppetmaster hiera]# hiera classes environment=jqprd osfamily=SLES
nil
  通过以上命令可以得在环境为jqprd,系统为RedHat的情况下,classes的变量为三个值(puppet、yum)。
  
  创建变量hostname对应的所有节点文件

[iyunv@ag1 ~]# facter hostname
ag1

[iyunv@puppetmaster hiera]# vim ag1.yaml
---
classes:
  - 'motd'
certname:
  - 'ag1_cert.jq.com'

[iyunv@puppetmaster hiera]# vim ag1.yaml
---
classes:
  - 'motd'
certname:
  - 'ag1_cert.jq.com'

[iyunv@puppetmaster hiera]# vim agent3.yaml
---
certname:
  - 'agent3_cert.jq.com'
  通过hiera命令测试

[iyunv@puppetmaster hiera]# hiera classes environment=jqprd hostname=agent
1
["motd"]
[iyunv@puppetmaster hiera]# hiera classes environment=jqprd hostname=agent
2
["motd"]
[iyunv@puppetmaster hiera]# hiera classes environment=jqprd hostname=agent
3
nil
[iyunv@puppetmaster hiera]# hiera certname environment=jqprd hostname=ag1
["ag1_cert.jq.com"]
[iyunv@puppetmaster hiera]# hiera certname environment=jqprd hostname=ag1
["ag1_cert.jq.com"]
[iyunv@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模块中的结构
  [iyunv@puppetmaster1 facts]# pwd
  /etc/puppet/environments/jqprd/environment/modules/facts
  [iyunv@puppetmaster1 facts]# mkdir -p {files,manifests,templates}

[iyunv@puppetmaster modules]# tree facts/
facts/
├── files
├── lib
│   └── facter
│       └── hwclock.rb   #通过pluginsync模式发布的自定义fact变量,无需修改
├── manifests
└── templates

5 directories, 1 file
  添加管理file资源的pp文件

[iyunv@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'],
  }
}
[iyunv@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",
  }
}
[iyunv@puppetmaster manifests]# vim init.pp
class facts{
    include facts::config,facts::exec
}
[iyunv@puppetmaster manifests]# vim init.pp
class facts{
    include facts::config,facts::exec
}
  创建file资源对应的下载文件

[iyunv@puppetmaster facts.d]# pwd
/etc/puppet/environments/jqprd/environment/modules/facts/files/facts.d
[iyunv@puppetmaster facts.d]# vim ag1.txt
env=prd
app=weblogic
[iyunv@puppetmaster facts.d]# vim ag1.txt
env=qa
app=db2
[iyunv@puppetmaster facts.d]# vim agent3.txt
env=prd
app=nginx
  
5.4 应用模块facts至hiera中

  由于模块facts属于全局的,应用于common.ymal或者RedHat.ymal中即可。
[iyunv@puppetmaster hiera]# vim RedHat.yaml
---
classes:
  - 'puppet'
  - 'yum'
  - 'facts'
  节点测试

[iyunv@ag1 ~]# ll /etc/facter/facts.d
ls: cannot access /etc/facter/facts.d: No such file or directory

[iyunv@agent3 ~]# puppet agent -t --environment=jqprd
[iyunv@agent3 ~]# cat /etc/facter/facts.d/ag1.txt
env=prd
app=weblogic

[iyunv@ag1 ~]# facter env
prd
[iyunv@ag1 ~]# facter app
weblogic

本系统puppet均根据kisspuppet的博客(http://kisspuppet.com/)进行实验,非常感谢!!!

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-114339-1-1.html 上篇帖子: 通过MCollective实现puppet向windows的推送 下篇帖子: Puppet权威指南
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表