官方文档 https://docs.saltstack.com/en/latest/topics/states/index.html

 

配置管理之SLS

Salt  State  SLS描述文件(YAML)

名称ID声明  默认是name声明

备注: 一个ID声明下面。状态模块不能重复使用

例:

apache-install:  
  pkg.installed:
    - names:
      - httpd
      - httpd-devel

apache-service:     # ID声明,高级状态,ID必须唯一。
  service.running:  # State声明 状态声明
    - name: httpd   # 选项声明
    - enable: True  

php:   
  pkg.installed

 

常用状态模块介绍

1)pkg  https://docs.saltstack.com/en/latest/ref/states/all/salt.states.pkg.html#module-salt.states.pkg

pkg.installed  # 安装
pkg.latest  # 确保最新版本
pkg.remove  # 卸载
pkg.purge  # 卸载并删除配置文件

# 同时安装多个包

common_packages:
  pkg.installed:
    - pkgs:
      - unzip
      - dos2unix
      - salt-minion: 2015.8.5-1.el6

 

2)file (https://docs.saltstack.com/en/latest/ref/states/all/salt.states.file.html#module-salt.states.file

salt:// 表示当前环境的根目录。例如:

SaltStack配置管理之状态模块和jinja2(五)

那么salt://lamp/files/httpd.conf  表示 /srv/salt/lamp/files/httpd.conf

 

3)service (https://docs.saltstack.com/en/latest/ref/states/all/salt.states.service.html#module-salt.states.service

redis:
  service.running:
    - enable: True  # 开机自启动 
    - reload: True  # 重载

 

LAMP架构slat实现安装、配置、启动

1.安装软件包 pkg

2.修改配置文件 file

3.启动服务 service

SaltStack配置管理之状态模块和jinja2(五)

lamp.sls文件内容如下

lamp-pkg:
  pkg.installed:
    - pkgs:
      - httpd
      - php
      - mariadb
      - mariadb-server
      - php-mysql
      - php-cli
      - php-mbstring

apache-config:
  file.managed:
    - name: /etc/httpd/conf/httpd.conf
    - source: salt://lamp/files/httpd.conf
    - user: root
    - group: root
    - mode: 644

php-config:
  file.managed:
    - name: /etc/php.ini
    - source: salt://lamp/files/php.ini
    - user: root
    - group: root
    - mode: 644

mysql-config:
  file.managed:
    - name: /etc/my.cnf
    - source: salt://lamp/files/my.cnf
    - user: root
    - group: root
    - mode: 644

apache-service:
  service.running:
    - name: httpd
    - enable: True
    - reload: True

mysql-service:
  service.running:
    - name: mariadb
    - enable: True
    - reload: True

命令: salt 'linux-node2*' state.sls lamp.lamp

执行结果

 1 linux-node2.example.com:
 2 ----------
 3           ID: lamp-pkg
 4     Function: pkg.installed
 5       Result: True
 6      Comment: 4 targeted packages were installed/updated.
 7               The following packages were already installed: httpd, mariadb-server, mariadb
 8      Started: 12:56:16.178765
 9     Duration: 194279.377 ms
10      Changes:   
11               ----------
12               libzip:
13                   ----------
14                   new:
15                       0.10.1-8.el7
16                   old:
17               php:
18                   ----------
19                   new:
20                       5.4.16-36.3.el7_2
21                   old:
22               php-cli:
23                   ----------
24                   new:
25                       5.4.16-36.3.el7_2
26                   old:
27               php-common:
28                   ----------
29                   new:
30                       5.4.16-36.3.el7_2
31                   old:
32               php-mbstring:
33                   ----------
34                   new:
35                       5.4.16-36.3.el7_2
36                   old:
37               php-mysql:
38                   ----------
39                   new:
40                       5.4.16-36.3.el7_2
41                   old:
42               php-pdo:
43                   ----------
44                   new:
45                       5.4.16-36.3.el7_2
46                   old:
47 ----------
48           ID: apache-config
49     Function: file.managed
50         Name: /etc/httpd/conf/httpd.conf
51       Result: True
52      Comment: File /etc/httpd/conf/httpd.conf is in the correct state
53      Started: 12:59:30.519583
54     Duration: 98.547 ms
55      Changes:   
56 ----------
57           ID: php-config
58     Function: file.managed
59         Name: /etc/php.ini
60       Result: True
61      Comment: File /etc/php.ini is in the correct state
62      Started: 12:59:30.620067
63     Duration: 36.824 ms
64      Changes:   
65 ----------
66           ID: mysql-config
67     Function: file.managed
68         Name: /etc/my.cnf
69       Result: True
70      Comment: File /etc/my.cnf is in the correct state
71      Started: 12:59:30.657074
72     Duration: 58.78 ms
73      Changes:   
74 ----------
75           ID: apache-service
76     Function: service.running
77         Name: httpd
78       Result: True
79      Comment: The service httpd is already running
80      Started: 12:59:30.853149
81     Duration: 40.481 ms
82      Changes:   
83 ----------
84           ID: mysql-service
85     Function: service.running
86         Name: mariadb
87       Result: True
88      Comment: The service mariadb is already running
89      Started: 12:59:30.893939
90     Duration: 33.928 ms
91      Changes:   
92 
93 Summary for linux-node2.example.com
94 ------------
95 Succeeded: 6 (changed=1)
96 Failed:    0
97 ------------
98 Total states run:     6
99 Total run time: 194.548 s
View Code

相关文章: