自动化运维工具Puppet(管理资源)
接上文《自动化运维工具Puppet》,利用模块与模版管理agent端资源
一、模块管理
此次实例为安装配置httpd
mkdir
-p
/etc/puppet/modules/httpd/
{files,manifests,templates} 创建httpd模块文件已经此目录下的相关文件夹
cd
/etc/puppet/modules/httpd/manifests
vim
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
.pp
node
'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.log
CustomLog logs/<%= domainname %>_access.log common
<
/VirtualHost
>
#vim /etc/puppet/manifests/site.pp
node 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 的详细介绍:请点这里
Puppet 的下载地址:请点这里
相关推荐
YJWcsdn 2020-01-17
84453864 2020-01-17
83274251 2012-05-09
zhangwentaohh 2020-06-05
tanzhe0 2020-01-13
小新没有蜡笔 2019-12-11
nicolesmile 2019-12-04
ppppfly 2015-08-20
sxjinmingjie 2019-06-30
winxcoder 2019-06-28
80327065 2019-06-27
xinluke 2019-03-17
mayongzhancsdn 2018-04-26
83317360 2018-05-28
onepiecedn 2013-11-02
88971932 2013-05-23
Justypc 2013-05-23
ballyyang 2012-11-10