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.39.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
saltstack grains
[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】中查看端口,已经改为80
[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
测试: