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

[经验分享] 通过自定义fact增强MCollective推送更新元数据的灵活性

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2014-4-21 09:25:54 | 显示全部楼层 |阅读模式
目前由于Facter并不全面,许多关于主机和环境的信息并没有作为Facter的fact。编写自定义的fact,可以让节点的facter包含更多的元数据fact,增加MCollective选择元数据定位主机的灵活性。
1 自定义节点变量
首选,需要在每个节点自定义一个facts文档,文档中包含了每个节点自定义的fact信息。为了方便管理,所有变量的值都必须事先定义好,可在puppet服务端定义一个fact变量列表,里面包含所有节点的自定义fact信息。然后,节点根据各自的主机特性选择合适的fact信息。
[iyunv@puppetserver ~]# vim /etc/mcollective/facts.txt #收集并定义所有节点的fact信息,仅仅作为查看用
fact_certname=<自定义>
fact_apply1=apache
fact_apply2=php
fact_apply3=mysql
fact_apply4=java
fact_apply5=tomcat
fact_apply6=oracle
fact_apply7=nginx
fact_apply8=jboss
fact_apply9=haproxy
fact_apply10=db2

[iyunv@agent1 ~]# cat /etc/mcollective/facts.txt #假设agent1节点具有以下fact变量信息
fact_certname=agent1.kisspuppet.com #puppet认证用,可写成其他名称
fact_apply3=mysql
fact_apply4=java
fact_apply10=db2
[iyunv@agent2 ~]# cat /etc/mcollective/facts.txt #假设agent1节点具有以下fact变量信息
fact_certname=agent2.kisspuppet.com #puppet认证用,可写成其他名称
fact_apply2=php
fact_apply3=mysql
fact_apply7=nginx
2 创建file资源模块
由于自定义fact信息属于每个节点的特性,放在agents(存放单个节点个性模块的目录)目录中,可将这部分定义成一个class包含到每个节点的class agentN{}中。
[iyunv@puppetserver ~]# cat /etc/puppet/agents/modules/agent1/manifests/init.pp
class agent1{
  include agent1::facts
}
class agent1::facts{
  file{ "/etc/mcollective/facts.txt":
    owner   => "root",
    group   => "root",
    mode    => 0400,
    content => template("agent1/facts.txt.erb"),
    backup  => 'main',
  }
}
...   
[iyunv@puppetserver agents]# cat modules/agent1/templates/facts.txt.erb
------------Some custom facts variables-------------
fact_certname=agent1.kisspuppet.com
fact_apply3=mysql
fact_apply4=java
fact_apply10=db2
...
----------------------------------------------------
[iyunv@puppetserver agents]# cat modules/agent1/manifests/init.pp
class agent1{
  include agent1::facts
}
class agent1::facts{
  file{ "/etc/mcollective/facts.txt":
    owner   => "root",
    group   => "root",
    mode    => 0400,
    content => template("agent1/facts.txt.erb"),
    backup  => 'main',
  }
}
...
[iyunv@puppetserver agents]# cat modules/agent2/templates/facts.txt.erb
------------Some custom facts variables-------------
fact_certname=agent2.kisspuppet.com
fact_apply2=php
fact_apply3=mysql
fact_apply7=nginx
...
----------------------------------------------------
3 创建fact模块
3.1 创建全局模块
新建一个模块可命名为public,放在environment(存放基础环境的模块)模块中,自定义fact fact_apply.rb(过滤各自的自定义fact信息)
[iyunv@puppetserver puppet]# cat environment/modules/public/lib/facter/fact_apply.rb
# certname is usered for /etc/puppet/puppet.conf
Facter.add("fact_certname") do
   setcode do
       Facter::Util::Resolution.exec("/bin/grep 'fact_certname=' /etc/mcollective/facts.txt |awk -F= '{print $2}'")
     end
end
# fact_apply1~N.rb
#
Facter.add("fact_apply1") do
   setcode do
       Facter::Util::Resolution.exec("/bin/grep 'fact_apply1=' /etc/mcollective/facts.txt |awk -F= '{print $2}'")
     end
end
Facter.add("fact_apply2") do
   setcode do
       Facter::Util::Resolution.exec("/bin/grep 'fact_apply2=' /etc/mcollective/facts.txt |awk -F= '{print $2}'")
     end
end

Facter.add("fact_apply10") do
   setcode do
       Facter::Util::Resolution.exec("/bin/grep 'fact_apply10=' /etc/mcollective/facts.txt |awk -F= '{print $2}'")
     end
end
3.2 设置局部模块
如果自定义的fact属于某一个模块下具有的特性,只需要将fact信息定义到对应的模块中即可,无需创建全局fact模块,比如放在mysql模块中等。
4 开启模块插件功能
当pluginsync选项设置为true后,就打开了“模块中的插件”功能。当agent连接到master时,每一个agent都会检查他们的模块中的自定义代码。Puppet会将这些自定义代码同步到相关的agent中。然后他们就能在这些agent中使用了。
[iyunv@puppetserver ~]# vim /etc/puppet/puppet.conf
[main]
pluginsync = true

[iyunv@agent2 ~]# vim /etc/puppet/puppet.conf
[main]
pluginsync = true

5 节点上测试自定义fact
5.1 节点运行puppet命令更新
[iyunv@agent2 ~]# puppet agent --test
info: Retrieving plugin
notice: /File[/var/lib/puppet/lib/facter/fact_apply.rb]/ensure: defined content as '{md5}03bdfe12d6f40fb8abe0bd407dab6d69'
info: Loading downloaded plugin /var/lib/puppet/lib/facter/fact_apply.rb #自动下载
info: Loading facts in /var/lib/puppet/lib/facter/backup_date.rb
info: Loading facts in /var/lib/puppet/lib/facter/fact_apply.rb #自动载入
info: Caching catalog for agent2.kisspuppet.com
info: Applying configuration version '1381211740'
5.2 通过节点查看自定义fact是否生效
[iyunv@agent2 ~]# facter -p | grep fact_
fact_apply2 => php
fact_apply3 => mysql
fact_apply7 => nginx
fact_certname => agent1.kisspuppet.com
[iyunv@agent1 facter]# facter -p | grep fact_
fact_certname => agent2.kisspuppet.com
fact_apply10 => db2
fact_apply3 => mysql
fact_apply4 => java
6 MCollective客户端测试自定义fact
[iyunv@puppetserver facter]# mco inventory agent1.kisspuppet.com | grep fact_
      fact_apply10 => db2
      fact_apply3 => mysql
      fact_apply4 => java
fact_certname => agent1.kisspuppet.com
[iyunv@puppetserver facter]# mco inventory agent2.kisspuppet.com | grep fact_
      fact_apply2 => php
      fact_apply3 => mysql
      fact_apply7 => nginx
fact_certname => agent2.kisspuppet.com
7 通过自定义facter定位主机触发更新
7.1 触发更新fact_apply4=java的主机
自定义fact fact_apply4=‘java’的主机目前只有agent1
[iyunv@puppetserver facter]# mco puppet -v runonce  mco facts -v --with-fact  fact_apply4='java'
Discovering hosts using the mc method for 2 second(s) .... 1
* [ ============================================================> ] 1 / 1
agent1.kisspuppet.com                      : OK
    {:summary=>      "Started a background Puppet run using the 'puppet agent --onetime --daemonize --color=false --splay --splaylimit 30' command"}
---- rpc stats ----
           Nodes: 1 / 1
     Pass / Fail: 1 / 0
      Start Time: Tue Oct 08 14:24:08 +0800 2013
  Discovery Time: 2003.39ms
      Agent Time: 1091.75ms
      Total Time: 3095.14ms
7.2 触发更新fact_apply3=mysql和系统为RHEL5.7的主机
自定义fact fact_apply3=‘mysql’的主机有agent1和agent2,系统为RHEL5.7的主机只有agent2(通过系统自带fact获取),取交集,只有agent2会被触发更新。
[iyunv@puppetserver facter]# mco puppet -v runonce   rpc --np -F  operatingsystemrelease='5.7' -F  fact_apply3='mysql'
Discovering hosts using the mc method for 2 second(s) .... 1
agent2.kisspuppet.com                      : OK
    {:summary=>      "Started a background Puppet run using the 'puppet agent --onetime --daemonize --color=false --splay --splaylimit 30' command"}
---- rpc stats ----
           Nodes: 1 / 1
     Pass / Fail: 1 / 0
      Start Time: Tue Oct 08 14:22:23 +0800 2013
  Discovery Time: 2004.56ms
      Agent Time: 1092.00ms
      Total Time: 3096.56ms


运维网声明 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-17996-1-1.html 上篇帖子: puppet通过filebucket实现生产节点文件的恢复介绍 下篇帖子: puppet结合SVN版本控制系统实现版本的集中化备份与恢复
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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