参考下面例子 在不同的 puppet client 上查询 facter 变量, 返回不同的变量值
[root@terryzeng-gz-qa-dns-d4yzu ~]# facter ipaddress_eth0 10.199.198.161 [root@terryzeng-gz-qa-dns-vid7e ~]# facter ipaddress_eth0 10.199.251.52也可以利用 json 格式进行数据返回
[root@terryzeng-gz-qa-dns-d4yzu facter]# facter -j partitions { "partitions": { "vda1": { "size": "41940992", "mount": "/", "filesystem": "ext4", "uuid": "0e6758d5-26c9-4cc6-ba78-281768d8855c" } } }查询所有参数
[root@terryzeng-gz-qa-dns-d4yzu ~]# facter architecture => x86_64 augeasversion => 1.0.0 bios_release_date => 01/01/2011 bios_vendor => Seabios bios_version => 0.5.1 blockdevice_vda_size => 21474836480 blockdevice_vda_vendor => 6900 ... ... uniqueid => c70aa1c6 uptime => 185 days uptime_days => 185 uptime_hours => 4462 uptime_seconds => 16065002 uuid => 14292DDA-257C-408D-8B25-9C4D6DAB3B45 virtual => kvm参考自定义 facter 官网
1. 当前 rhel6 默认 facter 变量 rb 文件可以存放在 ruby 库目录 '/usr/lib/ruby/site_ruby/1.8/facter' 中, 也可以存放在 ' /var/lib/puppet/lib/facter/' 目录中 2. 可以通过 RUBYLIB 环境变量定义 ruby 的库文件位置当前直接在 puppet client 端创建一个新的 facter 变量, 称为 myip, 目的为返回当前主机的 IP 地址
/usr/lib/ruby/site_ruby/1.8/facter/myip.rb
Facter.add(:myip) do setcode do require 'socket' IPSocket.getaddress(Socket.gethostname) end end参考下面的自定义模块
/etc/puppet/modules/myfacter ├── manifests │ └── init.pp └── templates └── testmyfacter.txt.erb参考整个调用过程
/etc/puppet/manifests/terry ├── terry-crond.pp ├── terry-factertest.pp <- facter 模块调用, 自动执行上面的 init.pp ├── terry-files.pp ├── terry-hosts.pp ├── terry-package.pp ├── terry-parameter.pp ├── terry-site.pp <- 流程控制 调用 terry-factertest.pp 文件 ├── terry-sysctl.pp └── terry-yumrepo.pp参考整个流程
/etc/puppet/manifests/terry/terry-site.pp
node 'terry-rhel7.vclound.com', 'terryzeng-gz-qa-dns-d4yzu.vclound.com', 'terryzeng-gz-qa-dns-vid7e.vclound.com' { import 'terry-parameter.pp' <- 参数配置, 目前没有调用, 下面会有说明 import 'terry-sysctl.pp' import 'terry-hosts.pp' import 'terry-yumrepo.pp' import 'terry-package.pp' import 'terry-files.pp' import 'terry-crond.pp' import 'terry-factertest.pp' }/etc/puppet/manifests/terry/terry-factertest.pp
class { myfacter: }/etc/puppet/modules/myfacter/manifests/init.pp
class myfacter { file { '/tmp/testmyfacter.txt': owner => root, group => root, mode => 0644, content => template('myfacter/testmyfacter.txt.erb'), } }/etc/puppet/modules/myfacter/templates/testmyfacter.txt.erb
# This is a example file, use for test facter. this is my ip address <%= @myip %> from facter. this is my fqdn_hostname <%= @fqdn %> from facter.目前以 (10.199.198.161 | terryzeng-gz-qa-dns-d4yzu.vclound.com) (手动维护 myip.rb) 为测试例子说明, 当 terryzeng-gz-qa-dns-d4yzu.vclound.com 连接 puppet master 后, /tmp/testmyfacter.txt 文件内容如下:
[root@terryzeng-gz-qa-dns-d4yzu ~]# cat /tmp/testmyfacter.txt # This is a example file, use for test facter. this is my ip address 10.199.198.161 from facter. this is my fqdn_hostname terryzeng-gz-qa-dns-d4yzu.vclound.com from facter.从上面可以看到, 我们不需要对变量进行定义, 模板中的变量环境会自动被置换成为对应 facter 的值. 再次强调, 不推荐自定义 facter 的方式进行变量定义, 下面例子将不再使用 myip.rb facter 变量, 另外, 假如你需要使用 ip 地址变量, 可以通过使用 ipaddress_eth0 facter 变量获得
自定义变量方法 参考下面模板, 我们介绍几种常见的变量定义方法
$addr = 'the_first_variable_addr', <- 单个变量定义 $addr_array = [ 'array_1', 'array_2', 'array_3' ], <- array 变量定义 $addr_outside = $crond::config::time, <- 外部变量调用参考文件结构
/etc/puppet/modules/myfacter ├── manifests │ ├── config.pp <- 配置 │ ├── init.pp <- 主控制 │ └── para.pp <- 模板内部参数定义文件 └── templates └── testmyfacter.txt.erb <- 模板文件主控制流程
class myfacter ( $addr = 'the_first_variable_addr', $addr_array = [ 'array_1', 'array_2', 'array_3' ], $addr_outside = $crond::config::time, $addr_var = 'variable_in_init_pp', ){ include myfacter::para, myfacter::config }说明:
1. 定义了独立变量, array, 外部变量, 2. 调用执行 para.pp, config.pp 3. 这里 $crond::config::time 意味着, 获取的是 /etc/pupppet/modules/crond/manifests/config.pp 中的变量 time 4. init.pp 中定义的参数可以执行继承到 para.pp 与 config.pp 中, 属于公共变量参考模板中的参数定义文件
/etc/puppet/modules/myfacter/manifests/para.pp class myfacter::para ( $newvar = 'data_from_para_pp', ){ }说明:
1. 比较推荐在模板中的一个位置定义所有的公共变量, 方便管理, 如 para.pp 2. 变量方法跟之前一样 3. para.pp 中的变量属于内部变量, 不可以直接被其他的模板文件使用.参考模板中配置文件
/etc/puppet/modules/myfacter/manifests/config.pp class myfacter::config ( $addr_inside = $myfacter::para::newvar, $addr_var = 'variable_in_config_pp', ){ file { '/tmp/testmyfacter.txt': owner => root, group => root, mode => 0644, content => template('myfacter/testmyfacter.txt.erb'), } }说明:
1. addr_inside 属于变量继承 (para.pp 中的 newvar), 不可以直接使用 newvar变量. (特别注意) 2. 假如需要直接调用 para.pp, 着需要添加继承关键字 class ( xxx ) inherits myfacter::para { xxx } 3. addr_var 本在 init.pp 中已经定义过, 这里的作用是覆盖之前的变量值 4. file 模块用于配置一个模板文件, 并在这时候调用之前的所有的变量参考模板文件
/etc/puppet/modules/myfacter/templates/testmyfacter.txt.erb # This is a example file, use for test facter. this is my ip address <%= @ipaddress_eth0 %> from facter. this is my fqdn_hostname <%= @fqdn %> from facter. this is the variable <%= @addr %> from modules/myfacter/manifests/init.pp. this is the array <%= @addr_array %> from modules/myfacter/manifests/init.pp. this is the array @ <%= @addr_array[0] %> from modules/myfacter/manifests/init.pp. this is the array @ <%= @addr_array[1] %> from modules/myfacter/manifests/init.pp. this is the variable <%= @addr_outside %> from modules/crond/manifests/config.pp. this is the variable <%= @addr_inside %> from modules/myfacter/manifests/para.pp. this is the variable <%= @addr_var %> from config.pp not init.pp.说明:
1. 在调用 array 时候, 如果不描述下标, 那么这里会返回所有值 2. array[0] 指第一个被数组变量值, 3. 除了 ipaddress_eth0, fqdn 以外, 所有的变量都是自定义变量参考 puppet client 推送后的文件结果
cat /tmp/testmyfacter.txt # This is a example file, use for test facter. this is my ip address 10.199.198.161 from facter. this is my fqdn_hostname terryzeng-gz-qa-dns-d4yzu.vclound.com from facter. this is the variable the_first_variable_addr from modules/myfacter/manifests/init.pp. this is the array array_1array_2array_3 from modules/myfacter/manifests/init.pp. this is the array @ array_1 from modules/myfacter/manifests/init.pp. this is the array @ array_2 from modules/myfacter/manifests/init.pp. this is the variable 26 from modules/crond/manifests/config.pp. this is the variable data_from_para_pp from modules/myfacter/manifests/para.pp. this is the variable variable_in_config_pp from class.