MCollective结合自定义facter对puppet节点进行分类触发操作


如果需要交流puppet 可加入puppet技术交流QQ群 296934942

实验环境:

puppetserver.rsyslog.org

MCollective客户端+ACtiveMQ服务端+Puppet服务端


agent1.rsyslog.org

MCollective服务端+Puppet客户端

应用:apache


agent2.rsyslog.org

MCollective服务端+Puppet客户端

应用:apache+mysql


agent3.rsyslog.org

MCollective服务端+Puppet客户端

应用:php


实验过程:

一、定义服务器变量列表
所有服务器变量列表
MY_APPLY1=apache
MY_APPLY2=mysql
MY_APPLY3=php
建议添加到所有服务器的/etc/profile文件中,然后source
主机变量:agent1.rsyslog.org
vim /etc/profile
MY_APPLY1=apache
export MY_APPLY1
export FACTERLIB=/var/lib/puppet/lib/facter
[root@agent1 ~]# source /etc/profile

主机变量:agent2.rsyslog.org
vim /etc/profile
MY_APPLY1=apache
MY_APPLY2=mysql
export MY_APPLY1
export MY_APPLY2
export FACTERLIB=/var/lib/puppet/lib/facter

主机变量:agent3.rsyslog.org
vim /etc/profile
MY_APPLY3=php
export MY_APPLY3
export FACTERLIB=/var/lib/puppet/lib/facter

二、自定义facter
1、创建目录结构
[root@puppetserver modules]# mkdir public/{modules,manifests,files,lib/facter}  -p

2、服务器和节点都打开模块中的插件功能
[root@puppetserver public]# vim /etc/puppet/puppet.conf
[main]
   pluginsync = true
3、编写自定义fact
[root@puppetserver public]# vim /etc/puppet/modules/public/lib/facter/my_apply1.rb
# my_apply1.rb
#
Facter.add("my_apply1") do
  setcode do
      Facter::Util::Resolution.exec('/bin/echo $MY_APPLY1')
    end
end
[root@puppetserver public]# vim /etc/puppet/modules/public/lib/facter/my_apply2.rb
# my_apply2.rb
#
Facter.add("my_apply2") do
  setcode do
      Facter::Util::Resolution.exec('/bin/echo $MY_APPLY2')
    end
end
[root@puppetserver public]# vim /etc/puppet/modules/public/lib/facter/my_apply3.rb
# my_apply3.rb
#
Facter.add("my_apply3") do
  setcode do
      Facter::Util::Resolution.exec('/bin/echo $MY_APPLY3')
    end
end

4、本地建立环境变量
[root@puppetserver public]# export FACTERLIB=/etc/puppet/modules/public/lib/facter

5、测试fact(如果不正常,会显示调试信息)因为本机并未定义,所有没有显示结果
[root@puppetserver facter]# facter my_apply1
[root@puppetserver facter]# facter my_apply2
[root@puppetserver facter]# facter my_apply3

6、服务器上运行一次puppet程序,只下放自定义的facter
[root@puppetserver facter]# mco puppet -v runonce

7、查看任意节点目录下是否下载了自定义的rb模版
[root@agent2 ~]# ll /var/lib/puppet/lib/facter
total 20
-rw-r--r-- 1 root root 125 Aug 26 11:19 backup_date.rb
-rw-r--r-- 1 root root 194 Aug 26 11:19 my_apply1.rb
-rw-r--r-- 1 root root 132 Aug 26 11:19 my_apply2.rb
-rw-r--r-- 1 root root 132 Aug 26 11:19 my_apply3.rb

8、重启所有节点的mcollective服务(可通过puppet资源实现)
/etc/rc.d/init.d/mcollective restart


三、测试自定义节点是否能够被使用
1、节点端测试
[root@agent1 ~]# facter my_apply1
apache
[root@agent1 ~]# facter my_apply2
[root@agent1 ~]# facter my_apply3

[root@agent2 ~]# facter my_apply3
[root@agent2 ~]# facter my_apply2
mysql
[root@agent2 ~]# facter my_apply1
apache

[root@agent3 ~]# facter my_apply1
[root@agent3 ~]# facter my_apply2
[root@agent3 ~]# facter my_apply3
php

2、MCollective客户端测试
[root@puppetserver facter]# mco inventory agent1.rsyslog.org  | grep my_apply
     my_apply1 => apache
[root@puppetserver facter]# mco inventory agent2.rsyslog.org  | grep my_apply
     my_apply1 => apache
     my_apply2 => mysql
[root@puppetserver facter]# mco inventory agent3.rsyslog.org  | grep my_apply
     my_apply3 => php

3、通过自定义的facter进行过滤运行puppetd服务
3.1、只运行变量my_apply3='php'的所有节点    
[root@puppetserver facter]# mco puppet -v runonce  mco facts -v --with-fact  my_apply3='php'
Discovering hosts using the mc method for 2 second(s) .... 1

* [ ============================================================> ] 1 / 1


agent3.rsyslog.org                     : 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: Mon Aug 26 11:56:53 +0800 2013
 Discovery Time: 2020.83ms
     Agent Time: 1368.84ms
     Total Time: 3389.67ms

3.2、只运行变量 my_apply1='apache'和 my_apply2='mysql'的所有节点
[root@puppetserver facter]# mco puppet -v runonce   rpc --np -F my_apply1='apache' -F my_apply2='mysql'
Discovering hosts using the mc method for 2 second(s) .... 1

agent2.rsyslog.org                     : 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: Mon Aug 26 11:58:07 +0800 2013
 Discovery Time: 2003.38ms
     Agent Time: 1051.53ms
     Total Time: 3054.91ms