Puppet - Facter 和 Facts



Puppet 支援將多個值作為環境變數儲存。此功能在 Puppet 中透過使用facter來支援。在 Puppet 中,facter 是一個獨立的工具,用於儲存環境級變數。它可以被認為類似於 Bash 或 Linux 的 env 變數。有時,facts 中儲存的資訊與機器的環境變數之間可能存在重疊。在 Puppet 中,鍵值對被稱為“fact”。每個資源都有自己的 facts,並且在 Puppet 中,使用者可以利用它來構建自己的自定義 facts。

# facter 

Facter 命令可用於列出所有不同的環境變數及其關聯的值。這些 facts 集合隨 facter 開箱即用,被稱為核心 facts。可以向集合中新增自定義 facts。

如果只想檢視一個變數,可以使用以下命令。

# facter {Variable Name}  

Example 
[root@puppetmaster ~]# facter virtual 
virtualbox 

facter 對 Puppet 如此重要的原因是,facter 和 facts 在整個 Puppet 程式碼中都可用作“全域性變數”,這意味著它可以在程式碼的任何時間點使用,而無需任何其他引用。

測試示例

[root@puppetmaster modules]# tree brcle_account 
brcle_account 
└── manifests  └── init.pp [root@puppetmaster modules]# cat brcle_account/manifests/init.pp  
class brcle_account {  
   user { 'G01063908': 
      ensure => 'present', 
      uid => '121', 
      shell => '/bin/bash', 
      home => '/home/G01063908', 
   }  
   
   file {'/tmp/userfile.txt': 
      ensure => file, 
      content => "the value for the 'OperatingSystem' fact is: $OperatingSystem \n", 
   } 
} 

測試它

[root@puppetmaster modules]# puppet agent --test 
Notice: /Stage[main]/Activemq::Service/Service[activemq]/ensure: 
ensure changed 'stopped' to 'running' 
Info: /Stage[main]/Activemq::Service/Service[activemq]: 
Unscheduling refresh on Service[activemq] 

Notice: Finished catalog run in 4.09 seconds  
[root@puppetmaster modules]# cat /tmp/testfile.txt  
the value for the 'OperatingSystem' fact is: Linux   

[root@puppetmaster modules]# facter OperatingSystem 
Linux

正如我們在上面的程式碼片段中看到的,我們沒有定義OperatingSystem。我們只是用軟編碼值$OperatingSystem作為普通變數替換了該值。

在 Puppet 中,可以使用和定義三種類型的 fact:

  • 核心 Facts
  • 自定義 Facts
  • 外部 Facts

核心 facts 在頂層定義,並且在程式碼中的任何位置都可以訪問。

Puppet Facts

在代理向 master 請求目錄之前,代理首先會編譯一個在其自身中可用的資訊的完整列表,這些資訊以鍵值對的形式存在。代理上的資訊由名為 facter 的工具收集,每個鍵值對稱為 fact。以下是代理上 facts 的常見輸出。

[root@puppetagent1 ~]# facter
architecture => x86_64 
augeasversion => 1.0.0 
bios_release_date => 13/09/2012 
bios_vendor => innotek GmbH 
bios_version => VirtualBox 
blockdevice_sda_model => VBOX HARDDISK 
blockdevice_sda_size => 22020587520 
blockdevice_sda_vendor => ATA 
blockdevice_sr0_model => CD-ROM 
blockdevice_sr0_size => 1073741312 
blockdevice_sr0_vendor => VBOX 
blockdevices => sda,sr0 
boardmanufacturer => Oracle Corporation 
boardproductname => VirtualBox 
boardserialnumber => 0 

domain => codingbee.dyndns.org  
facterversion => 2.1.0 
filesystems => ext4,iso9660 
fqdn => puppetagent1.codingbee.dyndns.org 
hardwareisa => x86_64 
hardwaremodel => x86_64 
hostname => puppetagent1 
id => root 
interfaces => eth0,lo 
ipaddress => 172.228.24.01 
ipaddress_eth0 => 172.228.24.01 
ipaddress_lo => 127.0.0.1 
is_virtual => true 
kernel => Linux 
kernelmajversion => 2.6 
kernelrelease => 2.6.32-431.23.3.el6.x86_64 
kernelversion => 2.6.32 
lsbdistcodename => Final 
lsbdistdescription => CentOS release 6.5 (Final) 
lsbdistid => CentOS 
lsbdistrelease => 6.5 
lsbmajdistrelease => 6 
lsbrelease => :base-4.0-amd64:base-4.0-noarch:core-4.0-amd64:core-4.0noarch:graphics-4.0-amd64:
graphics-4.0-noarch:printing-4.0-amd64:printing-4.0noarch 
macaddress => 05:00:22:47:H9:77 
macaddress_eth0 => 05:00:22:47:H9:77 
manufacturer => innotek GmbH 
memoryfree => 125.86 GB 
memoryfree_mb => 805.86 
memorysize => 500 GB 
memorysize_mb => 996.14 
mtu_eth0 => 1500 
mtu_lo => 16436 
netmask => 255.255.255.0 
netmask_eth0 => 255.255.255.0  

network_lo => 127.0.0.0 
operatingsystem => CentOS 
operatingsystemmajrelease => 6 
operatingsystemrelease => 6.5 
osfamily => RedHat 
partitions => {"sda1"=>{
"uuid"=>"d74a4fa8-0883-4873-8db0-b09d91e2ee8d", "size" =>"1024000", 
"mount" => "/boot", "filesystem" => "ext4"}, "sda2"=>{"size" => "41981952", 
"filesystem" => "LVM2_member"}
} 
path => /usr/lib64/qt3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin 
physicalprocessorcount => 1 
processor0 => Intel(R) Core(TM) i7 CPU         920  @ 2.67GHz 
processor1 => Intel(R) Core(TM) i7 CPU         920  @ 2.67GHz 
processor2 => Intel(R) Core(TM) i7 CPU         920  @ 2.67GHz 
processorcount => 3 
productname => VirtualBox 
ps => ps -ef 
puppetversion => 3.6.2 
rubysitedir => /usr/lib/ruby/site_ruby/1.8 
rubyversion => 1.8.7
selinux => true 
selinux_config_mode => enforcing 
selinux_config_policy => targeted 
selinux_current_mode => enforcing 
selinux_enforced => true 
selinux_policyversion => 24 
serialnumber => 0 
sshdsakey => AAAAB3NzaC1kc3MAAACBAK5fYwRM3UtOs8zBCtRTjuHLw56p94X/E0UZBZwFR3q7
WH0x5+MNsjfmdCxKvpY/WlIIUcFJzvlfjXm4qDaTYalbzSZJMT266njNbw5WwLJcJ74KdW92ds76pjgm
CsjAh+R9YnyKCEE35GsYjGH7whw0gl/rZVrjvWYKQDOmJA2dAAAAFQCoYABgjpv3EkTWgjLIMnxA0Gfud
QAAAIBM4U6/nerfn6Qvt43FC2iybvwVo8ufixJl5YSEhs92uzsW6jiw68aaZ32q095/gEqYzeF7a2knr
OpASgO9xXqStYKg8ExWQVaVGFTR1NwqhZvz0oRSbrN3h3tHgknoKETRAg/imZQ2P6tppAoQZ8wpuLrXU
CyhgJGZ04Phv8hinAAAAIBN4xaycuK0mdH/YdcgcLiSn8cjgtiETVzDYa+jF 
swapfree => 3.55 GB 
swapfree_mb => 2015.99 
swapsize => 3.55 GB 
swapsize_mb => 2015.99 
timezone => GMT 
type => Other 
uniqueid => a8c0af01 
uptime => 45:012 hours 
uptime_days => 0 
uptime_hours => 6 
uptime_seconds => 21865 
uuid => BD8B9D85-1BFD-4015-A633-BF71D9A6A741 
virtual => virtualbox 

在上面的程式碼中,我們可以看到一些資料與 bash “env” 變數中的一些資訊重疊。Puppet 不直接使用這些資料,而是使用 facter 資料,Facter 資料被視為全域性變數。

然後,這些 facts 可作為頂級變數使用,Puppet master 可以使用它們來編譯請求代理的 Puppet 目錄。Facters 在清單中以 $ 字首作為普通變數呼叫。

示例

if ($OperatingSystem == "Linux") { 
   $message = "This machine OS is of the type $OperatingSystem \n" 
} else { 
   $message = "This machine is unknown \n" 
} 

file { "/tmp/machineOperatingSystem.txt": 
   ensure => file, 
   content => "$message" 
}

上面的清單檔案只關心一個名為machineOperatingSystem.txt的檔案,其中此檔案的內容由名為OperatingSystem的 fact 推匯出。

[root@puppetagent1 /]# facter OperatingSystem 
Linux  

[root@puppetagent1 /]# puppet apply /tmp/ostype.pp 
Notice: Compiled catalog for puppetagent1.codingbee.dyndns.org 
in environment production in 0.07 seconds 
Notice: /Stage[main]/Main/File[/tmp/machineOperatingSystem.txt]/ensure: 
defined content as '{md5}f59dc5797d5402b1122c28c6da54d073' 
Notice: Finished catalog run in 0.04 seconds  

[root@puppetagent1 /]# cat /tmp/machinetype.txt 
This machine OS is of the type Linux

自定義 Facts

我們上面看到的所有 facts 都是機器的核心 facts。可以透過以下方式將這些自定義 facts 新增到節點中:

  • 使用“export FACTER … 語法”
  • 使用 $LOAD_PATH 設定
  • FACTERLIB
  • Pluginsync

使用“export FACTER”語法

可以使用 export FACTER_{fact 的名稱} 語法手動新增 facts。

示例

[root@puppetagent1 facter]# export FACTER_tallest_mountain="Everest" 
[root@puppetagent1 facter]# facter tallest_mountain Everest

使用 $LOAD_PATH 設定

在 Ruby 中,$LOAD_PATH 等效於 Bash 特殊引數。雖然它類似於 bash $PATH 變數,但在實際的 facts 中,$LOAD_PATH 不是環境變數,而是一個預定義的變數。

$LOAD_PATH 有一個同義詞“$:”。此變數是一個用於搜尋和載入值的陣列。

[root@puppetagent1 ~]# ruby -e 'puts $LOAD_PATH'            
# note you have to use single quotes.  
/usr/lib/ruby/site_ruby/1.6 
/usr/lib64/ruby/site_ruby/1.6 
/usr/lib64/ruby/site_ruby/1.6/x86_64-linux 
/usr/lib/ruby/site_ruby 
/usr/lib64/ruby/site_ruby 
/usr/lib64/site_ruby/1.6 
/usr/lib64/site_ruby/1.6/x86_64-linux 
/usr/lib64/site_ruby 
/usr/lib/ruby/1.6 
/usr/lib64/ruby/1.6 
/usr/lib64/ruby/1.6/x86_64-linux 

讓我們以建立一個名為 facter 的目錄並新增一個.pp檔案並將內容附加到其中的示例為例。

[root@puppetagent1 ~]# cd /usr/lib/ruby/site_ruby/ 
[root@puppetagent1 site_ruby]# mkdir facter 
[root@puppetagent1 site_ruby]# cd facter/ 
[root@puppetagent1 facter]# ls 
[root@puppetagent1 facter]# touch newadded_facts.rb 

將以下內容新增到 custom_facts.rb 檔案中。

[root@puppetagent1 facter]# cat newadded_facts.rb 
Facter.add('tallest_mountain') do 
   setcode "echo Everest" 
end 

Facter 的工作方式是掃描 $LOAD_PATH 中列出的所有資料夾,並查詢名為 facter 的目錄。一旦找到該特定資料夾,它就會在資料夾結構中的任何位置載入它們。如果找到此資料夾,則它會在該 facter 資料夾中查詢任何 Ruby 檔案,並將所有關於任何特定配置的已定義 facts 載入到記憶體中。

使用 FACTERLIB

在 Puppet 中,FACTERLIB 的工作方式與 $LOAD_PATH 非常相似,但只有一個關鍵區別,即它是一個作業系統級的環境引數,而不是 Ruby 特殊變數。預設情況下,可能未設定環境變數。

[root@puppetagent1 facter]# env | grep "FACTERLIB" 
[root@puppetagent1 facter]# 

要測試 FACTERLIB,我們需要執行以下步驟。

在以下結構中建立一個名為 test_facts 的資料夾。

[root@puppetagent1 tmp]# tree /tmp/test_facts/ 
/tmp/some_facts/ 
├── vipin 
│   └── longest_river.rb 
└── testing 
   └── longest_wall.rb 

將以下內容新增到 .rb 檔案中。

[root@puppetagent1 vipin]# cat longest_river.rb 
Facter.add('longest_river') do 
   setcode "echo Nile" 
end 

[root@puppetagent1 testing]# cat longest_wall.rb 
Facter.add('longest_wall') do 
   setcode "echo 'China Wall'" 
end 

使用 export 語句。

[root@puppetagent1 /]# export 
FACTERLIB = "/tmp/some_facts/river:/tmp/some_facts/wall" 
[root@puppetagent1 /]# env | grep "FACTERLIB" 
FACTERLIB = /tmp/some_facts/river:/tmp/some_facts/wall 

測試新的 facter。

[root@puppetagent1 /]# facter longest_river 
Nile 
[root@puppetagent1 /]# facter longest_wall 
China Wall 

外部 Facts

當用戶希望應用在預配時建立的一些新的 facts 時,外部 facts 非常有用。外部 facts 是在預配階段(例如,使用 vSphere、OpenStack、AWS 等)將元資料應用於 VM 的關鍵方法之一。

所有建立的元資料及其詳細資訊都可以由 Puppet 用於確定目錄中應存在哪些詳細資訊,這些詳細資訊將被應用。

建立外部 Fact

在代理機器上,我們需要建立一個如下所示的目錄。

$ mkdir -p /etc/facter/facts.d

在目錄中建立一個 Shell 指令碼,內容如下。

$ ls -l /etc/facter/facts.d 
total 4 
-rwxrwxrwx. 1 root root 65 Sep 18 13:11 external-factstest.sh 
$ cat /etc/facter/facts.d/external-factstest.sh 
#!/bin/bash 
echo "hostgroup = dev" 
echo "environment = development"

更改指令碼檔案的許可權。

$ chmod u+x /etc/facter/facts.d/external-facts.sh

完成後,我們現在可以看到具有鍵/值對的變數。

$ facter hostgroup 
dev 
$ facter environment 
development 

可以在 Puppet 中編寫自定義 facts。作為參考,請使用 Puppet 網站上的以下連結。

https://docs.puppet.com/facter/latest/fact_overview.html#writing-structured-facts

廣告