saltstack用sls文件安装nginx(2)

nginx实例安装:
在salt的默认目录/src/salt建立一个目录nginx,建立init.sls
在这里插入图片描述

install-nginx:
  pkg.installed:
    - pkgs:             #安装nigx所依赖的包
      - gcc
      - make
      - openssl-devel
      - pcre-devel
  archive.extracted:   #解压
    - name: /mnt
    - source: salt://nginx/
    - archive_format: tar

解压过去测试一下:
[root@node1 nginx]# salt node3 state.sls nginx
在这里插入图片描述
在这里插入图片描述在这里插入图片描述
启动服务:

[root@node1 nginx]# vim nginx.service 
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target

[root@node1 nginx]# ls
init.sls  nginx-1.16.1.tar.gz  nginx.conf  nginx.service  service.sls
[root@node1 nginx]# vim init.sls
install-nginx:
  pkg.installed:
    - pkgs:
      - gcc
      - make
      - openssl-devel
      - pcre-devel
  archive.extracted:
    - name: /mnt
    - source: salt://nginx/nginx-1.16.1.tar.gz
    - archive_format: tar
  cmd.run:
    - name: cd /mnt/nginx-1.16.1 && sed -i 's/CFLAGS="$CFLAGS -g"/#CFLAGS="$CFLAGS -g"/g' auto/cc/gcc && ./configure --prefix=/usr/local/nginx/ --with-http_ssl_module &> /dev/null && make && make install &> /dev/null
    - creates: /usr/local/nginx
  file.managed:
    - name: /lib/systemd/system/nginx.service
    - source: salt://nginx/nginx.service
  service.running:
    - name: nginx

在这里插入图片描述
在这里插入图片描述
在node1上更改端口,使node3上生效
再编写一个文件service.sls。在nginx目录里有init.sls.和servicr.sls.默认执行init.sls,如何两个都执行

第一种方法:top.sls
[root@node1 salt]# pwd
/srv/salt
[root@node1 salt]# ls
apache  _modules  nginx  top.sls
[root@node1 salt]# vim top.sls
base:
  "node2":
    - apache
  "node3":
     - nginx.init
     - nginx.service
[root@node1 salt]# cd nginx/
[root@node1 nginx]# ls
init.sls  nginx-1.16.1.tar.gz  nginx.conf  nginx.service  service.sls
[root@node1 nginx]# vim init.sls
install-nginx:
  pkg.installed:
    - pkgs:
      - gcc
      - make
      - openssl-devel
      - pcre-devel
  archive.extracted:
    - name: /mnt
    - source: salt://nginx/nginx-1.16.1.tar.gz
    - archive_format: tar
  cmd.run:
    - name: cd /mnt/nginx-1.16.1 && sed -i 's/CFLAGS="$CFLAGS -g"/#CFLAGS="$CFLAGS -g"/g' auto/cc/gcc && ./configure --prefix=/usr/local/nginx/ --with-http_ssl_module &> /dev/null && make && make install &> /dev/null
    - creates: /usr/local/nginx
  file.managed:
    - name: /lib/systemd/system/nginx.service
    - source: salt://nginx/nginx.service
 
[root@node1 nginx]# vim service.sls
service-nginx:
  file.managed:
    - source: salt://nginx/nginx.conf
    - name: /usr/local/nginx/conf/nginx.conf
  service.running:
    - name: nginx
    - watch:
      - file: service-nginx
[root@node1 salt]# salt node3 state.highstate    #先执行init.sls,再执行service.sls
第二种:include   #在service.sls里加init.sls。nginx默认就是nginx.init
[root@node1 nginx]# vim service.sls 
include:
  - nginx
service-nginx:
  file.managed:
    - source: salt://nginx/nginx.conf
    - name: /usr/local/nginx/conf/nginx.conf
  service.running:
    - name: nginx
    - watch:
      - file: service-nginx
[root@node1 nginx]# salt node3 state.sls nginx.service  #nginx默认就是nginx.init,在此时我们是service.sls包括了init,所以执行的时候要加nginx.service
第三种:init.sls包括service.sls
[root@node1 nginx]# vim service.sls
/usr/local/nginx/conf/nginx.conf:  #既当ID声明又当file.name。一个sls文件里可以有多个声明
  file.managed:
    - source: salt://nginx/nginx.conf
    - require:
      - sls: nginx
  service.running:
    - name: nginx
    - watch:
      - file: /usr/local/nginx/conf/nginx.conf
[root@node1 nginx]# vim init.sls 
install-nginx:
  pkg.installed:
    - pkgs:
      - gcc
      - make
      - openssl-devel
      - pcre-devel
  archive.extracted:
    - name: /mnt
    - source: salt://nginx/nginx-1.16.1.tar.gz
    - archive_format: tar
  cmd.run:
    - name: cd /mnt/nginx-1.16.1 && sed -i 's/CFLAGS="$CFLAGS -g"/#CFLAGS="$CFLAGS -g"/g' auto/cc/gcc && ./configure --prefix=/usr/local/nginx/ --with-http_ssl_module &> /dev/null && make && make install &> /dev/null
    - creates: /usr/local/nginx
  file.managed:
    - name: /lib/systemd/system/nginx.service
    - source: salt://nginx/nginx.service

include:
  - nginx.service
[root@node1 nginx]# salt node3 state.sls nginx
接第三种方法:第三种方法的sevice也可以这样写,一个sls里可以有多个ID声明:
[root@node1 nginx]# vim service.sls 
[root@node1 nginx]# pwd
/srv/salt/nginx
configure:
  file.managed:
    - source: salt://nginx/nginx.conf
    - name: /usr/local/nginx/conf/nginx.conf
    - require:
      - sls: nginx
qidong:
  service.running:
    - name: nginx
    - watch:
      - file: /usr/local/nginx/conf/nginx.conf
[root@node1 nginx]# salt node3 state.sls nginx

在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值