[root@server1 ~]# cd /etc/salt/
[root@server1 salt]# vim master
828 pillar_roots:
829 base:
830 - /srv/pillar
[root@server1 salt]# systemctl restart salt-master
[root@server1 salt]# mkdir /srv/pillar
[root@server1 salt]# cd /srv/pillar/
[root@server1 pillar]# mkdir web
[root@server1 pillar]# cd web/
[root@server1 web]# ls
[root@server1 web]# vim vars.sls
[root@server1 web]# cat vars.sls
{% if grains['fqdn'] == 'server2' %}
ip: 172.25.8.2
state: MASTER
vrid: 100
priority: 100
{% elif grains['fqdn'] == 'server3' %}
state: BACKUP
vrid: 100
priority: 50
{% endif %}
[root@server1 pillar]# ls
web
[root@server1 pillar]# vim top.sls
[root@server1 pillar]# cat top.sls
base:
'*':
- web.vars
[root@server1 salt]# cd apache/
[root@server1 apache]# ls
files install.sls
[root@server1 apache]# cat install.sls
install-apache:
pkg.installed:
- pkgs:
- httpd
file.managed:
- name: /etc/httpd/conf/httpd.conf
- source: salt://apache/files/httpd.conf
- template: jinja
- context:
port: 80
host: 172.25.8.2
service.running:
- name: httpd
- watch:
- file: install-apache
[root@server1 salt]# cd nginx/
[root@server1 nginx]# ls
files install.sls service.sls
[root@server1 nginx]# cat service.sls
include:
- nginx.install
/usr/local/nginx/conf/nginx.conf:
file.managed:
- source: salt://nginx/files/nginx.conf
service-nginx:
file.managed:
- name: /usr/lib/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@server1 salt]# cd keepalived/
[root@server1 keepalived]# ls
files install.sls
[root@server1 keepalived]# cat install.sls
install-keepalived:
pkg.installed:
- pkgs:
- keepalived
file.managed:
- name: /etc/keepalived/keepalived.conf
- source: salt://keepalived/files/keepalived.conf
- template: jinja
- context:
STATE: {{ pillar['state'] }}
VRID: {{pillar['vrid'] }}
PRIORITY: {{ pillar[priority] }}
service.running:
- name: keepalived
- watch:
- file: install-keepalived
[root@server1 keepalived]# cd files/
[root@server1 files]# ls
keepalived.conf
[root@server1 files]# vim keepalived.conf
16 state {{ STATE}}
17 interface eth0
18 virtual_router_id {{ VRID }}
19 priority {{ PRIORITY }}
[root@server1 salt]# ls
apache keepalived nginx pkgs top.sls
[root@server1 salt]# cat top.sls
base:
'roles:apache':
- match: grain
- apache.install
- keepalived.install
'roles:nginx':
- match: grain
- nginx.service
- keepalived.install
[root@server1 salt]# salt '*' state.highstate