使用saltstack来源码安装nginx

使用saltstack来源码安装nginx

使用saltstack来源码安装nginx

使用saltstack来源码安装nginx

环境:

salt_master: 192.168.100.228
salt_client1: 192.168.100.245

结构:

[root@salt_server base]# pwd
/srv/salt/base
[root@salt_server base]#
[root@salt_server base]# cat top.sls 
base:
  'salt_client*':
      - nginx
[root@salt_server base]# 
[root@salt_server base]# tree nginx
nginx
├── conf.sls
├── files
│   ├── nginx
│   ├── nginx-1.7.3.tar.gz
│   ├── nginx.conf
│   └── vhost.conf
├── init.sls
├── install.sls
└── vhost.sls

1 directory, 9 files
[root@salt_server base]# 

首先先查看init.sls,其中需要注意的是 include 包含了install,conf,vhost的sls,切记一定要注意顺序问题 [root@salt_server nginx]# cat init.sls include: - nginx.install - nginx.conf - nginx.vhost [root@salt_server nginx]#

查看安装文件,需要注意的是salt的sls是采用YAML格式的语法来写的,因此需要注意格式的严谨

[root@salt_server nginx]# cat install.sls 
##install source nginx		###现在nginx包到本地的home目录
nginx_source:
  file.managed:
    - name: /home/nginx-1.7.3.tar.gz
    - unless: test -e /home/nginx-1.7.3.tar.gz
    - source: salt://nginx/files/nginx-1.7.3.tar.gz

### tar nginx source    ##解压nginxtar文件,然后使用unless来检查是否正常,使用require来做依赖,如果nginx_source没有操作如下是不会操作的
extract_nginx:
  cmd.run:
    - cwd: /home
    - names:
        - tar xvf nginx-1.7.3.tar.gz
    - unless: test -d /home/nginx-1.7.3
    - require:
      - file: nginx_source

# adduser for nginx		##创建nginx用户指定相应的UID这个可以随意指定,
nginx_user:
  user.present:
    - name: nginx
    - uid: 1020
    - createhome: False
    - gid_from_name: True
    - shell: /sbin/nologin

#nginx pkg.install	##在安装nginx之前,先解决nginx的依赖关系,如下是安装Nginx的依赖包!
nginx_pkg:
  pkg.installed:
    - pkgs:
      - gcc
      - openssl-devel
      - pcre-devel
      - zlib-devel

#nginx source install	#### 编译安装nginx
nginx_commpile:
  cmd.run:
    - cwd: /home/nginx-1.7.3
    - names:
        - ./configure --prefix=/usr/local/nginx  --user=nginx  --group=nginx  --with-http_ssl_module  --with-http_gzip_static_module --http-client-body-temp-path=/usr/local/nginx/client/ --http-proxy-temp-path=/usr/local/nginx/proxy/   --http-fastcgi-temp-path=/usr/local/nginx/fcgi/   --with-poll_module  --with-file-aio  --with-http_realip_module  --with-http_addition_module --with-http_random_index_module   --with-pcre   --with-http_stub_status_module
        - make
        - make install
    - require:
        - cmd.run: extract_nginx
        - pkg: nginx_pkg
    - unless: test -d /usr/local/nginx

nginx_make:
  cmd.run:
    - cwd: /home/nginx-1.7.3
    - names:
        - make
    - require:
        - pkg: nginx_pkg

nginx_install:
  cmd.run:
    - cwd: /home/nginx-1.7.3
    - names:
         - make install
    - require:
        - pkg: nginx_pkg
        - cmd: nginx_make
[root@salt_server nginx]#

查看配置文件conf.sls

[root@salt_server nginx]# cat conf.sls 
include:			##引用install变量
  - nginx.install
{% set nginx_user = 'nginx' + '' + 'nginx' %}		###指定用户变量。

nginx_conf:
  file.managed:		##Nginx配置文件
    - name: /usr/local/nginx/conf/nginx.conf
    - source: salt://nginx/files/nginx.conf
    - template: jinja
    - defaults:
        nginx_user: {{ nginx_user }}
        num_cpus: {{grains['num_cpus']}}

nginx_service:		##Nginx服务管理
  file.managed:
    - name: /etc/init.d/nginx
    - user: root
    - mode: 755
    - source: salt://nginx/files/nginx
  cmd.run:		## 设置Nginx服务开机启动
    - names:
       - /sbin/chkconfig --add nginx
       - /sbin/chkconfig --level 35 nginx on
    - unless: /sbin/chkconfig --list nginx
  service.running:		##Nginx启动状态
    - name: nginx
    - enable: True
    - reload: True
[root@salt_server nginx]# 

查看vhost文件

首先是引用nginx.install文件,
[root@salt_server nginx]# cat vhost.sls 
include:
  - nginx.install
#通过pillar文件引用判断,如下是for循环来做判断,引用的是vhost.conf
{% for vhostname in pillar['vhost'] %}		

{{vhostname['name']}}:
   file.managed:
     - name: {{vhostname['target']}}
     - source: salt://nginx/files/vhost.conf
     - target: {{vhostname['target']}}
     - template: jinja
     - defaults:
         server_name: {{grains['fqdn_ip4'][0]}}
         log_name: {{vhostname['name']}}
     - watch_in:
         service: nginx
{% endfor %}
[root@salt_server nginx]# 

files文件

Nginx 是启动脚本
nginx.conf	主配置文件
vhost.conf	虚拟目录的配置文件
nginx_log_cut.sh	日志切割脚本
nginx-1.7.3.tar.gz	Nginx安装包

[root@salt_server nginx]# tree files/
files/
├── nginx
├── nginx-1.7.3.tar.gz
├── nginx.conf
├── nginx_log_cut.sh
└── vhost.conf

0 directories, 5 files
[root@salt_server nginx]#

pillars的top.sls

[root@salt_server nginx]# cat /srv/pillar/top.sls 	####vhost是针对所有文件
base:
  '*':
     - vhost
  'salt_client2':
     - apache.packages  ##http install
[root@salt_server nginx]# 

虚拟目录设置

pillars

测试:

salt 'salt_client1' state.highstate test=true

实施:

salt 'salt_client1' state.highstate

客户端查看:

/etc/rc.d/init/nginx status
links 127.0.0.1

相关源码文件:

https://github.com/opsnotes/saltsource ###源码配置文件


See also