|
Links:
http://dreamfire.blog.iyunv.com/418026/1399014
https://github.com/ripienaar/hiera
https://docs.puppetlabs.com/hiera/1/complete_example.html
1.什么是Hiera
Hiera是一个强大的puppet外部节点分类器(ENC),利用这个套件,可以大大减少部署中的重复配置。更加灵活高效的配置和管理变量,类,节点配置。
Hiera的安装
在puppet 3.0中,hiera已经包含在puppetmaster安装包中,会默认安装。
在puppet 2.7中,需要执行以下命令安装:
sudo puppet resource package hiera ensure=installed
也可以采用在线安装的方式
yum install hiera hiera-puppet
apt-get install
hiera-puppet
2.Hiera主配置文件
2.1默认hiera.yaml主配置文件在/etc目录下,为了结合后期版本控制系统集中管理,建议将此文件copy到/etc/puppet目录下,然后创建软连接指向/etc/hiera.yaml即可。
[iyunv@puppetmaster ~]# mv /etc/hiera.yaml /etc/puppet/
[iyunv@puppetmaster ~]# ln -s /etc/puppet/hiera.yaml /etc/hiera.yaml
[iyunv@puppetmaster ~]# ll /etc/hiera.yaml
lrwxrwxrwx 1root root 22Apr 2020:05/etc/hiera.yaml
-> /etc/puppet/hiera.yaml
2.2编辑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
- production.yaml
- virtual_true.yaml
- common.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
3. hiera变量的引入
引入变量语法两种:
%{variable}
%{function("input")}
注意:变量的值只能为字符串,通常数字也被自动当做字符串处理
3.1引入一般变量
变量可以在字符串的任何位置插入。
smtpserver: "mail.%{::domain}"
3.2使用查询函数
两种查询函数:hiera()和scope()
举例说明如下:
wordpress::database_server: "%{hiera('instances::mysql::public_hostname')}"
语法:
传入的参数必须用引号。
函数和插入的参数名称中,不能有空格。
hiera()函数只能用在数据源中。
wordpress::database_server: "%{hiera('instances::mysql::public_hostname')}"
scope()可以引入变量到字符串中:
以下两种方式,效果是一样的
smtpserver: "mail.%{::domain}"
smtpserver: "mail.%{scope('::domain')}"
3.3插入变量
(1)数据源中插入(in data source):
---
:hierarchy:
- "%{::clientcert}"
- "%{::custom_location}"
- "virtual_%{::is_virtual}"
- "%{::environment}"
- common
在以上层次结构中,除了最后一个源,其他源文件都将随着::clientcert, ::custom_location, ::is_virtual, ::environment 等变量的出入值而随之变化.
甚至可以动态定义datadir
:yaml:
:datadir: "/etc/puppet/hieradata/%{::environment}"
(2)yaml文件中插入变量
# /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'
3.4变量的引用
在原始的puppet中变量引用方式为 $::clientcert
在hiera中引用变量,需要去掉“$”,即::clientcert
注意:不要在局部作用域中创建变量,尽量从全局,node级别,facts,内嵌变量或者ENC引入变量。
变量的赋值:
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
4.Hiera完整例子如下:
https://docs.puppetlabs.com/hiera/1/complete_example.html
以ntp这儿默认的modules为例:
该modules中包ntp这个类,需要五个参数:
- servers (ntp server信息)
- restrict(能否成为ntp server)
- autoupdate(自动更新ntp包)
- enable
- template
4.1原始puppet配置如下:
/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,
}
}
4.2采用hiera方式
4.2.1配置hiera文件层次结构
vi /etc/puppet/hiera.yaml
---
:backends: ##源数据文件格式为yaml
- yaml
:yaml: ##yaml源文件存放路径
:datadir: /etc/puppet/hieradata
:hierarchy: ##源文件查询顺序
- "node/%{::fqdn}" ##此处的fqdn也可以换为certname,他们通常是一致的
- common
源文件查找顺序:
(1)在node目录下,有一系列以fqdn命名的yaml文件(E.g. /etc/puppet/hieradata/node/grover.example.com.yaml)。如果没有在node目录下找到匹配的yaml文件,将进入下一步common的查找。
(2)在common文件中(/etc/puppet/hieradata/common.yaml),将定义一些默认设置。
4.2.2配置软连接
ln -s /etc/puppet/hiera.yaml /etc/hiera.yaml
4.2.3编写数据源
(1)在/etc/puppet/hieradata/node中写入我们需要的节点配置。
例如,我们需要设置两个ntp server,即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:
vi /etc/puppet/hieradata/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
(2)在定义default的数据源
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
4.2.4配置puppet的site.pp
vi /etc/puppet/site.pp
node "kermit.example.com", "grover.example.com", "snuffie.example.com" {
include ntp
# or:
# class { "ntp": }
}
4.2.4使用hierad类导入node
(1)在site.pp中声明类
hiera_include('classes')
(2)定义数据源文件yaml
vi /etc/puppet/hieradata/node/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"]
5.使用factor动态使用类
(1)配置hiera主文件
vi /etc/puppet/hiera.yaml
---
: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)配置数据源
需要将vmwaretools装入redhat的/opt/vmware目录下,在ubuntu的/usr/local/vmware目录下
在puppet-vmwaretools模块中需要两个参数working_dir和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 |
|
|