puppet之模板和类

puppet之模板和类

不同节点布置资源

vim /etc/puppet/manifests/site.pp
  1 import "nodes/*.pp"

建立节点文件
mkdir /etc/puppet/manifests/nodes

编写http模块

mkdir -p /etc/puppet/modules/httpd/{files,manifests,templates}
httpd的部署包括下载软件包,配置,开启服务
1)加载httpd模块读取的文件

[root@server0 manifests]# cat init.pp 
class httpd {
	include httpd::install,httpd::config,httpd::service
}

2)安装模块

[root@server0 manifests]# cat install.pp 
class httpd::install {
	package {
		"httpd":
		ensure=>present
	}
}

3)配置部分

[root@server0 manifests]# cat config.pp 
class httpd::config {
	file {
		"/etc/httpd/conf/httpd.conf":
		source=>"puppet:///modules/httpd/httpd.conf",
		require=>Class["httpd::install"],
		notify=>Class["httpd::service"]
	}
}

4服务管理:

[root@server0 manifests]# cat service.pp 
class httpd::service {
	service {
		"httpd":
		ensure=>running
	}
}

让server4执行此模块:

vim /etc/puppet/manifests/nodes/server3.pp 
  1 node 'server4.example.com' {
  2         include httpd
  3 }

模板应用,稍后详解:
虚拟主机配置:文件存放在templates目录中加,以*.erb结尾

vim /etc/puppet/modules/httpd/templates/vhost.erb
  1 <VirtualHost *:80>
  2 ServerName <%= domainname %>
  3 DocumentRoot /var/www/<%= domainname %>
  4 ErrorLog logs/<%= domainname %>_error.log
  5 CustomLog logs/<%= domainname %>_access.log common
  6 </VirtualHost>

注意上传的配置文件:

vim /etc/puppet/modules/httpd/files/httpd.conf
 136 Listen 80
 990 NameVirtualHost *:80  ##使用虚拟主机所要打开的参数
vim /etc/puppet/modules/httpd/manifests/init.pp
  1 class httpd {
  2         include httpd::install,httpd::config,httpd::service
  3 }
  4 define httpd::vhost($domainname) {
  5         file {
  6                 "/etc/httpd/conf.d/${domainname}_vhost.conf":
  7                 content => template("httpd/vhost.erb"),
  8                 require => Class["httpd::install"],
  9                 notify => Class["httpd::service"]
 10         }
 11         file {
 12                 "/var/www/$domainname":
 13                 ensure => directory
 14         }
 15         file {
 16                 "/var/www/$domainname/index.html":
 17                 content => $domainname
 18         }
 19 }

将模块添加到server3节点上:

 vim /etc/puppet/manifests/nodes/server3.pp 
  1 node 'server3.example.com' {
  2         include httpd
  3         httpd::vhost {
  4                 'server3.example.com':
  5                 domainname => "server3.example.com"
  6         }
  7         httpd::vhost {
  8                 'www.example.com':
  9                 domainname => "www.example.com"
 10         }
 11 }

模板解析:
Erb模板语法:
<%= %> 直接替换成表达式
<% %> 包括条件循环
<%% or %%> 等同于<% or %>分别进行
创佳一个vhost.conf.erb模板文件,模板文件中定义变量为$port

[root@server0 templates]# cat vhost.conf.erb 
server {
	listen $port;
	server_name -;
	location /nginx_status {
		stub_status on;
		access_log off;
	}

}

在声明这个模板的时候,需要在资源中使用content,

[root@server0 manifests]# cat vhost.pp 
define nginx::vhost ($port) {
	file {
		'vhost.conf':
		path=>'/etc/nginx/config/vhosts/vhost.conf',
		ensure=>file,
		require=>Package['nginx'],
		content=>template('nginx/vhost.conf.erb')

	}

}

Puppet会查找nginx模块下template目录中的vhost.conf.erb(/etc/puppet/modules/nginx/templates/vhost.conf.erb)文件。

posted @ 2016-07-22 10:42  夏日花开  阅读(444)  评论(0编辑  收藏  举报