puppet之模板和类

puppet之模板和类

不同节点布置资源

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

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

993421-20160722104058138-1385619026.png

编写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)文件。

转载于:https://www.cnblogs.com/hanfei-1005/p/5694313.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值