接上文《自动化运维工具Puppet》http://kaliarch.blog.51cto.com/8966921/1973736
利用模块与模版管理agent端资源
一、模块管理
环境安装及相关配置见链接http://kaliarch.blog.51cto.com/8966921/1973736
此次实例为安装配置httpd
1
2
|
mkdir
-p
/etc/puppet/modules/httpd/
{files,manifests,templates} 创建httpd模块文件已经此目录下的相关文件夹
cd
/etc/puppet/modules/httpd/manifests
|
1
2
3
4
5
6
|
vim
install
.pp
#定义安装httpd资源模块
class httpd::
install
{
#定义类为httpd::install
package {
"httpd"
:
ensure => present,
}
}
|
1
2
3
4
5
6
7
8
9
|
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
}
}
|
1
2
3
4
5
6
7
8
|
vim service.pp
#定义启动httpd服务
class httpd::service {
service {
"httpd"
:
ensure =>
true
,
#启动httpd服务
enable
=>
true
,
#设置httpd开机自启动
require => Class[
"httpd::install"
,
"httpd::config"
]
#依赖模块
}
}
|
1
2
3
4
|
vi
init.pp
#总模块
class httpd {
include httpd::
install
,httpd::config,httpd::service
#调用其他模块
}
|
定义修改完成的httpd.conf
1
|
egrep
-
v
"^$|^#"
/etc/httpd/conf/httpd
.conf >
/etc/puppet/modules/httpd/files/httpd
.conf #将原httpd.conf中的以#开头的和空白字符去掉存放到httpd模块的files目录下
|
定义总的puppet-1资源文件
1
2
3
4
|
vim
/etc/puppet/manifests/nodes/site
.pp
node
'puppet-1'
{
include httpd
}
|
此时puppet master已经定义好了puppet-1节点的http服务的安装,修改配置文件,启动,并设置开机自启动,在puppet-1 手工测试
1
|
puppet agent --
test
|
查看http已经安装,配置文件更新完成,启动并设置开机自启动。
一、模版管理
模板应用(添加虚拟主机配置):
文件存放在templates目录中,以*.erb结尾。
在以上模块管理的基础上,使用模版来配置httpd虚拟主机
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
# 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
}
}
|
创建虚拟主机模版
1
2
3
4
5
6
7
|
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
>
|
1
2
3
4
5
6
7
|
#vim /etc/puppet/manifests/site.pp
node puppet-1 {
include httpd
#调用httpd模块
httpd::vhost {
'puppet-1'
:
domainname =>
"puppet-1.redhatxl.cn"
,
#定义域名
}
}
|
完成后目录结构如下:
在puppet-1之上进行手动测试
1
|
puppet agent --
test
|
验证发现httpd已安装完成,完成配置文件虚拟主机配置更新后启动服务,并设置开机自启动。
浏览器访问,次数页面已经更新为vhost配置文件内的index.html
查看dashboard
本文转自 KaliArch 51CTO博客,原文链接:http://blog.51cto.com/kaliarch/1973868,如需转载请自行联系原作者