-
Notifications
You must be signed in to change notification settings - Fork 0
/
saltstack_api.sh
153 lines (118 loc) · 4.95 KB
/
saltstack_api.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/bin/bash
## SaltStack安装 2017-07-03
## http://www.aqzt.com
## email: [email protected]
## robert yu
## centos 7
##安装必备软件
yum -y install mariadb mariadb-devel mariadb-server wget epel-release python-devel gcc c++ make openssl openssl-devel passwd libffi libffi-devel
yum -y install salt-master salt-minion salt-api nginx
wget https://bootstrap.pypa.io/get-pip.py
python get-pip.py
##配置salt-api
pip install pyOpenSSL==0.15.1 -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
useradd -M -s /sbin/nologin saltapi && echo "password"|/usr/bin/passwd saltapi --stdin
salt-call --local tls.create_self_signed_cert
##配置salt-master 我这里把soms解压到了/data/wwwroot下
cat > /etc/salt/master <<EOF
interface: 0.0.0.0
external_auth:
pam:
saltapi:
- .*
- '@wheel'
- '@runner'
- '@jobs'
rest_cherrypy:
port: 8000
ssl_crt: /etc/pki/tls/certs/localhost.crt
ssl_key: /etc/pki/tls/certs/localhost.key
file_recv: True
include: /data/wwwroot/soms/saltconfig/*.conf
EOF
##配置好后,把服务启起来,并测试salt-api
systemctl start salt-master salt-api
curl -sSk https://localhost:8000/login -H 'Accept: application/x-yaml' -d username=saltapi -d password=password -d eauth=pam
################## 另一套配置 ####################################################################################
################################################################################################################
##安装salt api
yum -y install salt-api pyOpenSSL
chkconfig salt-api on
##创建用户,saltapi认证使用
useradd -M -s /sbin/nologin kbson
echo 'kbson' | passwd kbson --stdin
##添加salt api配置
[root@operation ops]# cat /etc/salt/master.d/api.conf
rest_cherrypy:
port: 8000
ssl_crt: /etc/pki/tls/certs/localhost.crt
ssl_key: /etc/pki/tls/certs/localhost.key
external_auth:
pam:
kbson:
- .*
- '@wheel'
- '@runner'
##生成自签名证书
[root@operation ops]# salt-call tls.create_self_signed_cert
local:
Certificate "localhost" already exists
##提示已经存在时,可以删除/etc/pki/tls/certs/localhost.crt /etc/pki/tls/certs/localhost.key重新生成
##获取token
[root@operation ops]# curl -k https://192.168.56.102:8000/login -H "Accept: application/x-yaml" -d username='kbson' -d password='kbson' -d eauth='pam'
return:
- eauth: pam
expire: 1480714218.787106
perms:
- .*
- '@wheel'
- '@runner'
start: 1480671018.787106
token: ab3749a9a0fe83386b8a5d558d10e346c252e336
user: kbson
##重启salt-api后token会改变
##执行models,test.ping测试minion连通性
[root@operation ops]# curl -k https://192.168.56.102:8000 -H "Accept: application/x-yaml" -H "X-Auth-Token: ab3749a9a0fe83386b8a5d558d10e346c252e336" -d client='local' -d tgt='*' -d fun='test.ping'
return:
- operation: true
##远程执行命令
[root@operation ops]# curl -k https://192.168.56.102:8000 -H "Accept: application/x-yaml" -H "X-Auth-Token: ab3749a9a0fe83386b8a5d558d10e346c252e336" -d client='local' -d tgt='*' -d fun='cmd.run' -d arg='free -m'
return:
- operation: ' total used free shared buffers cached
Mem: 988 932 56 1 19 107
-/+ buffers/cache: 805 182
Swap: 1983 382 1601'
远程执行多个minion命令
[root@operation ops]# curl -k https://192.168.56.102:8000 -H "Accept: application/x-yaml" -H "X-Auth-Token: ab3749a9a0fe83386b8a5d558d10e346c252e336" -d client='local' -d tgt='operation,slave01' -d expr_form='list' -d fun='cmd.run' -d arg='free -m'
return:
- operation: ' total used free shared buffers cached
Mem: 988 925 63 1 21 81
-/+ buffers/cache: 821 166
Swap: 1983 393 1590'
slave01: ' total used free shared buffers cached
Mem: 1870 622 1248 6 79 300
-/+ buffers/cache: 242 1628
Swap: 2047 0 2047'
[root@operation ops]#
执行wheel
查看minion key状态
[root@operation ops]# curl -k https://192.168.56.102:8000 -H "Accept: application/x-yaml" -H "X-Auth-Token: ab3749a9a0fe83386b8a5d558d10e346c252e336" -d client='wheel' -d fun='key.list_all'
return:
- data:
_stamp: '2016-12-02T09:30:35.235660'
fun: wheel.key.list_all
jid: '20161202173034905379'
return:
local:
- master.pem
- master.pub
minions:
- operation
- slave01
minions_denied: []
minions_pre: []
minions_rejected: []
success: true
tag: salt/wheel/20161202173034905379
user: kbson
tag: salt/wheel/20161202173034905379