puppet 利用 facter, 实现不同主机调用不同变量方法



分析¶

变量类型常见三种

1. 内建变量, 如 $title, $name 等, 针对类固定的格式而进行调用
2. 自定义变量, 如 myname = tttttt, 等, 但需要在每个类中进行独立定义, 不能够在类之间进行传递, 
3. facter , 公共变量, 可以在每个类中共同使用, 当前打算使用 facter 作为变量的调用

测试1.¶

修改 /etc/puppet/fileserver.conf 如下, 目的, 创建文件传输目录

grep -v "#" /etc/puppet/fileserver.conf  | grep -v ^$
[extra_files]
  path /etc/puppet/files
  allow *

创建对应目录

mkdir /etc/puppet/files

把 facter 模板存放至 /etc/puppet/files

cat /etc/puppet/files/roles.txt

db01ip=10.10.10.1
puppetip=10.10.0.100
db01sqlip=10.10.10.2
db01user=terry
db02ip=10.10.20.2
db02sqlip=10.10.10.4
db02user=bbxxswqq

创建测试用 puppet 推送模板

[root@gd02-openstack-puppet manifests]# tree
.
├── openstack
│   ├── cinder.vclound.com.pp
│   ├── db.vclound.com.pp
│   ├── galera.vclound.com.pp
│   ├── glance.vclound.com.pp
│   ├── glusterfs.vclound.com.pp
│   ├── haproxy.vclound.com.pp
│   ├── keystone.vclound.com.pp
│   └── test.pp                  <--- 测试用模板
└── site.pp

测试模板内容

[root@gd02-openstack-puppet manifests]# cat openstack/test.pp
node 'gd02-openstack-testdb1.vclound.com', 'gd02-openstack-testdb2.vclound.com' {

  file { [ '/etc/facter','/etc/facter/facts.d']:
      ensure => directory,
      owner => root, group => root,
      mode => 0755,
  }

  file { '/etc/facter/facts.d/roles.txt':
      source =>  "puppet://$fileserver/extra_files/roles.txt",
      owner => root, group => root,
      mode => 0755,
      require => File [ '/etc/facter','/etc/facter/facts.d'],
  }

  if $fqdn == "gd02-openstack-testdb1.vclound.com" 
  {
        file { "/tmp/test.txt":
                content => "dbip=$db01ip\npuppet=puppetip\ndbsql=$db01sqlip\ndbuser=$db01user\n",
                owner => root, group => root,
                mode => 644,
                require => File ['/etc/facter/facts.d/roles.txt'],
        }

  }elsif $fqdn == "gd02-openstack-testdb2.vclound.com" 
  {
        file { "/tmp/test.txt":
                content => "dbip=$db02ip\npuppet=$puppetip\ndbsql=$db02sqlip\ndbuser=$db02user\n",
                owner => root, group => root,
                mode => 644,
               require => File ['/etc/facter/facts.d/roles.txt'],
        }

  }

}

目的, 分别在 'gd02-openstack-testdb1.vclound.com', 'gd02-openstack-testdb2.vclound.com' 主机中添加 /tmp/test.txt
文件中分别调用 facter 变量
效果如下

testdb1 内容

[root@gd01-openstack-testdb1 ~]# cat /tmp/test.txt
dbip=10.10.10.1
puppet=10.10.0.100
dbsql=10.10.10.2
dbuser=terry

testdb2 内容

[root@gd02-openstack-testdb2 /]# cat /tmp/test.txt
dbip=10.10.20.2
puppet=10.10.0.100
dbsql=10.10.10.4
dbuser=bbxxswqq

结论: 可以在不同主机上匹配不同的 facter 内容

你可能感兴趣的:(puppet,facter)