1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
# vi /etc/salt/master #不用重启salt服务,修改后即生效
nodegroups:
web:
'L@host1'
#以列表形式写,多个主机以逗点分割
db:
'L@host2'
注:也可以使用通配符,如web:
'host*'
#测试分组后效果
# salt -N db test.ping
host2:
True
# salt -N web test.ping
host1:
True
|
1
2
|
# vi /etc/salt/minion
user: root
|
1
2
3
4
|
# vi /etc/salt/master
file_roots:
base:
-
/srv/salt/
#默认存放管理指令和文件位置
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
# mkdir /srv/salt
# cd /srv/salt
# vi top.sls #使用YAML语言格式定义
base:
#默认的起点配置
'*'
:
#匹配所有在线minion,指定组直接写组名
- web.httpd
#web是默认/srv/salt下目录,http是state文件名字,以sls结尾
----------------------------
如果想使用分组定义,如下:
base:
web:
#组名
- match: nodegroup
- web.httpd
----------------------------
# mkdir web
# vi web/httpd.sls #创建目录或文件名根据自己情况定义
httpd:
#说明
pkg:
#包管理
-name: apache2
#包名称
- installed
#包的状态,卸载是removed
|
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
|
# salt '*' state.sls web.httpd #只执行单个sls文件(/srv/salt/web/httpd.sls)
# salt '*' state.highstate #给minion永久添加状态,读top关联的sls文件
host1:
----------
ID: httpd
Function: pkg.installed
Name: apache2
Result: True
Comment: The following packages were installed
/updated
: apache2.
Started: 11:30:40.994840
Duration: 103211.119 ms
Changes:
省略.....
Summary
------------
Succeeded: 1 (changed=1)
Failed: 0
------------
Total states run: 1
host2:
----------
ID: httpd
Function: pkg.installed
Name: apache2
Result: True
Comment: The following packages were installed
/updated
: apache2.
Started: 11:30:41.458100
Duration: 122426.802 ms
Changes:
省略.....
Summary
------------
Succeeded: 1 (changed=1)
Failed: 0
------------
Total states run: 1
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
# vi httpd.sls
httpd:
pkg:
-name: apache2
-installed
service:
-name: apache2
#服务脚本名
-running
#运行状态
-reload: True
#如watch监控项发生变化,则重新加载配置
-
watch
:
#监控文件变化
-
file
:
/etc/apache2/httpd
.conf
/etc/apache2/httpd
.conf:
#分发到minion位置
file
.managed:
#文件管理操作
-
source
: salt:
//web/httpd
.conf
#修改好的配置文件位置
-user: root
-group: root
-mode: 644
|
1
|
# salt '*' state.sls web.httpd #再执行下
|
1
|
# salt '*' cmd.run 'cat /etc/apache2/httpd.conf'
|