saltstack的JINJIA模块应用

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_42036824/article/details/89191215
[root@server1 salt]# pwd
/srv/salt
[root@server1 salt]# cd httpd/
[root@server1 httpd]# vim service.sls 
[root@server1 httpd]# cat service.sls 
include:
  - httpd.install

/etc/httpd/conf/httpd.conf:
  file.managed:
    - source: salt://httpd/files/httpd.conf
    - template: jinja
      port: 8080

httpd-service:
  service.running:
    - name: httpd
    - enable: False
    - reload: True
      watch:
        - file: /etc/httpd/conf/httpd.conf

[root@server1 httpd]# cd files/
[root@server1 files]# vim httpd.conf 
Listen  {{ port }}

[root@server1 httpd]# salt server2 state.sls httpd.service

[root@server2 salt]# netstat -antlp
  • 端口变为8080

在这里插入图片描述

[root@server1 httpd]# cat service.sls 
include:
  - httpd.install

/etc/httpd/conf/httpd.conf:
  file.managed:
    - source: salt://httpd/files/httpd.conf
    - template: jinja
      port: 8080
      host: 172.25.19.2
      ##host: {{ grains['ipv4'][-1] }}      选一种即可
httpd-service:
  service.running:
    - name: httpd
    - enable: False
    - reload: True
      watch:
        - file: /etc/httpd/conf/httpd.conf

Listen  {{ host }}:{{ port }}

[root@server2 salt]# systemctl restart httpd
[root@server2 salt]# netstat -antlp
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 172.25.19.2:8080        0.0.0.0:*               LISTEN      3878/httpd  

在这里插入图片描述

[root@server1 ~]# cd /srv/pillar/
[root@server1 pillar]# vim web.sls 
[root@server1 pillar]# cat web.sls 
{% if grains['fqdn'] == 'server2' %}
webserver: httpd
IP: 172.25.19.2
{% elif grains['fqdn'] == 'server3' %}
webserver: nginx
IP: 172.25.19.3 
{% endif %}

  
[root@server1 httpd]# pwd
/srv/salt/httpd
[root@server1 httpd]# cat service.sls 
include:
  - httpd.install

/etc/httpd/conf/httpd.conf:
  file.managed:
    - source: salt://httpd/files/httpd.conf
    - template: jinja
      port: 80
      host: {{ pillar['IP'] }}

httpd-service:
  service.running:
    - name: httpd
    - enable: False
    - reload: True
      watch:
        - file: /etc/httpd/conf/httpd.conf

[root@server1 httpd]# salt server2 state.sls httpd.service
[root@server2 ~]# netstat -antlp
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 172.25.19.2:80          0.0.0.0:*               LISTEN      1143/httpd

优先级

[root@server1 httpd]# vim lib.sls    ##优先级高
{% set host = '172.25.19.2' %}

[root@server1 files]# vim httpd.conf
{% from 'httpd/lib.sls' import test %}
host: 127.0.0.1
[root@server1 httpd]# salt server2 state.sls httpd.service

[root@server2 ~]# netstat -antlp
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 127.0.0.1:80            0.0.0.0:*               LISTEN      1143/httpd 

[root@server1 nginx]# cat install.sls 
{% set nginx_ver = '1.15.8' %}

nginx-install:
  pkg.installed:
    - pkgs:
      - pcre-devel
      - zlib-devel
      - gcc
      - make

  file.managed:
    - name: /mnt/nginx-{{ nginx_ver }}.tar.gz
    - source: salt://nginx/files/nginx-{{ nginx_ver }}.tar.gz

  cmd.run:
    - name: cd /mnt && tar zxf nginx-{{ nginx_ver }}.tar.gz && cd nginx-{{ nginx_ver }} && sed -i 's/CFLAGS="$CFLAGS -g"/#CFLAGS="$CFLAGS -g"/g' auto/cc/gcc && ./configure --prefix=/usr/local/nginx &> /dev/null && make &> /dev/null && make install &> /dev/null && cd .. && rm -fr nginx-{{ nginx_ver }}
    - creates: /usr/local/nginx 
[root@server1 nginx]# cat service.sls 
include:
  - nginx.install
  - users.nginx

/usr/local/nginx/conf/nginx.conf:
  file.managed:
    - source: salt://nginx/files/nginx.conf
    - template: jinja
nginx-service:
  file.managed:
    - name: /etc/systemd/system/nginx.service
    - source: salt://nginx/files/nginx.service

  service.running:
    - name: nginx
    - reload: True
    - watch:
      - file: /usr/local/nginx/conf/nginx.conf

[root@server3 mnt]# ls
nginx-1.15.8.tar.gz
[root@server3 mnt]# rm -fr *
再次推送就过来了
[root@server1 nginx]# salt server3 state.sls nginx.service
[root@server3 mnt]# ls
nginx-1.15.8.tar.gz
展开阅读全文

没有更多推荐了,返回首页