安裝salt api
yum -y install salt-api pyOpenSSL
systemctl enable salt-api
生成自簽名證書(用于ssl
make testcert
cd /etc/pki/tls/private/
# 解密key檔案,生成無密碼的key檔案, 過程中需要輸入key密碼,該密碼為之前生成證書時設定的密碼
openssl rsa -in localhost.key -out localhost_nopass.key
建立用于salt-api的使用者
useradd -M -s /sbin/nologin pengyao
echo "pengyao_pass" | passwd pengyao --stdin
配置eauth, /etc/salt/master.d/eauth.conf
external_auth:
pam:
pengyao:
- .*
- '@wheel'
- '@runner'
配置Salt-API, /etc/salt/master.d/api.conf
rest_cherrypy:
port: 8000
ssl_crt: /etc/pki/tls/certs/localhost.crt
ssl_key: /etc/pki/tls/private/localhost_nopass.key
#salt master配置檔案:/etc/salt/master
#取消注釋
default_include: master.d/*.conf
systemctl restart salt-master
systemctl start salt-api
Login
Request
curl -k https://192.168.38.10:8000/login -H "Accept: application/x-yaml" \
-d username='pengyao' \
-d password='pengyao_pass' \
-d eauth='pam'
Response
return:
- eauth: pam
expire: 1385579710.806725
perms:
- .*
start: 1385536510.8067241
token: 784ee23c63794576a50ca5d3d890eb71efb0de6f
user: pengyao
其中 token 後邊的串為認證成功後擷取的token串,之後可以不用再次輸入密碼,直接使用本Token即可
查詢Minion(minion-01.example.com)的資訊
curl -k https://192.168.38.10:8000/minions/minion-01.example.com \
-H "Accept: application/x-yaml" \
-H "X-Auth-Token: 8e211da5d6bbb51fbffe6468a3ca0c6a24b3e535"
其中 X-Auth-Token 後邊的串為之前Login擷取到的Token串, 如果請求的URL不包含 minion-01.example.com ,則請求的為所有Minion的資訊
- minion-01.example.com:
cpu_flags:
- fpu
- vme
- de
......
job管理
擷取緩存的jobs清單
curl -k https://192.168.38.10:8000/jobs/ \
- '20131127065003726179':
Arguments: []
Function: test.ping
Start Time: 2013, Nov 27 06:50:03.726179
Target: '*'
Target-type: glob
User: sudo_vagrant
查詢指定的job
curl -k https://192.168.38.10:8000/jobs/20131127065003726179 \
- minion-01.example.com: true
遠端執行子產品
curl -k https://192.168.38.10:8000/ \
-H "X-Auth-Token: 8e211da5d6bbb51fbffe6468a3ca0c6a24b3e535" \
-d client='local' \
-d tgt='*' \
-d fun='test.ping'
也可以請求 https://192.168.38.10:8000/run ,不過該方法為一次性使用,無法使用Token, 隻能使用username和password
Response:
運作runner
-d client='runner' \
-d fun='manage.status'
- down: []
up:
- minion-01.example.com
運作wheel
注意: 由于目前版本的Salt Master有一處bug, 導緻wheel的結果無法傳回(https://groups.google.com/forum/#!topic/salt-users/9HcZ6R7MB0g),官方在最新的代碼中已經修複,使用時需要使用github中最新的salt代碼
Request(例子為查詢所有的minion key清單)
curl -k https://localhost:8000/ \
-d client='wheel' \
-d fun='key.list_all'
- data:
_stamp: 2013-12-23_04:54:22.483159
fun: wheel.key.list_all
jid: '20131223045422481844'
return:
local:
- master.pem
- master.pub
minions:
- minion-01.example.com
minions_pre: []
minions_rejected: []
success: true
tag: salt/wheel/20131223045422481844
user: pengyao
tag: salt/wheel/20131223045422481844
Targeting
謝謝 苦咖啡 提供
如果想在api中使用salt的 Targeting 功能,可以在Request的Post Data中增加 expr_form (預設是 glob )及值即可:
依然以curl為例:
-d tgt='webcluster' \
-d expr_form='nodegroup' \
将利用 nodegroup 比對到名為 webcluster 的target。
總結