lbdbzj110 发表于 2018-8-2 09:34:39

Puppet 通过基础模块、类、节点正则表达式批量管理Apache服务器

=> 创建httpd基础模块  
# mkdir /etc/puppet/modules/httpd/{files,manifests,templates} -pv
  
# tree /etc/puppet/modules/httpd/
  
/etc/puppet/modules/httpd/
  
├── files//基础模块所调用的配置文件,agent可以通过puppet协议将files目录所定义的文件下载到本地。
  
├── manifests   //主要存放基础模块所使用的类文件及相关资源,如init.pp文件。
  
└── templates//主要存放定义的模板文件,例如虚拟主机的定义等等。
  

  
3 directories, 0 files
  

  
=> 存放测试页面或者存放http的配置文件,这里一测试页面为例进行演示。
  
# ls /etc/puppet/modules/httpd/files/
  
index.html
  

  
=> 定义各资源清单,如package、services、file、init等资源。
  
   manifests主要存放管理代码以及site.pp入口,并根据init.pp定义进行调用资源清单。
  
# cd /etc/puppet/modules/httpd/manifests
  

  
=> 定义package资源
  
# cat install.pp
  
class httpd::install {
  
package {'httpd':
  
ensure => installed,
  
allow_virtual => false,
  
require => Class,
  
}
  
}
  

  
=> 定义file资源,主要用于定义agent获取资源的方式以及属性信息
  
# cat file.pp
  
class httpd::file {
  
file {"/var/www/html/index.html":
  
ensure => file,
  
owner => root,
  
group => root,
  
source => "puppet:///modules/httpd/index.html"
  
}
  
}
  

  
=> 定义service资源
  
# cat service.pp
  
class httpd::service {
  
service {"httpd":
  
ensure => running,
  
enable => true,
  
}
  
}
  

  

  
=> 定义init.pp资源主要用于调用manifests所定义的类资源
  
# cat manifests/init.pp
  
class httpd {
  
include httpd::install    调用install资源清单
  
include httpd::service    调用service资源清单
  
include httpd::file    调用文件资源清单
  
}
  

  
=> 通过正则表达式管理节点
  
   主要用于判断agent所匹配的信息,满足执行相关资源定义,不满足则匹配默认(default)所定义的资源。
  
# cat /etc/puppet/manifests/site.pp
  
node /^(agent|zabbix)\.gdy\.com$/ {//只有满足主机名为agent.gdy.com和zabbix.gdy.com两台主机,才会加载httpd基础模块,其他则加载default所定义的资源。
  
include httpd
  
}
  
node default {   ==>当agent不满足需求时,则执行通知机制。
  
notify {"Notice error,Not match your node,this is default node":}
  
}
  

  
=> 通过基础模块的形式部署Apache基本完成,下面我们一起看下测试结果,是否正常安装Apache呢?
  
注意:
  
其他相关组件的安装和定义类似,例如(LNMP/LAMP/tomcat...),大家只需定义相关资源即可。
  
================
  

  
=> agent端测试:
  
# hostname
  
zabbix.gdy.com
  
# rpm -qa | grep httpd
  
httpd-tools-2.2.15-29.el6_4.x86_64
  
# puppet agent -t
  
Info: Retrieving pluginfacts
  
Info: Retrieving plugin
  
Info: Caching catalog for zabbix.gdy.com
  
Info: Applying configuration version '1435386756'
  
Notice: /Stage/Httpd::Install/Package/ensure: created
  
Notice: /Stage/Httpd::Service/Service/ensure: ensure changed 'stopped' to 'running'
  
Info: /Stage/Httpd::Service/Service: Unscheduling refresh on Service
  
Notice: /Stage/Httpd::File/File/ensure: defined content as '{md5}d72717e69f29438790d728bdcad27913'
  
Notice: Finished catalog run in 9.86 seconds
  
# rpm -qa | grep httpd
  
httpd-tools-2.2.15-29.el6_4.x86_64
  
httpd-2.2.15-29.el6_4.x86_64
  
# service httpd status
  
httpd (pid1298) is running...
  
# chkconfig --list httpd
  
httpd          0:off1:off2:on3:on4:on5:on6:off
  
#
  
//当前系统主机名为zabbix.gdy.com,满足我们所定义的主机,所以会加载httpd基础模块下的资源。
  

  
===============================================
  
# hostname
  
test01.gdy.com         => 当前主机名为test01.gdy.com,是不满足我们的需求,则会执行default所定义的通知机制。
  
# puppet agent -t
  
Info: Retrieving pluginfacts
  
Info: Retrieving plugin
  
Info: Caching catalog for test01.gdy.com
  
Info: Applying configuration version '1436147237'
  
Notice: Notice error,Not match your node,this is default node
  
Notice: /Stage/Main/Node/Notify/message: defined 'message' as 'Notice error,Not match your node,this is default node'
  
Notice: Finished catalog run in 0.05 seconds
  
//当前系统主机名为test01.gdy.com,不满足所定义的主机资源,故执行默认所定义的default资源。
  

  
=> ok,今天就先到这,谢谢大家
页: [1]
查看完整版本: Puppet 通过基础模块、类、节点正则表达式批量管理Apache服务器