这里举几个简单的例子来记录一下sls文件的两种调用方式,仅用以记录。
1、分发/etc/hosts文件
1)准备sls文件
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
|
[root@salt-master base]
# vim hosts.sls
[root@salt-master base]
# cat hosts.sls
/etc/hosts
:
file
.managed:
-
source
: salt:
//testfiles/hosts
- user: root
- group: root
- mode: 644
[root@salt-master base]
# vi top.sls
[root@salt-master base]
# cat top.sls
base:
'salt-minion02.contoso.com'
:
- hosts
[root@salt-master base]
# mkdir testfiles
[root@salt-master base]
# cp /etc/hosts testfiles/
[root@salt-master base]
# ll
total 12
-rw-r--r-- 1 root root 116 Jun 5 14:59 hosts.sls
drwxr-xr-x 2 root root 4096 Jun 5 14:59 testfiles
-rw-r--r-- 1 root root 49 Jun 5 15:00
top
.sls
[root@salt-master base]
# tree
.
├── hosts.sls
├── testfiles
│ └── hosts
└──
top
.sls
1 directory, 3 files
|
2)使用state.highstate调用
注意:使用state.highstate调用的前提是存在top.sls文件,因此需要提前写好top.sls文件。
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
|
[root@salt-master base]
# salt 'salt-minion02.contoso.com' state.highstate
salt-minion02.contoso.com:
----------
ID:
/etc/hosts
Function:
file
.managed
Result: True
Comment: File
/etc/hosts
updated
Started: 15:01:50.762485
Duration: 14.93 ms
Changes:
----------
diff
:
---
+++
@@ -1,5 +1,5 @@
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
-192.168.49.100 salt-master salt-master.contoso.com
-192.168.49.101 salt-minion01 salt-minion01.contoso.com
-192.168.49.102 salt-minion02 salt-minion02.contoso.com
+192.168.49.100 salt-master salt-master.contoso.com
+192.168.49.101 salt-minion01 salt-minion01.contoso.com
+192.168.49.102 salt-minion02 salt-minion02.contoso.com
Summary
------------
Succeeded: 1 (changed=1)
Failed: 0
------------
Total states run:
|
1
3)使用state.sls调用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
[root@salt-master base]
# salt 'salt-minion02.contoso.com' state.sls hosts
salt-minion02.contoso.com:
----------
ID:
/etc/hosts
Function:
file
.managed
Result: True
Comment: File
/etc/hosts
is
in
the correct state
Started: 15:05:56.276074
Duration: 8.149 ms
Changes:
Summary
------------
Succeeded: 1
Failed: 0
------------
Total states run: 1
|
2、安装软件包
1)准备sls文件
1
2
3
4
5
6
7
8
9
10
11
12
13
|
[root@salt-master base]
# vi package.sls
[root@salt-master base]
# cat package.sls
pkg-
install
:
pkg.installed:
- names:
- gcc
- lrzsz
- tree
[root@salt-master base]
# vi top.sls
[root@salt-master base]
# cat top.sls
base:
'salt-minion02.contoso.com'
:
- package
|
2)使用state.highstate调用
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
|
[root@salt-master base]
# salt 'salt-minion02.contoso.com' state.highstate
salt-minion02.contoso.com:
----------
ID: pkg-
install
Function: pkg.installed
Name: gcc
Result: True
Comment: Package gcc is already installed.
Started: 17:07:14.548436
Duration: 887.609 ms
Changes:
----------
ID: pkg-
install
Function: pkg.installed
Name: tree
Result: True
Comment: Package tree is already installed.
Started: 17:07:15.436190
Duration: 0.419 ms
Changes:
----------
ID: pkg-
install
Function: pkg.installed
Name: lrzsz
Result: True
Comment: Package lrzsz is already installed.
Started: 17:07:15.436663
Duration: 0.234 ms
Changes:
Summary
------------
Succeeded: 3
Failed: 0
------------
Total states run: 3
|
3)使用state.sls调用
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
|
[root@salt-master base]
# salt 'salt-minion02.contoso.com' state.sls package
salt-minion02.contoso.com:
----------
ID: pkg-
install
Function: pkg.installed
Name: gcc
Result: True
Comment: Package gcc is already installed.
Started: 17:05:47.186879
Duration: 3979.297 ms
Changes:
----------
ID: pkg-
install
Function: pkg.installed
Name: tree
Result: True
Comment: Package tree is already installed.
Started: 17:05:51.166338
Duration: 0.406 ms
Changes:
----------
ID: pkg-
install
Function: pkg.installed
Name: lrzsz
Result: True
Comment: Package lrzsz is already installed.
Started: 17:05:51.166799
Duration: 0.271 ms
Changes:
Summary
------------
Succeeded: 3
Failed: 0
------------
Total states run: 3
|
3、管理定时任务
1)准备sls文件(添加定时任务)
1
2
3
4
5
6
7
|
[root@salt-master base]
# vi cron.sls
[root@salt-master base]
# cat cron.sls
/usr/sbin/ntpdate
210.72.145.44 64.147.116.229
time
.nist.gov >
/dev/null
2>&1:
cron
.present:
- identifier: SUPERCRON
- user: root
- minute:
'0'
|
2)使用state.sls调用
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
|
[root@salt-master base]
# salt 'salt-minion02.contoso.com' state.sls cron
salt-minion02.contoso.com:
----------
ID:
/usr/sbin/ntpdate
210.72.145.44 64.147.116.229
time
.nist.gov >
/dev/null
2>&1
Function:
cron
.present
Result: True
Comment: Cron
/usr/sbin/ntpdate
210.72.145.44 64.147.116.229
time
.nist.gov >
/dev/null
2>&1 added to root's
crontab
Started: 17:34:17.516341
Duration: 22.481 ms
Changes:
----------
root:
/usr/sbin/ntpdate
210.72.145.44 64.147.116.229
time
.nist.gov >
/dev/null
2>&1
Summary
------------
Succeeded: 1 (changed=1)
Failed: 0
------------
Total states run: 1
# 检查执行结果
[root@salt-master base]
# salt 'salt-minion02.contoso.com' cron.raw_cron root
salt-minion02.contoso.com:
0 * * * *
/usr/sbin/ntpdate
210.72.145.44 64.147.116.229
time
.nist.gov >
/dev/null
2>&1
# Lines below here are managed by Salt, do not edit
# SALT_CRON_IDENTIFIER:SUPERCRON
0 * * * *
/usr/sbin/ntpdate
210.72.145.44 64.147.116.229
time
.nist.gov >
/dev/null
2>&1
|
3)准备sls文件(删除定时任务)
1
2
3
4
5
6
7
8
9
10
11
12
|
[root@salt-master base]
# vi cron.sls
[root@salt-master base]
# cat cron.sls
/usr/sbin/ntpdate
210.72.145.44 64.147.116.229
time
.nist.gov >
/dev/null
2>&1:
cron
.absent:
- identifier: SUPERCRON
- user: root
- minute:
'0'
[root@salt-master base]
# vi top.sls
[root@salt-master base]
# cat top.sls
base:
'salt-minion02.contoso.com'
:
-
cron
|
4)使用state.highstate调用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
[root@salt-master base]
# salt 'salt-minion02.contoso.com' state.highstate
salt-minion02.contoso.com:
----------
ID:
/usr/sbin/ntpdate
210.72.145.44 64.147.116.229
time
.nist.gov >
/dev/null
2>&1
Function:
cron
.absent
Result: True
Comment: Cron
/usr/sbin/ntpdate
210.72.145.44 64.147.116.229
time
.nist.gov >
/dev/null
2>&1 removed from root's
crontab
Started: 17:46:18.360892
Duration: 24.8 ms
Changes:
----------
root:
/usr/sbin/ntpdate
210.72.145.44 64.147.116.229
time
.nist.gov >
/dev/null
2>&1
Summary
------------
Succeeded: 1 (changed=1)
Failed: 0
------------
Total states run: 1
# 检查执行结果
[root@salt-master base]
# salt 'salt-minion02.contoso.com' cron.raw_cron root
salt-minion02.contoso.com:
0 * * * *
/usr/sbin/ntpdate
210.72.145.44 64.147.116.229
time
.nist.gov >
/dev/null
2>&1
# Lines below here are managed by Salt, do not edit
|
本文转自 jerry1111111 51CTO博客,原文链接:http://blog.51cto.com/jerry12356/1933044,如需转载请自行联系原作者