Puppet部署Nginx返代示例

时间:2022-07-13 23:44:30

一、创建目录并编辑Nginx安装模块

mkdir -pv /etc/puppet/modules/nginx/{manifests,files,templates,spec,tests,lib}
]# vim nginx/manifests/init.pp
class nginx {
package{'nginx':
ensure => latest
} ->
service{'nginx':
ensure => running,
enable => true
}
}

二、编辑Nginx配置文件

]# vim nginx/manifests/web.pp
class nginx::web($port=) inherits nginx {
file{'web.conf':
path => '/etc/nginx/conf.d/web.conf',
content => template('nginx/web.conf.erb')
}
file{'/ngxdata/html':
ensure => directory
}
file{'index.html':
ensure => file,
path => '/ngxdata/html/index.html',
source => 'puppet:///modules/nginx/index.html',
require => File['/ngxdata/html']
}
Service['nginx'] {
subscribe => File['web.conf']
}
}

三、编辑反向代理配置

]# vim nginx/manifests/proxy.pp
class nginx::proxy($proxy_port=) inherits nginx {
file{'proxy.conf':
path => '/etc/nginx/conf.d/proxy.conf',
content => template('nginx/proxy.conf.erb'),
}
Service['nginx'] {
subscribe => File['proxy.conf']
}
}

四、配置模板与页面测试文件

]# vim nginx/templates/web.conf.erb
server {
listen <%= @port %>;
server_name <%= @fqdn %>;
location /
root /ngxdata/html;
}
}
]# vim nginx/files/index.html
<h1> Nginx ok </h1>
]# vim nginx/templates/proxy.conf.erb
server {
listen <%= @proxy_port %>;
server_name <%= @fqdn %>;
location / {
proxy_pass http://xxx.xxx.xxx:8080/;
}
}

五、应用

]# puppet apply -v -e 'include nginx::proxy'

查看模板是否成效:

]# cat /etc/nginx/conf.d/proxy.conf
server {
listen ;
server_name node1.danran.com;
location / {
proxy_pass http://xxx.xxx.xxx:8080/;
}
}