为什么用hiera: https://docs.puppet.com/hiera/3.1/#why-hiera
- hierarchy层级体系。可以设置公共属性,也可以覆写!
- “注入”设置 class 中的属性值。
- hiera_include 通过配置来完成site.pp同样的功能,并且比
node
更加强大灵活(数组值可以合并)。
基本概念:
- hiera.yaml 默认配置文件放在 $codedir/hiera.yaml 。 结合puppet使用时可以通过修改 puppet.conf 的 hiera_config 自定义配置的文件。
- hierarchy hierarchy定义好可以简化很多工作量。如需要根据操作系统 %{::osfamily} 进行适配。
- datasource yaml格式介绍。
windows cygwin命令行环境配置
winse@Lenovo-PC ~
$ cat bin/hiera
#!/bin/sh
# default puppetlabs config in c:\Users\winse\Puppetlabs
export HOME=/cygdrive/c/Users/winse
name=`basename $0`
# execute
"C:/Progra~1/Puppet~1/Puppet/bin"/$name.bat "$@"
winse@Lenovo-PC ~
$ cat .bash_profile
...
function hiera_look(){
code_dir=`puppet config print codedir | sed 's/\r//' `
~/bin/hiera -c "$code_dir/hiera.yaml" --debug "$@" ::environment=production
}
HelloWorld
winse@Lenovo-PC /cygdrive/d/esw-shells/puppet/dta/code
$ cat /cygdrive/c/Users/winse/.puppetlabs/etc/puppet/puppet.conf
[main]
codedir = D:/esw-shells/puppet/dta/code
hiera_config = $codedir/hiera.yaml
certname = winse
winse@Lenovo-PC /cygdrive/d/esw-shells/puppet/dta/code
$ cat hiera.yaml
---
:backends:
- yaml
:hierarchy:
- "nodes/%{::trusted.certname}"
- common
:yaml:
:datadir: "D:/esw-shells/puppet/dta/code/environments/%{::environment}/hieradata"
winse@Lenovo-PC /cygdrive/d/esw-shells/puppet/dta/code
$ cat environments/production/hieradata/common.yaml
whoami: winse
winse@Lenovo-PC ~
$ hiera_look whoami
DEBUG: 2016-05-03 11:27:41 +0100: Hiera YAML backend starting
DEBUG: 2016-05-03 11:27:41 +0100: Looking up whoami in YAML backend
DEBUG: 2016-05-03 11:27:41 +0100: Looking for data source common
DEBUG: 2016-05-03 11:27:41 +0100: Found whoami in common
winse
与Puppet结合使用
- https://docs.puppet.com/hiera/3.1/puppet.html
- https://docs.puppet.com/hiera/3.1/puppet.html#assigning-classes-to-nodes-with-hiera-hierainclude
- 单值属性重复优先级:就近原则 https://docs.puppet.com/hiera/3.1/hierarchy.html#example
参考案例
- https://docs.puppet.com/hiera/3.1/complete_example.html
- https://kisspuppet.gitbooks.io/puppet/content/puppet_learning_ext1.html
主要功能
- hiera获取puppet-facts的属性
- puppet读取hiera中的属性
- hiera注入设置puppet-module的属性: 获取到第一个就返回了(类似于hiera),对于strings, arrays, hashes类型 cannot merge values from multiple hierarchy levels; 需要使用来 hiera_array or hiera_hash 代替。
- hiera_include
动手实践
winse@Lenovo-PC /cygdrive/d/esw-shells/puppet/dta/code/environments/production
$ tree .
.
├── hieradata
│ ├── common.yaml
│ └── nodes
│ └── winse.yaml
├── manifests
│ └── site.pp
└── modules
└── helloworld
└── manifests
└── init.pp
$ cat hieradata/common.yaml
whoami: "%{calling_module} - %{calling_class} - %{calling_class_path} - %{::domain}"
$ cat hieradata/nodes/winse.yaml | iconv -f gbk -t utf8
---
classes:
- helloworld::hello
- helloworld::world
# 文件编码需要与环境匹配,windows要GBK的
helloworld::hello::hello: 你好
$ cat modules/helloworld/manifests/init.pp
class helloworld::hello ($hello = "hello"){
notify {$hello :
}
}
class helloworld::world {
notify {hiera('whoami') : # 不推荐在module中使用hiera方法,这里仅为了演示获取calling_module等
}
}
$ cat manifests/site.pp
hiera_include('classes')
$ puppet apply environments/production/manifests/site.pp
Notice: Compiled catalog for winse in environment production in 0.28 seconds
Notice: 你好
Notice: /Stage[main]/Helloworld::Hello/Notify[你好]/message: defined 'message' as '你好'
Notice: helloworld - helloworld::world - helloworld/world - DHCP HOST
Notice: /Stage[main]/Helloworld::World/Notify[helloworld - helloworld::world - helloworld/world - DHCP HOST]/message: defined 'message' as 'helloworld - helloworld::world - helloworld/world - DHCP HOST'
Notice: Applied catalog in 0.02 seconds
# 测试获取hiera变量
$ puppet apply -e "notice(hiera('whoami'))"
Notice: Scope(Class[main]): - - - DHCP HOST
Notice: Compiled catalog for winse in environment production in 0.05 seconds
Notice: Applied catalog in 0.03 seconds
$ puppet apply -e "notice(hiera('classes'))"
Notice: Scope(Class[main]): [helloworld::hello, helloworld::world]
Notice: Compiled catalog for winse in environment production in 0.05 seconds
Notice: Applied catalog in 0.02 seconds
facts
自定义指标
- https://docs.puppet.com/facter/3.1/custom_facts.html
- Example https://docs.puppet.com/facter/3.1/fact_overview.html
- 《Learning Puppet4》
三种方式:
- 文件: yaml/json/txt。推荐放置到 modules/[name]/facts.d 目录下
- 可执行脚本输出键值对。推荐放置到 modules/[name]/facts.d 目录下,可执行脚本!
- ruby。放置到 modules/[name]/lib/facter 。custom facts should go in lib/facter/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
|
–END