1、saltstack簡介:
什麼是saltstack?
saltstack是基於python開發的一套C/S架構配置管理工具
使用SSL證書籤方的方式進行認證管理
號稱世界上最快的消息隊列ZeroMQ使得SaltStack能快速在成千上萬臺機器上進行各種操作
採用RSA Key方式確認身份
傳輸採用AES加密,這使得它的安全性得到了保障。
主要功能
saltstack最主要的兩個功能是: 配置管理 and 遠程執行
管理員能對多個操作系統進行管理
saltstack不只是一個配置管理工具,還是一個雲計算與數據中心架構編排的利器
saltstack已經支持Docker相關模塊
在友好地支持各大雲平臺之後,配合Saltstack的Mine實時發現功能可以實現各種雲平臺業務的自動擴展
通過部署saltstack,我們可以在成千萬臺服務器上做到批量執行命令,根據不同業務進行配置集中化管理、分發文件、採集服務器數據、操作系統基礎及軟件包管理等,saltstack是運維人員提高工作效率、規範業務配置與操作的利器。
Saltstack架構
• Saltstack基於C/S架構
- 服務器端稱作Master 端口爲 4506
- 客戶端稱作Minion 端口爲 4505
- 可以實現傳統處理方式,即:客戶端發送請求給服務器,服務器收到請求後處理請求,再將結果返回
- 也可以使用消息隊列中的發佈與訂閱(pub/sub)服務模式
saltstack工作原理:
Saltstack工作機制
• Master和Minion都以守護進程的方式運行
• Master監聽配置文件裏定義的ret_port(接收minion請求),和publish_port(發佈消息)的端口
• 當Minion運行時,它會自動連接到配置文件裏定義的Master地址ret_port端口進行連接認證
實現過程:
saltstack採用C/S模式server端就是salt的master,client端就是minion,minion與master>之間通過ZeroMQ消息隊列通信。
minion上線後先與master端聯繫,把自己的pub key發過去,這時master端通過salt-key -L命令就會看到minion的key,接受該minion-key後,也就是master與minion已經互信。
master可以發送任何指令讓minion執行了,salt有很多可執行模塊,比如說cmd模塊,>在安裝minion的時候已經自帶了,它們通常位於你的python庫中,locate salt | grep /usr/ 可以看到salt自帶的所有東西。
這些模塊是python寫成的文件,裏面會有好多函數,如cmd.run,當我們執行salt '*' cmd.run 'uptime'的時候,master下發任務匹配到的minion上去,minion執行模塊函數,並
返回結果。master監聽4505和4506端口,4505對應的是ZMQ的PUB system,用來發送消息,4506對應的是REP system是來接受消息的。
具體步驟如下:
saltstack的Master與Minion之間通過ZeroMq進行消息傳遞,使用了ZeroMq的發佈-訂閱模式,連接方式包括tcp,ipc
salt命令,將cmd.run ls命令從salt.client.LocalClient.cmd_cli發佈到master,獲>取一個Jodid,根據jobid獲取命令執行結果。
master接收到命令後,將要執行的命令發送給客戶端minion。
minion從消息總線上接收到要處理的命令,交給minion._handle_aes處理
minion._handle_aes發起一個本地線程調用cmdmod執行ls命令。線程執行完ls後,調用
minion._return_pub方法,將執行結果通過消息總線返回給master
master接收到客戶端返回的結果,調用master._handle_aes方法,將結果寫的文件中
salt.client.LocalClient.cmd_cli通過輪詢獲取Job執行結果,將結果輸出到終端。
2、實驗過程
實驗環境:
redhat6.5
關閉防火牆、iptables、setenforce
master: test1:172.25.1.11
minion: test2: 172.25.1.12
千萬注意:實驗開始前,實驗用到的每個主機需要進行解析,否則配置文件內必須填寫主機對應的ip 。
[root@test1 ~]# vim /etc/hosts
1、搭建yum 源
首先下載rhel6.5的saltstack資源包,然後配置saltstack所需的yum源(筆者這裏搭建的是本地yum源,你也可以搭建網絡yum源):
master端yum源
[root@foundation1 pub]# ls rhel6/ //這是已經下載好並放到pub下
[root@foundation1 pub]# scp rhel6/ root@test1:/
[root@test1 ~ ]# cd /rhel6
[root@test1 ~ ]# ls //可以看到rhel6的資源包,這裏不再列出
[root@test1 rhel6]# vim /etc/yum.repos.d/rhel-source.repo
[rhel-source]
name=Red Hat Enterprise Linux $releasever - $basearch - Source
baseurl=http://172.25.1.250/rhel6.5 //網絡yum源
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release[salt]
name=saltstack
baseurl=file:///rhel6 //本地yum源,需要搭建的saltstack倉庫
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release
[root@test1 rhel6]# yum clean all //可以看到共有29個資源包
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
注意:如果不成功,可以試着這麼做:
[root@test1 rhel6]# rm -rf rhel6
[root@test1 rhel6]# rm -rf repodata/
[root@test1 rhel6]# yum install -y createrepo
[root@test1 rhel6]# createrepo -v .
[root@test1 rhel6]# ls //查看會出現新的repodata目錄
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
minion端yum源
[root@test1 ~]# scp -r /rhel6/ root@test2:/
[root@test1 ~]# scp /etc/yum.repos.d/rhel-source.repo root@test2:/etc/yum.repos.d/
[root@test2 rhel6]# yum repolist
2、安裝程序
[root@test1 ~]# yum install -y salt-master //安裝master
[root@test1 ~]# /etc/init.d/salt-master start
[root@test2 ~]# yum install -y salt-minion //安裝minion
若出現yum源過低的報錯,可下載高一點的yum源資源包,如
提示yum版本過低,下載大於3.2.29-56版本的yum即可
yum install -y yum-3.2.29-69.el6.centos.noarch.rpm
3、配置
[root@test2 ~ ]# vim /etc/salt/minion //這裏需要注意解析
[root@test2 ~]# vim /etc/hosts //解析
172.25.1.11 test1
[root@test2 ~]# /etc/init.d/salt-minion start //開啓minion端的服務
交換公鑰
[root@test1 ~]# salt-key -L //查看是否發現minion,發現test2
[root@test1 ~]# salt-key -A //接受並添加
[root@test1 ~]# salt-key -L //查看是否添加成功
查看公鑰是否交換成功
test1端:
[root@test1 ~]# cd /etc/salt/pki/master
[root@test1 master]# md5sum master.pub
在test2端查看公鑰和test1的是否相同:
[root@test2 ~]# cd /etc/salt/pki/minion/
[root@test2 minion]# ls
[root@test2 minion]# md5sum minion_master.pub
可以看出test1和test2的公鑰是相同的,則密鑰交換成功
在test1查看信息:
[root@test1 master]# yum install -y tree
[root@test1 master]# tree //可以看出minion端只有一個test2
4505端口是長鏈接:
[root@test1 master]# yum install -y lsof
[root@test1 master]# lsof -i :4505
到此,master---minion就搭建成功啦,接着開始部署lamp架構
4、簡單瞭解YAML語法
由於編輯腳本時需要用YAML語言進行編寫,所以:
默認的sls文件的renderer是YAML renderer。YAML是一個有很多強大特性的標記性語言。Salt使用了一個YAML的小型子集,映射非常常用的數據結構,像列表和字典。YAML renderer的工作是將YAML數據格式的結構編譯成爲python數據結構給salt使用。
只要記住三個非常簡單的規則就可以使用YAML語法寫sls文件了。
- 規則一: 縮進
YAML使用一個固定的縮進風格表示數據層結構關係。salt需要每個縮進級別由兩個空格組成。不要使用tab鍵。
- 規則二: 冒號
字典的keys在YAML中的表現形式是一個以冒號結尾的字符串。values的表現形式冒號下面的每一行,用一個空格隔開:
my_key: my_value
另一種選擇,一個value可以通過縮進與key聯接。
my_key:
my_value註解
上面的語法是有效的YAML,但是在sls文件罕見,因爲通常情況下,一個key的value不是單一的,而是一個 列表 的values。
字典可以被嵌套:
first_level_dict_key:
second_level_dict_key: value_in_second_level_dict
- 規則三: 短橫槓
想要表示列表項,使用一個短橫槓加一個空格。多個項使用同樣的縮進
級別作爲同一列表的一部分。- list_value_one
- list_value_two
- list_value_three
5、部署LAMP架構
master端
1.安裝apache、php
[root@test1 ~]# vim /etc/salt/master
[root@test1 ~]# mkdir /srv/salt
[root@test1 ~]# cd /srv/salt/
[root@test1 salt]# mkdir apache
[root@test1 salt]# cd apache/
[root@test1 apache]# vim web.sls
apache-install:
pkg.installed:
- pkgs:
- httpd
- php
注意,爲了實驗效果,若test2已經有httpd服務請提前將test2的httpd服務卸載掉
[root@test2 ~]# yum remove httpd
[root@test1 apache]# salt test2 state.sls apache.web //master將服務推過去到test2
[root@test2 ~]# rpm -q httpd
[root@test2 ~]# rpm -q php
2.實現文件傳輸
[root@test1 apache]# vim web.sls
apache-install:
pkg.installed:
- pkgs:
- httpd
- php
/var/www/html/index.php:
file.managed:
- source: salt://apache/files/index.php
- mode: 644
- user: root
- group: root
[root@test1 apache]# mkdir files
[root@test1 apache]# cd files
[root@test1 files]# vim index.php //寫出php的默認
<?php
phpinfo()
?>
[root@test1 files]# salt test2 state.sls apache.web //推送成功
[root@test2 ~]# cd /var/www/html/
[root@test2 html]# ls //可以發現出現了index.php
index.php
此時可以在網頁進行查看:
當然配置文件也可以是:
apache-install:
pkg.installed:
- pkgs:
- httpd
- php
file.managed:
- name: /var/www/html/index.php
- source: salt://apache/files/index.php
- mode: 644
- user: root
- group: root
或者將index.php換成index.index 但是需要在master端的/srv/salt/apache/files/下提前創建好index.html
3.開啓apache服務
apache-install:
pkg.installed:
- pkgs:
- httpd
- php
file.managed:
- name: /var/www/html/index.html
- source: salt://apache/files/index.html
- mode: 644
- user: root
- group: root
service.running:
- name: httpd
[root@test1 apache]# cd files
[root@test1 files]# vim index.html
<h1>index.html</h1>
[root@test1 files]# cd ..
[root@test1 apache]# salt test2 state.sls apache.web
到客戶端進行查看,index.html是否遠程傳輸過來
在網頁進行訪問:
3.實現服務的開機自啓
[root@test1 apache]# vim web.sls
apache-install:
pkg.installed:
- pkgs:
- httpd
- php
file.managed:
- name: /var/www/html/index.php
- source: salt://apache/files/index.php
- mode: 644
- user: root
- group: root
service.running:
- name: httpd
- enable: True
- reload: True
進行測試:
推送成功,在客戶端進行查看:
[root@test2 html]# chkconfig --list httpd
[root@test1 apache]# vim web.sls
apache-install:
pkg.installed:
- pkgs:
- httpd
- php
file.managed:
- name: /var/www/html/index.php
- source: salt://apache/files/index.php
- mode: 644
- user: root
- group: root
apache-service:
file.managed:
- name: /etc/httpd/conf/httpd.conf
- source: salt://apache/files/httpd.conf
service.running:
- name: httpd
- enable: True
- reload: True
- watch:
- file: apache-service
[root@test1 apache]# cd files
[root@test1 files]# cp /etc/httpd/conf/httpd.conf . //否則會有報錯
[root@test1 files]# ls
httpd.conf index.html index.php
[root@test1 files]# cd ..
[root@test1 apache]# salt test2 state.sls apache.web
推送成功
此時apache服務的端口爲80
嘗試將服務器端的apache配置文件端口由80更改爲8080
[root@test1 apache]# cd files/
[root@test1 files]# vim httpd.conf
再次推送
[root@test1 apache]# salt test2 state.sls apache.web //推送成功
在客戶端查看:
[root@test2 html]# netstat -antlp | grep httpd
[root@test1 apache]# vim install.sls
apache-install:
pkg.installed:
- pkgs:
- httpd
- php
file.managed:
- name: /var/www/html/index.php
- source: salt://apache/files/index.php
- mode: 644
- user: root
- group: root
[root@test1 apache]# vim service.sls
include:
- apache.install
apache-service:
file.managed:
- name: /etc/httpd/conf/httpd.conf
- source: salt://apache/files/httpd.conf
service.running:
- name: httpd
- enable: True
- reload: True
- watch:
- file: apache-service
[root@test1 apache]# cd files/
[root@test1 files]# vim httpd.conf
一鍵推送
[root@test1 files]# salt test2 state.sls apache.service //推送成功
在test2上查看
[root@test2 html]# netstat -antlp | grep httpd
6.一鍵部署nginx
root@test1 salt]# ls
apache
[root@test1 salt]# mkdir nginx
[root@test1 salt]# cd nginx/
[root@test1 nginx]# mkdir files
[root@test1 nginx]# cd files/
[root@test1 files]# cp /root/nginx-1.14.0.tar.gz . //注意:這裏需要用到nginx源碼包,請提前將其下載下來並放到/root下以便拷貝
[root@test1 files]# touch nginx
[root@test1 files]# ls
[root@test1 files]# cd ..
[root@test1 nginx]# vim install.sls
nginx-install:
pkg.installed:
- pkgs:
- pcre-devel
- openssl-devel
- gcc
file.managed:
- name: /mnt/nginx-1.14.0.tar.gz
- source: salt://nginx/files/nginx-1.14.0.tar.gz
cmd.run:
- name: cd /mnt && tar zxf nginx-1.14.0.tar.gz && cd nginx-1.14.0 && sed -i.bak 's/#define NGINX_VER "nginx\/" NGINX_VERSION/#define NGINX_VER "nginx"/g' src/core/nginx.h && sed -i.bak 's/CFLAGS="$CFLAGS -g"/#CFLAGS="$CFLAGS -g"/g' auto/cc/gcc && ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-threads --with-file-aio &> /dev/null && make &> /dev/null && make install &> /dev/null
- creates: /usr/local/nginx //表示當系統有nginx文件時不進行重複安裝與編譯
[root@test1 nginx]# salt test2 state.sls nginx.install //遠程調用執行成功
現在在客戶端進行查看:
[root@test2 ~]# ps -ax //快速抓拍可以看到正在編譯等
[root@test2 minion]# cd /mnt/
[root@test2 mnt]# ls
讓服務自動運行:
重新編寫腳本,保證一個腳本負責源碼編譯,一個腳本負責運行服務,以便更好的管理:
[root@test1 srv]# cd
[root@test1 ~]# cd /srv/salt/
[root@test1 salt]# ls
apache nginx
[root@test1 salt]# cd nginx/
[root@test1 nginx]# ls
files install.sls
[root@test1 nginx]# vim service.sls //目的是得到nginx的配置文件,讓服務自動運行
include:
- nginx.install
nginx-service:
cmd.run:
- name: /usr/local/nginx/sbin/nginx
[root@test1 nginx]# salt test2 state.sls nginx.service //推送成功
此時查看服務是否已經運行:
[root@test2 ~]# netstat -antlp | grep nginx //可以看到端口號爲80的進程
[root@test1 files]# cd ..
[root@test1 nginx]# cd files/
[root@test1 files]# vim nginx //在/srv/salt/nginx/file目錄下寫一個nginx腳本,用來調用nginx服務
#!/bin/sh
# nginx Startup script for nginx
# chkconfig: - 85 15
# processname: nginx
# config: /usr/local/nginx/conf/nginx/nginx.conf
# pidfile: /usr/local/nginx/logs/nginx.pid
# description: nginx is an HTTP and reverse proxy server
### BEGIN INIT INFO
# Provides: nginx
# Required-Start: $local_fs $remote_fs $network
# Required-Stop: $local_fs $remote_fs $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: start and stop nginx
### END INIT INFO# Source function library.
. /etc/rc.d/init.d/functionsif [ -L $0 ]; then
initscript=`/bin/readlink -f $0`
else
initscript=$0
fi#sysconfig=`/bin/basename $initscript`
#if [ -f /etc/sysconfig/$sysconfig ]; then
# . /etc/sysconfig/$sysconfig
#finginx=${NGINX-/usr/local/nginx/sbin/nginx}
prog=`/bin/basename $nginx`
conffile=${CONFFILE-/usr/local/nginx/conf/nginx.conf}
lockfile=${LOCKFILE-/var/lock/subsys/nginx}
pidfile=${PIDFILE-/usr/local/nginx/logs/nginx.pid}
SLEEPMSEC=${SLEEPMSEC-200000}
UPGRADEWAITLOOPS=${UPGRADEWAITLOOPS-5}
RETVAL=0start() {
echo -n $"Starting $prog: "daemon --pidfile=${pidfile} ${nginx} -c ${conffile}
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch ${lockfile}
return $RETVAL
}stop() {
echo -n $"Stopping $prog: "
killproc -p ${pidfile} ${prog}
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}
}reload() {
echo -n $"Reloading $prog: "
killproc -p ${pidfile} ${prog} -HUP
RETVAL=$?
echo
}upgrade() {
oldbinpidfile=${pidfile}.oldbinconfigtest -q || return
echo -n $"Starting new master $prog: "
killproc -p ${pidfile} ${prog} -USR2
echofor i in `/usr/bin/seq $UPGRADEWAITLOOPS`; do
/bin/usleep $SLEEPMSEC
if [ -f ${oldbinpidfile} -a -f ${pidfile} ]; then
echo -n $"Graceful shutdown of old $prog: "
killproc -p ${oldbinpidfile} ${prog} -QUIT
RETVAL=$?
echo
return
fi
doneecho $"Upgrade failed!"
RETVAL=1
}configtest() {
if [ "$#" -ne 0 ] ; then
case "$1" in
-q)
FLAG=$1
;;
*)
;;
esac
shift
fi
${nginx} -t -c ${conffile} $FLAG
RETVAL=$?
return $RETVAL
}rh_status() {
status -p ${pidfile} ${nginx}
}# See how we were called.
case "$1" in
start)
rh_status >/dev/null 2>&1 && exit 0
start
;;
stop)
stop
;;
status)
rh_status
RETVAL=$?
;;
restart)
configtest -q || exit $RETVAL
stop
start
;;
upgrade)
rh_status >/dev/null 2>&1 || exit 0
upgrade
;;
condrestart|try-restart)
if rh_status >/dev/null 2>&1; then
stop
start
fi
;;
force-reload|reload)
reload
;;
configtest)
configtest
;;
*)
echo $"Usage: $prog {start|stop|restart|condrestart|try-restart|force-reload|upgrade|reload|status|help|configtest}"
RETVAL=2
esacexit $RETVAL
[root@test2 ~]# scp /usr/local/nginx/conf/nginx.conf root@test1:/srv/salt/nginx/files //將test2的服務配置文件發送給test1
將/srv/salt/nginx/files/nginx.conf進行更改:
[root@test1 nginx]# cd files/
[root@test1 files]# ls
創建用戶
[root@test1 files]# vim nginx.conf
user nginx nginx;
[root@test1 files]# cd /srv/salt/
[root@test1 salt]# mkdir users
[root@test1 salt]# cd users/
[root@test1 users]# ls
[root@test1 users]# vim nginx.sls
nginx-group:
group.present:
- name: nginx
- gid: 800
nginx-user:
user.present:
- name: nginx
- uid: 800
- gid: 800
- shell: /sbin/nologin //指定shell
- createhome: False
- home: /usr/local/nginx //指定家目錄
[root@test1 users]# cd ..
[root@test1 salt]# cd nginx/
[root@test1 nginx]# ls
files install.sls service.sls
[root@test1 nginx]# vim service.sls
include:
- users.nginx
- nginx.install
/usr/local/nginx/conf/nginx.conf:
file.managed:
- source: salt://nginx/files/nginx.conf
nginx-service:
file.managed:
- name: /etc/init.d/nginx
- source: salt://nginx/files/nginx
- mode: 755
service.running:
- name: nginx
- reload: True
- watch:
- file: /usr/local/nginx/conf/nginx.conf
[root@test1 nginx]# salt test2 state.sls nginx.service //再次將其推給test2
注意:可提前將test2的nginx服務關掉
[root@test2 usr]# /usr/local/nginx/sbin/nginx -s stop
[root@test1 files]# salt test2 state.sls nginx.service //推送成功
[root@test2 usr]# netstat -antlp | grep nginx
查看test2前後的變化:
[root@test2 ~]# id nginx //推送前
[root@test2 ~]# id nginx
id: nginx: No such user
[root@test2 ~]# id nginx //推送後
爲了方便源碼編譯其他tar包時需要安裝依賴性,將依賴性工具進行打包,簡化步驟:
[root@test1 nginx]# cd ..
[root@test1 salt]# mkdir pkgs
[root@test1 salt]# cd pkgs/
[root@test1 pkgs]# vim make.sls
[root@test1 pkgs]# ls
make.sls
[root@test1 pkgs]# cd ..
[root@test1 salt]# ls
apache nginx pkgs users
[root@test1 salt]# cd nginx/
[root@test1 nginx]# ls
files install.sls service.sls
[root@test1 nginx]# vim install.sls
include:
- pkgs.make //直接導入make包,避免當多個源碼編譯都用到make裏面的安裝包時都得重新寫一遍
nginx-install:
file.managed:
- name: /mnt/nginx-1.14.0.tar.gz
- source: salt://nginx/files/nginx-1.14.0.tar.gz
cmd.run:
- name: cd /mnt && tar zxf nginx-1.14.0.tar.gz && cd nginx-1.14.0 && sed -i.bak 's/#define NGINX_VER "nginx\/" NGINX_VERSION/#define NGINX_VER "nginx"/g' src/core/nginx.h && sed -i.bak 's/CFLAGS="$CFLAGS -g"/#CFLAGS="$CFLAGS -g"/g' auto/cc/gcc && ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-threads --with-file-aio &> /dev/null && make &> /dev/null && make install &> /dev/null
- creates: /usr/local/nginx
grains、pillar、Jinja模塊的使用(作用相同)
!!!溫馨提示:下面這段是練習模塊,若需要搭建高可用架構可以直接跳過,繼續下一段動作。
例如用grains模塊:
在minion端配置
在託管主機上修改
[root@test1 salt]# vim /etc/salt/minion //test1端將roles:對應的參數改爲想要配置的服務
[root@test2 ~]# vim /etc/salt/minion //test2端將roles:對應的參數改爲想要配置的服務
[root@test2 ~]# /etc/init.d/salt-minion restart
[root@test1 salt]# /etc/init.d/salt-minion restart
[root@test1 salt]# salt '*' grains.item roles //可以看見roles對應的服務
[root@test1 salt]# vim top.sls
base:
'test3':
- haproxy.service
'roles:apache': //和apache匹配的roles推送apache
- match: grain
- apache.service
'roles:nginx': //和nginx匹配的roles推送nginx
- match: grain
- nginx.service[root@test1 keepalived]# salt '*' state.highstate //一鍵推送,此時查看可以看到test1和test2端推送上了nginx和apache服務
再如pillar模塊:(注意:pillar和grains模塊沒有什麼聯繫,但可以結合使用)
pillar模塊在master配置
[root@test1 salt]# vim /etc/salt/master
[root@test1 salt]# /etc/init.d/salt-master restart
[root@test1 srv]# mkdir pillar/ //創建pillar目錄
[root@test1 srv]# cd pillar/
[root@test1 pillar]# mkdir web
[root@test1 pillar]# cd web/[root@test1 web]# vim install.sls
{ % if grains[' fqdn '] == 'test2' % } //與test2匹配的主機推送apache
webserver: apache
{ % elif grains[' fqdn '] == 'test1' % } //與test1匹配的主機推送nginx
webserver: nginx
{ % endif % }[root@test1 web]# cd ..
[root@test1 pillar]# vim top.sls //這個文件必須要寫
base:
'*':
- web.install然後一鍵推送,成功
[root@test2 ~]# netstat -antlp | grep 80
Jinja模塊的使用
例如:需要端口是變量,可以隨時改變,那麼就需要用到Jinja這個模塊
[root@test1 pillar]# cd /srv/salt/apache/files/
[root@test1 files]# vim httpd.conf[root@test1 files]# cd ..
[root@test1 apache]# vim service.sls
include:
- apache.installapache-service:
file.managed:
- name: /etc/httpd/conf/httpd.conf
- source: salt://apache/files/httpd.conf
- template: jinja
- context:
port: 80
bind: 172.25.1.12service.running:
- name: httpd
- enable: True
- reload: True
- watch:
- file: apache-service[root@test1 apache]# salt test2 state.sls apache.service
[root@test2 ~]# netstat -antlp | grep httpd //推送過去之後查看端口變化
[root@test2 ~]# vim /etc/httpd/conf/httpd.conf //配置文件也相應變化
再如只修改端口爲任意數:
apache配置文件內將80修改爲{{ port }}
更改service.sls,添加到file.managed:後面
- template: jinja
- context:
port: 8080
一鍵推送,推送成功以後修改端口只需要修改service.sls的port對應的參數爲其它數然後一鍵推送即可。
到此這部分就結束啦
別忘記接着來看下一篇哦!