原文:http://kisspuppet.com/2013/11/10/my-fact/

www.kisspuppet.com puppet實戰

歡迎puppet愛好者加入自動化運維交流總QQ群:296934942

目前由於Facter並不全面,許多關於主機和環境的資訊並沒有作為Facter的fact。編寫自定義的fact,可以讓節點的facter包含更多的後設資料fact,增加MCollective選擇後設資料定位主機的靈活性。

1 自定義節點變數

首選,需要在每個節點自定義一個facts文件,文件中包含了每個節點自定義的fact資訊。為了方便管理,所有變數的值都必須事先定義好,可在puppet服務端定義一個fact變數列表,裡面包含所有節點的自定義fact資訊。然後,節點根據各自的主機特性選擇合適的fact資訊。

[root@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
…
[root@agent1 ~]# cat /etc/mcollective/facts.txt #假設agent1節點具有以下fact變數資訊
fact_certname=agent1.kisspuppet.com #puppet認證用,可寫成其他名稱
fact_apply3=mysql
fact_apply4=java
fact_apply10=db2
[root@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{}中。

[root@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`,
        }
}
...
[root@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
...
----------------------------------------------------
[root@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`,
        }
}
...
[root@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資訊)

[root@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中使用了。

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

5 節點上測試自定義fact

5.1 節點執行puppet命令更新

[root@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是否生效

[root@agent2 ~]# facter -p | grep fact_
fact_apply2 => php
fact_apply3 => mysql
fact_apply7 => nginx
fact_certname => agent1.kisspuppet.com
[root@agent1 facter]# facter -p | grep fact_
fact_certname => agent2.kisspuppet.com
fact_apply10 => db2
fact_apply3 => mysql
fact_apply4 => java

6 MCollective客戶端測試自定義fact

[root@puppetserver facter]# mco inventory agent1.kisspuppet.com | grep fact_
      fact_apply10 => db2
      fact_apply3 => mysql
      fact_apply4 => java
fact_certname => agent1.kisspuppet.com
[root@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

[root@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會被觸發更新。

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