saltstack sls调用方式

简介:

这里举几个简单的例子来记录一下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,如需转载请自行联系原作者
相关实践学习
日志服务之使用Nginx模式采集日志
本文介绍如何通过日志服务控制台创建Nginx模式的Logtail配置快速采集Nginx日志并进行多维度分析。
相关文章
|
NoSQL Ubuntu 网络安全
|
NoSQL Ubuntu 网络安全
|
应用服务中间件 nginx PHP
|
网络协议 开发工具
|
5天前
|
C++
JNI Log 日志输出
JNI Log 日志输出
13 1
|
5天前
|
存储 运维 大数据
聊聊日志硬扫描,阿里 Log Scan 的设计与实践
泛日志(Log/Trace/Metric)是大数据的重要组成,伴随着每一年业务峰值的新脉冲,日志数据量在快速增长。同时,业务数字化运营、软件可观测性等浪潮又在对日志的存储、计算提出更高的要求。
|
11天前
|
XML Java Maven
Springboot整合与使用log4j2日志框架【详解版】
该文介绍了如何在Spring Boot中切换默认的LogBack日志系统至Log4j2。首先,需要在Maven依赖中排除`spring-boot-starter-logging`并引入`spring-boot-starter-log4j2`。其次,创建`log4j2-spring.xml`配置文件放在`src/main/resources`下,配置包括控制台和文件的日志输出、日志格式和文件切分策略。此外,可通过在不同环境的`application.yml`中指定不同的log4j2配置文件。最后,文章提到通过示例代码解释了日志格式中的各种占位符含义。
|
11天前
|
运维 监控 Go
Golang深入浅出之-Go语言中的日志记录:log与logrus库
【4月更文挑战第27天】本文比较了Go语言中标准库`log`与第三方库`logrus`的日志功能。`log`简单但不支持日志级别配置和多样化格式,而`logrus`提供更丰富的功能,如日志级别控制、自定义格式和钩子。文章指出了使用`logrus`时可能遇到的问题,如全局logger滥用、日志级别设置不当和过度依赖字段,并给出了避免错误的建议,强调理解日志级别、合理利用结构化日志、模块化日志管理和定期审查日志配置的重要性。通过这些实践,开发者能提高应用监控和故障排查能力。
87 1