1. 环境准备 OS:CentOS 6.4 关闭selinux和iptables
部署Puppet:1.0 Puppet 3.7部署 安装Puppet源:http://yum.puppetlabs.com/puppetlabs-release-el-6.noarch.rpm
完成PuppetMaster/Agent的部署,证书签署... Hiera是一个key/value配置数据的查找工具,可以用来方便的配置特殊节点配置数据.
2. 使用Hiera a) 安装hiera 如果安装的是Puppet 3.7不需要再安装Hiera了..
如果安装的是2.7版本,需要执行下面的命令.
1
| yum install hiera hiera-puppet -y
|
b)配置hiera.yaml
查看配置文件的路径
1
| puppet master --configprint hiera_config #默认在/etc/puppet/hiera.yaml
|
hiera的配置文件必须是YAML hash格式.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
| #一个参考的配置..,注意每个最顶层的配置,前面必须要有一个 :
---
:backends:
- yaml
- json
:yaml:
:datadir: /etc/puppet/hieradata
:json:
:datadir: /etc/puppet/hieradata
:hierarchy:
- "%{::clientcert}"
- "%{::custom_location}"
- common
#如果hiera.yaml存在,但是里面是空的..,那么它的默认配置如下..
---
:backends: yaml
:yaml:
:datadir: /var/lib/hiera:hierarchy: common
:logger: console
|
c) 配置参数
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
| #顶层配置
:hierarchy
#必须是一个字符串或数组,每一个名字都代表一个静态或动态数据源
#动态数据是一个简单的 %{variable} 内插变量
#在层次结构中,按顺序检查,自上而下.
#默认值是common
:backend
#必须是一个字符串或数组,每一个名字都代表一个可用的Hiera的backend
#内置的backend是yaml和json,可以添加额外的backend作为附加的
#默认值是yaml
#backend设置
:yaml and :json
:datadir
#可以在目录中找到数据源的文件,必须是一个字符串
#可以在路径中使用变量内插/etc/puppet/hieradata/%{::environment}
#默认值 /var/lib/hiera
#配置hiera.yaml
mkdir /etc/puppet/hieradata/
cat > /etc/puppet/hiera.yaml <<eof
---
:backends:
- yaml
:yaml:
:datadir: /etc/puppet/hieradata
:logger: console
:hierarchy:
- "%{operatingsystem}%{::operatingsystemmajrelease}"
- "%{::osfamily}"
- common
EOF
#创建默认会被匹配的数据文件..,使用classes引入base模块
cat > /etc/puppet/hieradata/common.yaml <<eof
---
classes:
- base
EOF
#创建当osfamily被匹配为RedHat时的文件...
cat > /etc/puppet/hieradata/RedHat.yaml <<eof
---
os: redhat
EOF
#创建当operatingsystem和operatingsystemmajrelease被匹配时的文件..
cat > /etc/puppet/hieradata/CentOS6.yaml <<eof
---
os: CentOS
os_type:
Linux: true
unix: false
EOF
#配置site.pp自动导入类模块...
cat > /etc/puppet/manifests/site.pp <<eof
hiera_include('classes')
EOF
#创建一个base模块,打印出os变量
#对于查找数据可以使用三种函数
#标准查询hiera 获取一个匹配key的value
#合并查询hiera_array 获取所有匹配的键,然后返回它们的值到一个数组中
#合并查询hiera_hash 类似数组,返回hash的结构.
mkdir /etc/puppet/modules/base/manifests
cat > /etc/puppet/modules/base/manifests/init.pp <<eof
class base {
$test1 = hiera('os')
$test2 = hiera_array('os')
$test3 = hiera_hash('os_type')
notify { "OS1: $test1":}
notify { "OS2: $test2":}
notify { "OS3: $test3['linux'] $test3['unix']":}
}
EOF
#重启Puppet Master
|
3. 执行Puppet Agent
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
| [iyunv@agent1 ~]# puppet agent --verbose --no-daemonize
Notice: Starting Puppet client version 3.7.3
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Caching catalog for agent1.dbsa.cn
Info: Applying configuration version '1417636182'
Notice: OS3: Linuxtrueunixfalse['linux'] Linuxtrueunixfalse['unix']
Notice: /Stage[main]/Base/Notify[OS3: Linuxtrueunixfalse['linux'] Linuxtrueunixfalse['unix']]/message: defined 'message' as 'OS3: Linuxtrueunixfalse['linux'] Linuxtrueunixfalse['unix']'
Notice: OS1: CentOS
Notice: /Stage[main]/Base/Notify[OS1: CentOS]/message: defined 'message' as 'OS1: CentOS'
Notice: OS2: CentOSredhatdefault
Notice: /Stage[main]/Base/Notify[OS2: CentOSredhatdefault]/message: defined 'message' as 'OS2: CentOSredhatdefault'
Notice: Finished catalog run in 0.03 seconds
^CNotice: Caught INT; calling stop
[iyunv@agent1 ~]# facter operatingsystem
CentOS
[iyunv@agent1 ~]# facter operatingsystemmajrelease
6
|
</eof
</eof
</eof
</eof
</eof
</eof
|