使用 puppet 安装 nginx 一例

发布时间:2020-01-16编辑:脚本学堂
使用 puppet 安装 nginx 一例

使用 puppet 安装 nginx 一例,供大家学习参考。

首先,看看所有需要用到的东东。
cd /etc/puppet/modules/
[root@puppet nginx]# tree
.
|-- files
|   |-- nginx
|   |-- nginx-0.8.55.tar.gz
|   `-- nginx_install.sh
|-- manifests
|   |-- after.pp
|   |-- init.pp
|   |-- install.pp
|   |-- nginxfile.pp
|   `-- service.pp
`-- templates

3 directories, 8 files

安装过程

复制代码 代码如下:

mkdir  -p nginx/{manifests,templates,files}

cd  /etc/puppet/modules/nginx/files
wget http://nginx.org/download/nginx-0.8.55.tar.gz

[root@puppet manifests]# cat init.pp
 

复制代码 代码如下:
class nginx {
      include nginx::after,nginx::nginxfile,nginx::install,nginx::service
}

[root@puppet manifests]# cat after.pp
 

复制代码 代码如下:
class nginx::after {
      package { "pcre-devel":
               ensure => 'present',
               before => Class["nginx::nginxfile"],
}
}
 

[root@puppet manifests]# cat nginxfile.pp
 

复制代码 代码如下:

class nginx::nginxfile {
               file { "/data/soft/nginx-0.8.55.tar.gz":
               source => 'puppet://puppet.cctvcjw.com/modules/nginx/nginx-0.8.55.tar.gz',
               mode => 0777,
               ensure => 'present',
             
}

        file { "/data/soft/nginx_install.sh":
               source => 'puppet://puppet.cctvcjw.com/modules/nginx/nginx_install.sh',
               mode => 0777,
               ensure => 'present',

}
        file { "/etc/rc.d/init.d/nginx":
               source => 'puppet://puppet.cctvcjw.com/modules/nginx/nginx',
               mode => 0777,
               ensure => 'present',
               before => Class["nginx::install"],
}

}

[root@puppet manifests]# cat install.pp
 

复制代码 代码如下:
class  nginx::install {
       exec { "/bin/bash /data/soft/nginx_install.sh":
              path=>'/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin',
              cwd =>'/data/soft',
               require =>Class["nginx::nginxfile"],
              before => Class["nginx::service"],
}
 
}

[root@puppet manifests]# cat service.pp
 

复制代码 代码如下:
class nginx::service {
         service { "nginx":
               ensure =>'running',
               enable => 'true',
               name => 'nginx',
               hasrestart => 'true',
               hasstatus =>'false',
               require => Class["nginx::install"],
 }
}

[root@puppet manifests]# cat  ../files/nginx_install.sh
 

复制代码 代码如下:

#!/bin/bash
#cd /data/soft
tar xvzf nginx-0.8.55.tar.gz
cd nginx-0.8.55
./configure --prefix=/usr/local/nginx/   --with-http_ssl_module  --with-http_flv_module --with-http_stub_status_module --with-http_gzip_static_module

make
make install

[root@puppet manifests]# cat  ../files/nginx
 

复制代码 代码如下:

#!/bin/bash
# chkconfig: 35 85 15
# description: nginx service

start(){
 if [  -f  "/usr/local/nginx/logs/nginx.pid" ]
     then
         echo 'nginx service status is start'
     else
         /usr/local/nginx/sbin/nginx 
fi
}

stop(){
  if [    -f  "/usr/local/nginx/logs/nginx.pid"  ]
          then
            num=`cat  /usr/local/nginx/logs/nginx.pid`
           kill $num
     else
          echo 'nginx service status is stop'
    fi
}

restart(){
    if [    -f  "/usr/local/nginx/logs/nginx.pid"  ]
          then
            num=`cat  /usr/local/nginx/logs/nginx.pid`
           kill $num
            /usr/local/nginx/sbin/nginx
     else
            /usr/local/nginx/sbin/nginx
    fi
}

case "$1" in
start)
  start
;;
stop)
stop
;;
restart)
restart
;;
*)
echo 'usage service nginx {restart|stop|start}'
esac