一、在Master上配置一个puppet测试节点的管理信息
本文根据《Puppet in action》内容进行实践和测试后,经整理形成了记录,以供日后参考。
Master节点主机名为mytest1
Master节点主机名为mytest1
Client节点主机名为mytest2
在主节点上执行:
- mkdir -p /etc/puppet/modules/test/{manifests,templates,files} --创建必要的目录,manifests中存放模块的主配置文件init.pp。如果使用到了templates函数,则会读取templates目录中的ERB模块文件。
- # vim /etc/puppet/modules/test/manifests/init.pp
class test {
file { "/tmp/$hostname.txt": content => "Hello World!"; }
} ---应用类文件 - ERB模块文件 模块文件名与类名保持一致。在模块配置文件中使用的主机名变量,需要将该变量传给ERB模块文件,主机名变量值通过facter命令获取,并以变量$hostname的形式传递给mytest2.pp。
#vim /etc/puppet/modules/test/templates/test.erb
hostname <%= fqdn %> - 创建测试节点
# vim /etc/puppet/manifests/nodes/mytest2.pp
node 'mytest2' {
include test
} - 将测试节点载入到Puppet
# vim /etc/puppet/manifests/site.pp
import "nodes/mytest2.pp" - 在服务端检测语法
# puppet parser validate /etc/puppet/modules/test/manifests/init.pp
在客户端节点上执行:
- 在客户端检测语法及代码,noop的意思是验证配置而不执行
# puppet agent --test --server mytest1 --noop - 在客户端,去除noop参数后,即可执行命令和代码
二、配置一个puppet客户端节点,以自定义yum源方式安装httpd模块
- 创建httpd模块相应的目录
# mkdir -p /etc/puppet/modules/httpd/{manifests,templates,files} - 编辑httpd模块文件,指定源配置信息。在httpd模块的init.pp配置文件中定义了两个资源,yumrepo和package,第二个资源package使用了元参数require来声明资源之间的依赖关系,即Yumrepo["repo163"]资源会先于它执行。
# vim /etc/puppet/modules/httpd/manifests/init.pp
class httpd {
yumrepo { "repo163":
descr => "163 repo",
baseurl => "http://mirrors.163.com/centos/6/os/x86_64/",
gpgcheck => "0",
enabled => "1";
}
package {
"httpd":
ensure => installed,
require => Yumrepo["repo163"];
}
} - 修改mytest2.pp,增加httpd模块
# vim /etc/puppet/manifests/nodes/mytest2.pp
node 'mytest2' {
#load test class
include test
include httpd
} - 在服务端检测语法,在客户端检测语法后执行代码
# puppet parser validate /etc/puppet/modules/httpd/manifests/init.pp - 在客户端上执行配置
# puppet agent --test --server mytest1 --noop 语法检测
# puppet agent --test --server mytest1