文章目錄
jinja模板的概念
Jinja是一種基於python的模板引擎,在SLS文件裏可以直接使用jinja模板來做一些操作。
通過jinja模板可以爲不同服務器定義各自的變量。
兩種分隔符: {% … %} 和 {{ … }}
前者用於執行諸如 for 循環 或賦值的語句,後者把表達式的結果打印到模板上。
jinja模板的使用方式
Jinja最基本的用法是使用控制結構包裝條件
# vim /srv/salt/test.sls
/mnt/testfile:
file.append:
{% if grains['fqdn'] == 'server2' %}
- text: server2
{% elif grains['fqdn'] == 'server3' %}
- text: server3
{% endif %}
1.2.
Jinja在普通文件的使用
# vim /srv/salt/apache.sls
/etc/httpd/conf/httpd.conf:
file.managed:
- source: salt://httpd.conf
- template: jinja
- context:
bind: 172.25.0.2
在ser3查看時,已經被替換
直接引用grains變量
Listen {{ grains['ipv4'][1] }}
直接引用pillar變量:
Listen {{ pillar['ip'] }}
在state文件中引用:
- template: jinja
- context:
bind: {{ pillar['ip'] }}
import方式,可在state文件之間共享
定義變量文件:
# vim lib.sls
{% set port = 80 %}
導入模板文件:
# vim httpd.conf
{% from 'lib.sls' import port %}
...
Listen {{ prot }}
在ser3上查看
saltsatck部署keepalived
- keepalived安裝
[root@server2 salt]# mkdir keepalived
[root@server2 keepalived]# pwd
/srv/salt/keepalived
[root@server2 keepalived]# cat init.sls
install-kp:
pkg.installed:
- name: keepalived
[root@server2 keepalived]# salt '*' state.sls keepalived # 所有的節點安裝
2.得到配置文件
[root@server2 keepalived]# scp server3:/etc/keepalived/keepalived.conf .
3.master端 的配置
root@server2 keepalived]# cat init.sls
install-kp:
pkg.installed:
- name: keepalived
file.managed:
- name: /etc/keepalived/keepalived.conf
- source: salt://keepalived/keepalived.conf
service.running:
- name: keepalived
- reload: true
- watch:
- file: install-kp
[root@server2 keepalived]# cat keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from keepalived@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.25.2.100
}
}
[root@server2 keepalived]# salt server3 state.sls keepalived
ser3做master端
3.backup端的設置
[root@server2 keepalived]# cat /srv/salt/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from keepalived@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state {{ STATE }}
interface eth0
virtual_router_id 51
priority {{ PRI }}
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
{{ VIP }}
}
}
[root@server2 keepalived]# cat /srv/salt/keepalived/init.sls
install-kp:
pkg.installed:
- name: keepalived
file.managed:
- name: /etc/keepalived/keepalived.conf
- source: salt://keepalived/keepalived.conf
- template: jinja
- contest:
STATE: {{ pillar['kpstate'] }}
PRI: {{ pillar['kppri'] }}
VIP: {{ pillar['vip'] }}
service.running:
- name: keepalived
- reload: true
- watch:
- file: install-kp
[root@server2 keepalived]# cat /srv/pillar/web.sls
{% if grains['fqdn'] == 'server3' %}
webserver: httpd
port: 8080
kpstate: MASTER
kppri: 100
{% elif grains['fqdn'] == 'server4' %}
webserver: nginx
port: 80
kpstate: BACKUP
kppri: 50
{% endif %}
vip: 172.25.2.100
[root@server2 keepalived]# salt '*' state.sls keepalived
方式二:
[root@server2 keepalived]# cat /srv/salt/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from keepalived@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state {{ STATE }}
interface eth0
virtual_router_id 51
priority {{ PRI }}
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
{{ VIP }}
}
}
[root@server2 keepalived]# cat /srv/salt/keepalived/init.sls
install-kp:
pkg.installed:
- name: keepalived
file.managed:
- name: /etc/keepalived/keepalived.conf
- source: salt://keepalived/keepalived.conf
- template: jinja
- contest:
{% if grains['fqdn'] == 'server3' %}
webserver: httpd
port: 8080
STATE: MASTER
PRI: 100
VIP: 172.25.60.100
{% elif grains['fqdn'] == 'server4' %}
webserver: nginx
port: 80
STATE: BACKUP
PRI: 50
VIP: 172.25.60.100
{% endif %}
service.running:
- name: keepalived
- reload: true
- watch:
- file: install-kp
[root@server2 keepalived]# salt '*' state.sls keepalived