接上文《自动化运维工具Puppet》,利用模块与模版管理agent端资源
一、模块管理
环境安装及相关配置见这里。 http://www.linuxidc.com/Linux/2017-12/149672.htm
此次实例为安装配置httpd
mkdir -p /etc/puppet/modules/httpd/{files,manifests,templates} 创建httpd模块文件已经此目录下的相关文件夹cd /etc/puppet/modules/httpd/manifestsvim install.pp #定义安装httpd资源模块class httpd::install { #定义类为httpd::install package {"httpd": ensure => present, }}vim config.pp #定义配置文件资源class httpd::config { file {"/etc/httpd/conf/httpd.conf": ensure => present, source => "puppet:///modules/httpd/httpd.conf", #实际路径在/etc/puppet/modules/httpd/files/httpd.conf require => Class["httpd::install"], #依赖与httpd::install notify => Class["httpd::service"], #变更后通知httpd::service }}vim service.pp #定义启动httpd服务class httpd::service { service {"httpd": ensure => true, #启动httpd服务 enable => true, #设置httpd开机自启动 require => Class["httpd::install","httpd::config"] #依赖模块 }}vi init.pp #总模块class httpd { include httpd::install,httpd::config,httpd::service #调用其他模块}定义修改完成的httpd.conf
egrep -v "^$|^#" /etc/httpd/conf/httpd.conf > /etc/puppet/modules/httpd/files/httpd.conf #将原httpd.conf中的以#开头的和空白字符去掉存放到httpd模块的files目录下定义总的puppet-1资源文件
vim /etc/puppet/manifests/nodes/site.ppnode 'puppet-1' { include httpd}此时puppet master已经定义好了puppet-1节点的http服务的安装,修改配置文件,启动,并设置开机自启动,在puppet-1 手工测试
puppet agent --test查看http已经安装,配置文件更新完成,启动并设置开机自启动。
一、模版管理
模板应用(添加虚拟主机配置):
文件存放在templates目录中,以*.erb结尾。
在以上模块管理的基础上,使用模版来配置httpd虚拟主机
# vim /etc/puppet/modules/httpd/manifests/init.pp class httpd { include httpd::install,httpd::config,httpd::service}define httpd::vhost($domainname) {file {"/etc/httpd/conf.d/${domainname}_vhost.conf": content => template("httpd/httpd_vhost.conf.erb"), require => Class["httpd::install"], notify => Class["httpd::service"] }file {"/var/www/$domainname": ensure => directory }file {"/var/www/$domainname/index.html": content => $domainname }}创建虚拟主机模版
vim /etc/puppet/modules/httpd/templates/httpd_vhost.conf.erb<VirtualHost *:80>ServerName <%= domainname %>DocumentRoot /var/www/<%= domainname %>ErrorLog logs/<%= domainname %>_error.logCustomLog logs/<%= domainname %>_access.log common</VirtualHost>#vim /etc/puppet/manifests/site.ppnode puppet-1 { include httpd #调用httpd模块 httpd::vhost {'puppet-1': }}完成后目录结构如下:
在puppet-1之上进行手动测试
puppet agent --test验证发现httpd已安装完成,完成配置文件虚拟主机配置更新后启动服务,并设置开机自启动。
浏览器访问,次数页面已经更新为vhost配置文件内的index.html
查看dashboard
Puppet 学习系列:
Puppet 学习一:安装及简单实例应用 http://www.linuxidc.com/Linux/2013-08/88710.htm
Puppet学习二:简单模块配置和应用 http://www.linuxidc.com/Linux/2013-08/88711.htm
相关阅读:
有关Puppet agent端三种备份恢复方案探讨研究 http://www.linuxidc.com/Linux/2013-07/87885.htm
选择更安全的方式注册你的Puppet节点 http://www.linuxidc.com/Linux/2013-07/87884.htm
通过配置SSH深刻理解Puppet的语法及工作机制 http://www.linuxidc.com/Linux/2013-07/87882.htm
Puppet利用Nginx多端口实现负载均衡 http://www.linuxidc.com/Linux/2013-02/79794.htm
CentOS(5和6)下Puppet的C/S模式实例 http://www.linuxidc.com/Linux/2011-12/50502.htm
自动化运维工具Puppet 快速入门 http://www.linuxidc.com/Linux/2016-12/137893.htm
CentOS 7下安装配置Puppet http://www.linuxidc.com/Linux/2017-02/140932.htm
Puppet 的详细介绍:请点这里
Puppet 的下载地址:请点这里
本文永久更新链接地址:http://www.linuxidc.com/Linux/2017-12/149673.htm

![UEBT{DT0FX]R4TEO(ZBI1~3.png wKiom1nnHBOgizngAABZiw-zN4I390.png](../../upload/2017_12/171221202949625.png)


![PVQ21CIVYOG1G3ZB5B]TRW9.png wKioL1nnGkuAqCtgAAELAWryhK4144.png](../../upload/2017_12/171221202949626.png)



![]GQO11S[U1EZV_KH8WL]FSF.png wKiom1nnIDOyhnsAAAAaKnoy2cs153.png](../../upload/2017_12/171221202949629.png)
