共计 3261 个字符,预计需要花费 9 分钟才能阅读完成。
接上文《自动化运维工具 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': domainname => "puppet-1.RedHatxl.cn", # 定义域名 }}完成后目录结构如下:

在 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






