saltstack的JINJA模块应用

第一种:

[root@server1 ~]# cd /srv/salt/
[root@server1 salt]# cd httpd/
[root@server1 httpd]# vim install.sls 
httpd:
  pkg.installed
php:
  pkg.installed

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

/etc/httpd/conf/httpd.conf:
    file.managed:
      - source: salt://httpd/files/httpd.conf
      - mode: 644
      - user: root
      - template: jinja
      - context:
          bind: 172.25.40.2
          port: 8080
[root@server1 httpd]# cd files/
[root@server1 files]# vim httpd.conf           
 136 Listen {{port}}
[root@server1 files]# salt server2 state.sls httpd.install

这里写图片描述
第二种:

[root@server1 files]# vim httpd.conf 
 136 Listen {{ bind }}:{{ port }}
[root@server1 files]# salt server2 state.sls httpd.install

这里写图片描述
在【server2】上查看端口更改为8080

[root@server2 ~]# vim /etc/httpd/conf/httpd.conf

这里写图片描述

第三种:

[root@server1 files]# vim httpd.conf 
{% from 'httpd/lib.sls' import port with context %}
[root@server1 files]# cd ..
[root@server1 httpd]# vim lib.sls
{% set port = 80 %}
[root@server1 httpd]# salt server2 state.sls httpd.install

这里写图片描述


grains

[root@server1 httpd]# salt server2 grains.item ip
[root@server1 httpd]# salt server2 grains.item ipv4

这里写图片描述

[root@server1 httpd]# vim install.sls 
 21           bind: {{ grains['ipv4'][-1] }}
[root@server1 httpd]# cd files/
[root@server1 files]# vim httpd.conf 
 137 Listen {{ grains['ipv4'][-1] }}:{{ port }}
[root@server1 httpd]# salt server2 state.sls httpd.install

测试:

查看端口已经推送到server2上

[root@server2 ~]# vim /etc/httpd/conf/httpd.conf

这里写图片描述


[root@server1 httpd]# cd /srv/
[root@server1 srv]# cd pillar/
[root@server1 pillar]# cd web/
[root@server1 web]# ls
install.sls
[root@server1 web]# vim install.sls 
{% if grains['fqdn'] == 'server2' %}
webserver: httpd
bind: 172.25.40.2
port: 8080
{% elif grains['fqdn'] == 'server3' %}
webserver: nginx
{% endif %}

[root@server1 web]# cd /srv/salt/httpd/files/
[root@server1 files]# vim httpd.conf 
 137 Listen {{ bind }}:{{ port }}
[root@server1 files]# cd ..
[root@server1 httpd]# vim install.sls 
 21         bind: {{ pillar['bind'] }}
 22         port: {{ pillar['port'] }}
[root@server1 httpd]# salt server2 state.sls httpd.install

这里写图片描述

测试:

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值