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版本,需要执行下面的命令.
yum install hiera hiera-puppet -y
b)配置hiera.yaml
查看配置文件的路径
puppet master --configprint hiera_config #默认在/etc/puppet/hiera.yaml
hiera的配置文件必须是YAML hash格式.
#一个参考的配置..,注意每个最顶层的配置,前面必须要有一个 :
---
: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: consolec) 配置参数
#顶层配置
: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 Master3. 执行Puppet Agent
[root@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 [root@agent1 ~]# facter operatingsystem CentOS [root@agent1 ~]# facter operatingsystemmajrelease 6
本文出自 “晓风残月” 博客,请务必保留此出处http://kinda22.blog.51cto.com/2969503/1586495
【跟我学Puppet】1.5 Puppet 3.7 使用Hiera定义配置
原文地址:http://kinda22.blog.51cto.com/2969503/1586495