根据之前http://my.oschina.net/davehe/blog/354626 的简单安装puppet之后,下面先来简单试用一下它.
测试实例1
1.配置服务测试节点,让其客户机根据服务端模块信息生成相应文件.
(1)puppet端创建test模块.模块目录为test,class类名也必须是test.test类里有一个File资源,目的是在/tmp目录中创建一个以agent主机名(这里客户端主机名是agent.domain.com)命名的txt文件,其内容是"Hello puppet"
[email protected]:puppet#mkdir -p /etc/puppet/modules/test/manifests/ [email protected]:puppet#mkdir -p /etc/puppet/modules/test/templates/ [email protected]:puppet#mkdir -p /etc/puppet/modules/test/files [email protected]:manifests# pwd /etc/puppet/modules/test/manifests [email protected]:manifests# cat init.pp class test { file { "/tmp/$hostname.txt": content => "hello world!";} }
(2) 以上文件定义了一个变量"$hostname",需要将该变量传给ERB模块文件.此文件放在test类目录下的templates目录中,文件名与类名保持一致.主机变量值通过facter命令获取,以变量"$hostname"的形式传递给客户端.配置如下:
[email protected]:templates# pwd /etc/puppet/modules/test/templates [email protected]:templates# cat test.erb hostname <%= fqdn %>(3)创建测试节点客户端agent.domain.com.pp
[email protected]:nodes# pwd /etc/puppet/manifests/nodes [email protected]:nodes# cat agent.domain.com.pp node '10.1.1.34.domain.com' { include test }(4)将测试节点载入到puppet,修改site.pp配置文件,puppet会首先读取site.pp.
[email protected]:manifests# pwd /etc/puppet/manifests [email protected]:manifests# cat site.pp #import "nodes/agent.domain.com.pp" import "nodes/*.pp"
2.检测配置文件
(1)puppet master服务端检查
[email protected]:manifests# puppet parser validate /etc/puppet/modules/test/manifests/init.pp
还可以通过find命令查找当前目录下以pp结尾的文件.通过管道传给xargs命令. [email protected]:modules# find -name '*.pp' | xargs -n 1 -t puppet parser validate puppet parser validate ./httpd/manifests/init.pp puppet parser validate ./tcpdump/manifests/init.pp puppet parser validate ./test/manifests/init.pp(2)puppet agent客户端检查
[email protected]:tmp# puppet agent --test --server puppet.domain.com --noop Info: Retrieving pluginfacts Info: Retrieving plugin Info: Caching catalog for agent.domain.com Info: Applying configuration version '1418227527' Notice: /Stage[main]/Test/File[/tmp/agent.txt]/ensure: current_value absent, should be file (noop) Notice: Class[Test]: Would have triggered 'refresh' from 1 events Notice: Stage[main]: Would have triggered 'refresh' from 1 events Notice: Finished catalog run in 0.05 seconds
由于使用noop模式,代码没有被执行.
3.客户端运行配置
[email protected]:tmp# puppet agent --test --server puppet.domain.com --noop Info: Retrieving pluginfacts Info: Retrieving plugin Info: Caching catalog for agent.domain.com Info: Applying configuration version '1418227527' Notice: /Stage[main]/Test/File[/tmp/agent.txt]/ensure: current_value absent, should be file (noop) Notice: Class[Test]: Would have triggered 'refresh' from 1 events Notice: Stage[main]: Would have triggered 'refresh' from 1 events Notice: Finished catalog run in 0.05 seconds [email protected]:tmp# puppet agent --test --server puppet.domain.com Info: Retrieving pluginfacts Info: Retrieving plugin Info: Caching catalog for agent.domain.com Info: Applying configuration version '1418227527' Notice: /Stage[main]/Test/File[/tmp/agent.txt]/ensure: defined content as '{md5}fc3ff98e8c6a0d3087d515c0473f8677' Notice: Finished catalog run in 0.08 seconds [email protected]:tmp# cat /tmp/agent.txt hello world!以上可知agent缓存了主机信息,资源被应用,/tmp/agent.txt文件被创建,并根据文件内容计算出MD5值.最后得出完成时间.
测试实例2: 安装tcpdump模块
1.创建tcpdump模块相应的目录:
[email protected]:nodes# mkdir -p /etc/puppet/modules/tcpdump/{manifests,templates,files}2.编辑tcpdump模块
[email protected]:manifests# cat init.pp class tcpdump { yumrepo {"davehe_repo163": descr => "163 davehe repo", baseurl => "http://mirrors.aliyun.com/centos/6.5/os/x86_64/", gpgcheck => "0", enabled => "1"; } package { "tcpdump": ensure => installed, allow_virtual => true, require => Yumrepo["davehe_repo163"]; } }3.修改/etc/puppet/manifests/nodes/agent.domain.com.pp
[email protected]:nodes# cat agent.domain.com.pp node 'agent.domain.com' { include tcpdump }4.客户端运行配置并查看该包是否安装上.
[email protected]:tmp# rpm -qa | grep tcpdump [email protected]:tmp# puppet agent --test Info: Retrieving pluginfacts Info: Retrieving plugin Info: Caching catalog for agent.domain.com Info: Applying configuration version '1418355920' Notice: /Stage[main]/Tcpdump/Package[tcpdump]/ensure: created Notice: Finished catalog run in 3.71 seconds [email protected]:tmp# rpm -qa | grep tcpdump tcpdump-4.0.0-3.20090921gitdf3cb4.2.el6.x86_64