注意:基本配置环境同上篇
1. 编写Nginx编译所需依赖包安装配置文件:
为了统一管理所有服务安装的pre依赖包,这里新建了一个pkg目录
[root@rhel65-lockey1 ~]# mkdir /srv/salt/pkg
[root@rhel65-lockey1 nginx]# cat /srv/salt/pkg/nginx-pre.sls
pkg-init:
pkg.installed:
- pkgs:
- gcc-c++
- zlib-devel
- openssl-devel
- pcre-devel
2. 编写Nginx编译安装正式配置文件:
[root@rhel65-lockey1 ~]# mkdir /srv/salt/nginx
[root@rhel65-lockey1 nginx]# cat /srv/salt/nginx/install.sls
include:#将依赖安装脚本包含进来
- pkg.nginx-pre
nginx-source-install:
file.managed:#配置源码包文件管理
- name: /mnt/nginx-1.12.1.tar.gz
- source: salt://nginx/files/nginx-1.12.1.tar.gz
cmd.run:#编写源码安装执行的命令
- name: cd /mnt && tar zxf nginx-1.12.1.tar.gz && cd nginx-1.12.1 && sed -i.bak 's/#define NGINX_VER "nginx\/" NGINX_VERSION/#define NGINX_VER "nginx"/g' src/core/nginx.h && sed -i.bak 's/CFLAGS="$CFLAGS -g"/#CFLAGS="$CFLAGS -g"/g' auto/cc/gcc && ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module && make && make install && ln -s /usr/local/nginx/sbin/nginx /usr/sbin/nginx
- creates: /usr/local/nginx#安装检测
测试一下脚本是否正常
[root@rhel65-lockey1 nginx]# salt rhel65-lockey2 state.sls nginx.install test=true
rhel65-lockey2:
----------
ID: pkg-init
Function: pkg.installed
Result: True
Comment: All specified packages are already installed
Started: 15:00:14.168522
Duration: 640.02 ms
Changes:
----------
ID: nginx-source-install
Function: file.managed
Name: /mnt/nginx-1.12.1.tar.gz
Result: None
Comment: The file /mnt/nginx-1.12.1.tar.gz is set to be changed
Started: 15:00:14.811412
Duration: 29.168 ms
Changes:
----------
ID: nginx-source-install
Function: cmd.run
Name: cd /mnt && tar zxf nginx-1.12.1.tar.gz && cd nginx-1.12.1 && sed -i.bak 's/#define NGINX_VER "nginx\/" NGINX_VERSION/#define NGINX_VER "nginx"/g' src/core/nginx.h && sed -i.bak 's/CFLAGS="$CFLAGS -g"/#CFLAGS="$CFLAGS -g"/g' auto/cc/gcc && ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module && make && make install && ln -s /usr/local/nginx/sbin/nginx /usr/sbin/nginx
Result: True
Comment: /usr/local/nginx exists
Started: 15:00:14.841462
Duration: 0.435 ms
Changes:
Summary for rhel65-lockey2
------------
Succeeded: 3 (unchanged=1)
Failed: 0
------------
Total states run: 3
Total run time: 669.623 ms
如果测试成功则执行自动安装
[root@rhel65-lockey1 nginx]# salt rhel65-lockey2 state.sls nginx.install
3. Nginx配置文件推送以及启动项的修改
服务运行用户添加配置文件
[root@rhel65-lockey1 nginx]# cat /srv/salt/user/nginx.sls
nginx:
user.present:
- uid: 800
-