- Puppet 教程
- Puppet - 首页
- 基础 Puppet
- Puppet - 概述
- Puppet - 架构
- Puppet - 安装
- Puppet - 配置
- Puppet - 环境配置
- Puppet - Master
- Puppet - Agent 设置
- Puppet - SSL 证书设置
- 安装和配置 r10K
- Puppet - 验证设置
- Puppet - 编码风格
- Puppet - 清单文件
- Puppet - 模块
- Puppet - 文件服务器
- Puppet - Facter 和 Facts
- 高级 Puppet
- Puppet - 资源
- Puppet - 资源抽象层
- Puppet - 模板
- Puppet - 类
- Puppet - 函数
- Puppet - 自定义函数
- Puppet - 环境
- Puppet - 类型和提供程序
- Puppet - RESTful API
- Puppet - 实时项目
- Puppet 有用资源
- Puppet - 快速指南
- Puppet - 有用资源
- Puppet - 讨论
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