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/cd 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

这里写图片描述

测试:

这里写图片描述

猜你喜欢

转载自blog.csdn.net/Argued_D/article/details/81781609