sudo puppet resource package hiera ensure=installed
也可以采用在线安装的方式
apt-
get
install hiera-puppet
[root@puppetmaster ~]# mv /etc/hiera.yaml /etc/puppet/
[root@puppetmaster ~]# ln -s /etc/puppet/hiera.yaml /etc/hiera.yaml
[root@puppetmaster ~]# ll /etc/hiera.yaml
lrwxrwxrwx 1root root 22Apr
2020
:
05
/etc/hiera.yaml -> /etc/puppet/hiera.yaml
添加全局变量common,注释掉defaults、global和clientcert。
添加系统类型变量osfamily
添加主机名变量hostname
备注: 以上变量其实就是fact变量。
root@crdc-c210-170:/etc/puppet# cat hiera.yaml
---
:backends:
- yaml
:hierarchy:
# - defaults
# - "%{clientcert}"
# - "%{environment}"
# - global
- common
- "%{environment}"
- "%{osfamily}"
- "%{hostname}"
:yaml:
# datadir is empty here, so hiera uses its defaults:
# - /var/lib/hiera on *nix
# - %CommonAppData%\PuppetLabs\hiera\var on Windows
# When specifying a datadir, make sure the directory exists.
# :datadir:"/etc/puppet/environments/%{environment}/hiera"
:datadir: /etc/puppet/hieradatadir
hiera的主配置文件必须为yaml格式,配置最上层key必须以冒号(:)开头
backends 定义了hiera数据文件的格式,支持yaml和json两种格式
hierarchy(层次结构)为数据源的层次顺序,即hiera查找的顺序,先从common中查找,如果没有则再往下,依次查找。
hierarchy中的值必须为字符串,其中common为默认数据源。
hiera主配置文件编写完成之后,需要重启puppetmaster后方可生效。
举例说明如下:
::clientcert
= web01.example.com
::environment
= production
::is_virtual
= true
查找到的yaml文件如下:
变量查找顺序:
# web01.example.com.yaml
mykey: one
# common.yaml
mykey:
- two
- three
(1)hiera默认先查找优先级高的yaml,如果找到相应的变量值,则不再继续向下查找。
如上例中,mykey一旦找到在web01.example.com.yaml找到相应内容,就不再查找common.yaml
(2) hiera 还支持j聚合查询
队列聚合:
array merge查询:会按照hierarchy定义的顺序,逐层查找相应的mykey,最后将得到的所有结果存入数组中并返回。
此例中,array merge方式返回的mykey为:[one, two, three]
.
哈希聚合:
详见: https://docs.puppetlabs.com/hiera/1/lookup_types.html
%{variable}
%{function("input")}
smtpserver: "mail.%{::domain}"
3.2使用查询函数
wordpress::database_server: "%{hiera('instances::mysql::public_hostname')}"
语法:
wordpress::database_server: "%{hiera('instances::mysql::public_hostname')}"
smtpserver: "mail.%{::domain}"
smtpserver: "mail.%{scope('::domain')}"
---
:hierarchy:
- "%{::clientcert}"
- "%{::custom_location}"
- "virtual_%{::is_virtual}"
- "%{::environment}"
- common
在以上层次结构中,除了最后一个源,其他源文件都将随着
::clientcert, ::custom_location, ::is_virtual,
::environment
等变量的出入值而随之变化.
:yaml:
:datadir: "/etc/puppet/hieradata/%{::environment}"
# /var/lib/hiera/common.yaml
---
smtpserver: "mail.%{::domain}"
# /var/lib/hiera/common.yaml
---
bacula::jobs:
"%{::hostname}_Cyrus":
fileset: MailServer
bacula_schedule: 'CycleStandard'
"%{::hostname}_LDAP":
fileset: LDAP
bacula_schedule: 'CycleStandard'
$::clientcert
::clientcert
Example:
# /etc/puppet/hieradata/appservers.yaml
---
proxies:
- hostname: lb01.example.com
ipaddress: 192.168.22.21
- hostname: lb02.example.com
ipaddress: 192.168.22.28
Good:
# Get the structured data:
$proxies = hiera('proxies')
# Index into the structure:
$use_ip = $proxies[1]['ipaddress'] # will be 192.168.22.28
/etc/puppet/manifests/site.pp
node "kermit.example.com" {
class { "ntp":
servers => [ '0.us.pool.ntp.org iburst','1.us.pool.ntp.org iburst','2.us.pool.ntp.org iburst','3.us.pool.ntp.org iburst'],
autoupdate => false,
restrict => [],
enable => true,
}
}
node "grover.example.com" {
class { "ntp":
servers => [ 'kermit.example.com','0.us.pool.ntp.org iburst','1.us.pool.ntp.org iburst','2.us.pool.ntp.org iburst'],
autoupdate => true,
restrict => [],
enable => true,
}
}
node "snuffie.example.com", "bigbird.example.com", "hooper.example.com" {
class { "ntp":
servers => [ 'grover.example.com', 'kermit.example.com'],
autoupdate => true,
enable => true,
}
}
---
:backends: ##源数据文件格式为yaml
- yaml
:yaml: ##yaml源文件存放路径
:datadir: /etc/puppet/hieradata
:hierarchy: ##源文件查询顺序
- "node/%{::fqdn}" ##此处的fqdn也可以换为certname,他们通常是一致的
- common
/etc/puppet/hieradata/node/grover.example.com.yaml
)。如果没有在node目录下找到匹配的yaml文件,将进入下一步common的查找。
/etc/puppet/hieradata/common.yaml)
,将定义一些默认设置。
ln -s /etc/puppet/hiera.yaml /etc/hiera.yaml
kermit.example.com和grover.example.com
配置如下:
vi /etc/puppet/hieradata/node/kermit.example.com.yaml
---
ntp::restrict:
-
ntp::autoupdate: false
ntp::enable: true
ntp::servers:
- 0.us.pool.ntp.org iburst
- 1.us.pool.ntp.org iburst
- 2.us.pool.ntp.org iburst
- 3.us.pool.ntp.org iburst
配置完后,测试一下:
hiera ntp::servers ::fqdn=kermit.example.com
然后配置下一个node:
grover.example.com.yaml
---
ntp::restrict:
-
ntp::autoupdate: true
ntp::enable: true
ntp::servers:
- kermit.example.com iburst
- 0.us.pool.ntp.org iburst
- 1.us.pool.ntp.org iburst
- 2.us.pool.ntp.org iburst
再测试一下:
hiera ntp::servers ::fqdn=grover.example.com
vi /etc/puppet/hieradata/common.yaml
---
ntp::autoupdate: true
ntp::enable: true
ntp::servers:
- grover.example.com iburst
- kermit.example.com iburst
测试:
hiera ntp::servers ::fqdn=snuffie.example.com
node "kermit.example.com", "grover.example.com", "snuffie.example.com" {
include ntp
# or:
# class { "ntp": }
}
hiera_include('classes')
(2)定义数据源文件yaml
kermit.example.com.yaml
---
---
classes:
- ntp
- apache
- postfix
ntp::restrict:
-
ntp::autoupdate: false
ntp::enable: true
ntp::servers:
- 0.us.pool.ntp.org iburst
- 1.us.pool.ntp.org iburst
- 2.us.pool.ntp.org iburst
- 3.us.pool.ntp.org iburst
测试:
$ hiera classes ::fqdn=kermit.example.com
["ntp", "apache", "postfix"]
---
:backends:
- yaml
:yaml:
:datadir: /etc/puppet/hieradata
:hierarchy:
- "node/%{::fqdn}"
- "virtual/%{::virtual}"
- "osfamily/%{osfamily}"
- common
其中fqdn、osfamily、virtual为factor变量
(2)创建相应数据源目录
`mkdir /etc/puppet/hieradata/virtual; mkdir /etc/puppet/hieradata/osfamily`
(3)配置数据源
version
vi /etc/puppet/hieradata/virtual/vmware.yaml
---
classes: vmwaretools
cd /etc/puppet/hieradata/osfamily
vi RedHat.yaml
---
vmwaretools::working_dir: /opt/vmware
vi Debian.yaml
---
vmwaretools::working_dir: /usr/local/vmware
将version参数放在common.yaml
vi /etc/puppet/hieradata/common.yaml
---
vmwaretools::version: 8.6.5-621624
ntp::autoupdate: true
ntp::enable: true
ntp::servers:
- grover.example.com iburst
- kermit.example.com iburst
测试:
$ hiera vmwaretools::working_dir osfamily=RedHat
/opt/vmware
$ hiera vmwaretools::working_dir osfamily=Debian
/usr/local/vmware
$ hiera vmwaretools::version
8.6.5-621624
$ hiera classes ::virtual=vmware
vmwaretools
参考链接:
https://docs.puppetlabs.com/hiera/1/complete_example.html