Puppet支持将多个值保存为环境变量. Puppet中使用 facter 支持此功能.在Puppet中,facter是一个保存环境级变量的独立工具.可以认为类似于Bash或Linux的env变量.有时,事实中存储的信息与机器的环境变量之间可能存在重叠.在Puppet中,键值对被称为"事实".每个资源都有自己的事实,在Puppet中,用户可以利用它来构建自己的自定义事实.
#facter
Facter命令可用于列出所有不同的环境变量及其相关值.这些事实的集合带有开箱即用的东西,被称为核心事实.可以将自定义事实添加到集合中.
如果只想查看一个变量.可以使用以下命令完成.
# facter {Variable Name} Example [root@puppetmaster ~]# facter virtual virtualbox
之所以对于Puppet很重要的原因是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中,有三种类型的事实可以使用,定义 :
核心事实
自定义事实
外部事实
核心事实在顶层定义,并且可以在代码中的任何位置访问.
Puppet Facts
在代理从主服务器请求目录之前,代理首先以密钥值对的形式编译自身可用信息的完整列表.有关代理的信息由名为facter的工具收集,每个键值对都被称为事实.以下是代理商的常见事实输出.
[root@puppetagent1 ~]# facterarchitecture => 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.7selinux => true selinux_config_mode => enforcing selinux_config_policy => targeted selinux_current_mode => enforcing selinux_enforced => true selinux_policyversion => 24 serialnumber => 0 sshdsakey => AAAAB3NzaC1kc3MAAACBAK5fYwRM3UtOs8zBCtRTjuHLw56p94X/E0UZBZwFR3q7WH0x5+MNsjfmdCxKvpY/WlIIUcFJzvlfjXm4qDaTYalbzSZJMT266njNbw5WwLJcJ74KdW92ds76pjgmCsjAh+R9YnyKCEE35GsYjGH7whw0gl/rZVrjvWYKQDOmJA2dAAAAFQCoYABgjpv3EkTWgjLIMnxA0GfudQAAAIBM4U6/nerfn6Qvt43FC2iybvwVo8ufixJl5YSEhs92uzsW6jiw68aaZ32q095/gEqYzeF7a2knrOpASgO9xXqStYKg8ExWQVaVGFTR1NwqhZvz0oRSbrN3h3tHgknoKETRAg/imZQ2P6tppAoQZ8wpuLrXUCyhgJGZ04Phv8hinAAAAIBN4xaycuK0mdH/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数据视为全局变量.
然后事实作为顶级变量提供,Puppet master可以使用他们为请求代理编译Puppet目录.掩饰在清单中作为普通变量用$前缀调用.
示例
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 的事实扣除.
[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
自定义事实
我们所见的所有上述事实都是机器的核心事实.可以通过以下方式将此自定义事实添加到节点 :
使用"export FACTER ... Syntax"
使用$ LOAD_PATH设置
FACTERLIB
Pluginsync
使用"export FACTER"语法
可以使用导出FACTER_ {fact's name}语法手动添加事实.
示例
[root @ puppetagent1 facter] #export FACTER_tallest_mountain ="Everest" [root @ puppetagent1 facter] #fieter tallest_mountain Everest
使用$ LOAD_PATH设置
在Ruby中,$ LOAD_PATH等同于Bash特殊参数.虽然它类似于bash $ PATH变量,但事实上$ 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
让我们举一个创建目录因素并添加 .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文件,并加载有关内存中任何特定配置的所有已定义事实.
使用FACTERLIB
在Puppet中,FACTERLIB与$ LOAD_PATH非常相似,但只有一个关键区别,它是OS级环境参数而不是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
测试新的因素.
[root@puppetagent1 /]# facter longest_river Nile [root@puppetagent1 /]# facter longest_wall China Wall
外部事实
当用户希望应用在配置时创建的一些新事实时,外部事实非常有用.外部事实是在配置阶段将虚拟机应用于虚拟机的关键方式之一(例如,使用vSphere,OpenStack,AWS等).
创建的所有元数据及其详细信息均可Puppet用来确定目录中应该包含哪些细节.
创建外部事实
在代理上机器,我们需要创建一个如下所述的目录.
$ 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中编写自定义事实.作为参考,请使用Puppet站点中的以下链接.
https://docs.puppet.com/facter/latest/fact_overview.html