阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

在Puppet中用ERB模板来自动配置Nginx虚拟主机

148次阅读
没有评论

共计 3274 个字符,预计需要花费 9 分钟才能阅读完成。

模板文件是在 puppet 模块下面 templates 目录中以”.erb”结尾的文件,puppet 模板主要用于文件,例如各种服务的配置文件,相同的服务,不同的配置就可以考虑使用模板文件,例如 Nginx 和 Apache 的虚拟主机配置就可以考虑采用 ERB 模板,nginx 的安装在这里建议用系统内部自带的 YUM 源来安装或其它第三方 YUM 源来安装,如果是用 Nginx 的官方源来安装 nginx 的话,我们可以查看下 /etc/yum.repos.d/nginx.repo 文件内容,如下所示:

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/CentOS/$releasever/$basearch/
gpgcheck=0
enabled=1

第二种方式就是通过 createrepo 自建自己的 YUM 源,这种方式更加宁活,我们可以在 nginx 官网去下载适合自己的 rpm 包,然后添加进自己的 YUM 源,在自动化运维要求严格的定制环境中,绝大多数运维同学都会选择这种方法。大家通过此种方式安装 nginx 以后会发现,确实比源码安装 Nginx 方便多了,像自动分配了运行 nginx 的用户 nginx:nginx,Nginx 的日志存放会自动保存在 /var/log/nginx 下,其工作目录为 /etc/nginx,这一点跟源码编译安装的 nginx 区别很大,请大家在实验过程也注意甄别。

像 Puppet 其它初级知识点我这里就略过了,我直接贴上文件内容,/etc/puppet 的文件结构如下:

|– auth.conf
|– fileserver.conf
|– manifests
|  |– nodes
|  |  |– client.linuxidc.com.pp
|  |  `– test.linuxidc.com.pp
|  `– site.pp
|– modules
|  `– nginx
|      |– files
|      |– manifests
|      |  `– init.pp
|      `– templates
|          |– nginx.conf.erb
|          `– nginx_vhost.conf.erb
`– puppet.conf

site.pp 的文件内容如下:

1 import “nodes/*.pp”

client.linuxidc.com.pp 的文件内容如下所示:

node ‘client.linuxidc.com’ {
  include nginx
  nginx::vhost {‘client.linuxidc.com’:
  sitedomain => “client.linuxidc.com” ,
  rootdir => “client”,
}
}

test.linuxidc.com.pp 的文件内容如下所示:

node ‘test.linuxidc.com’ {
  include nginx
  nginx::vhost {‘test.linuxidc.com’:
  sitedomain => “test.linuxidc.com” ,
  rootdir => “test”,
}
}

 

/etc/puppet/modules/nginx/manifests/init.pp

文件内容如下所示:

class nginx{
        package{“nginx”:
        ensure          =>present,
}
        service{“nginx”:
        ensure          =>running,
        require        =>Package[“nginx”],
}
file{“nginx.conf”:
ensure => present,
mode => 644,owner => root,group => root,
path => “/etc/nginx/nginx.conf”,
content=> template(“nginx/nginx.conf.erb”),
require=> Package[“nginx”],
}
}
define nginx::vhost($sitedomain,$rootdir) {
    file{“/etc/nginx/conf.d/${sitedomain}.conf”:
        content => template(“nginx/nginx_vhost.conf.erb”),
        require => Package[“nginx”],
    }
}

/etc/puppet/modules/nginx/templates/nginx.conf.erb

文件内容如下所示:

user  nginx;
worker_processes  8;
error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;
events {
    use epoll;
    worker_connections  51200;
}
http {
    include      /etc/nginx/mime.types;
    default_type  application/octet-stream;
    log_format  main  ‘$remote_addr – $remote_user [$time_local] “$request” ‘
                      ‘$status $body_bytes_sent “$http_referer” ‘
                      ‘”$http_user_agent” “$http_x_forwarded_for”‘;
    access_log  /var/log/nginx/access.log  main;
    sendfile        on;
    #tcp_nopush    on;
    keepalive_timeout  65;
    #gzip  on;
    include /etc/nginx/conf.d/*.conf;
}

/etc/puppet/modules/nginx/templates/nginx_vhost.conf.erb

文件内容如下所示:

server {
    listen      80;
server_name  <%= sitedomain %>;
access_log /var/log/nginx/<%= sitedomain %>.access.log;
location / {
root /var/www/<%= rootdir %>;
index    index.php index.html index.htm;
}
}

最后我们可以在节点名为 client.linuxidc.com 和 test.linuxidc.com 的机器验证效果,命令如下所示:

puppetd –test –server server.linuxidc.com

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 的详细介绍 :请点这里
Puppet 的下载地址 :请点这里

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-20发表,共计3274字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中