第1章 SaltStack配置管理
1.1 状态配置管理
1.1.1 使用状态管理
1.1.1.1 创建配置文件目录
[root@linux-node01 ~]# vim /etc/salt/master 664 file_roots: 665 base: # 系统初始化及所有机器公用的服务 666 - /srv/salt/base 667 dev: 668 - /srv/salt/dev 669 test: 670 - /srv/salt/test 671 prod: 672 - /srv/salt/prod [root@linux-node01 ~]# mkdir -p /srv/salt/{base,dev,test,prod} [root@linux-node01 ~]# systemctl restart salt-master
1.1.1.2 编辑状态配置文件
[root@linux-node01 ~]# vim /srv/salt/base/apache.sls # 需要已经安装apache的状态 apache-install: pkg.installed: - name: httpd # 需要apache服务的状态是运行的并且是开机自启动的 apache-servcie: service.running: - name: httpd - enable: True
1.1.1.3 按照配置文件执行
[root@linux-node01 ~]# salt 'linux-node02' state.sls apache linux-node02: ---------- ID: apache-install Function: pkg.installed Name: httpd Result: True Comment: The following packages were installed/updated: httpd Started: 11:57:46.580650 Duration: 33981.14 ms Changes: ---------- httpd: ---------- new: 2.4.6-93.el7.centos old: httpd-tools: ---------- new: 2.4.6-93.el7.centos old: mailcap: ---------- new: 2.1.41-2.el7 old: ---------- ID: apache-service Function: service.running Name: httpd Result: True Comment: Service httpd has been enabled, and is running Started: 11:58:20.572044 Duration: 5548.77 ms Changes: ---------- httpd: True Summary for linux-node02 ------------ Succeeded: 2 (changed=2) Failed: 0 ------------ Total states run: 2 Total run time: 39.530 s
- 命令解析:
1.1.2 多级目录时文件引用
[root@linux-node01 ~]# tree /srv/salt/ /srv/salt/ ├── base │ └── web │ └── apache.sls ├── dev ├── prod └── test [root@linux-node01 ~]# salt 'linux-node02' state.sls web.apache
1.1.3 高级状态管理
1.1.3.1 top.sls文件(类似docker中的编排文件)
[root@linux-node01 ~]# vim /etc/salt/master 555 ##### State System settings ##### 556 ########################################## 557 # The state system uses a "top" file to tell the minions what environment to 558 # use and what modules to use. The state_top file is defined relative to the 559 # root of the base environment as defined in "File Server settings" below. 560 #state_top: top.sls
1.1.3.2 编辑top.sls文件
[root@linux-node01 ~]# vim /srv/salt/base/top.sls base: 'linux-node01': - web.apache 'linux-node02': - web.apache
1.1.3.3 执行高级状态管理
[root@linux-node01 ~]# salt '*' state.highstate linux-node02: ---------- ID: apache-install Function: pkg.installed Name: httpd Result: True Comment: All specified packages are already installed Started: 12:19:52.904711 Duration: 1390.2 ms Changes: ---------- ID: apache-service Function: service.running Name: httpd Result: True Comment: The service httpd is already running Started: 12:19:54.296175 Duration: 73.898 ms Changes: Summary for linux-node02 ------------ Succeeded: 2 Failed: 0 ------------ Total states run: 2 Total run time: 1.464 s linux-node01: ---------- ID: apache-install Function: pkg.installed Name: httpd Result: True Comment: The following packages were installed/updated: httpd Started: 12:19:53.739561 Duration: 35016.125 ms Changes: ---------- httpd: ---------- new: 2.4.6-93.el7.centos old: httpd-tools: ---------- new: 2.4.6-93.el7.centos old: mailcap: ---------- new: 2.1.41-2.el7 old: ---------- ID: apache-service Function: service.running Name: httpd Result: True Comment: Service httpd has been enabled, and is running Started: 12:20:28.794078 Duration: 5471.892 ms Changes: ---------- httpd: True Summary for linux-node01 ------------ Succeeded: 2 (changed=2) Failed: 0 ------------ Total states run: 2 Total run time: 40.488 s
1.2 状态配置模块
备注:状态管理模块:https://docs.saltstack.com/en/latest/ref/states/all/index.html
1.2.1 状态模块pkg
1.2.1.1 功能
管理软件包状态
1.2.1.2 常用方法
- installed:确保软件包已经安装,如果没有安装则安装
- latest:确保软件包是最新版本,如果不是则进行升级
- remove:确保软件包已经卸载,如果之前已经安装则进行卸载
- purge:除remove外,也会删除其配置文件
1.2.2 状态模块file
1.2.2.1 功能
管理文件状态
1.2.2.2 常用方法
- managed:保证文件存在并且为对应的状态
- recurse:保证目录存在并且为对应的状态
- absent:确保文件不存在,如果存在则进行删除
- directory:确保有一个指定的目录,并且有正确的属性
1.2.3 状态模块service
1.2.3.1 功能
管理服务状态
1.2.3.2 常用方法
- running:确保服务处于运行状态,如果没有运行就启动服务
- enabled:确保服务开机自启动
- disabled:确保服务开机不自动启动
- dead:确保服务当前没有运行,如果运行则停止服务
1.2.4 状态模块requisites
1.2.4.1 功能
处理状态间关系
1.2.4.2 常用方法
- reuire:我依赖某个状态
- require_in:我被某个状态依赖
- watch:我关注某个状态
- watch_in:我被某个状态关注
1.2.5 状态模块cmd
1.2.5.1 功能
执行任意命令
1.2.5.2 常用方法
- run:如果满足某些情况则运行命令
- wait:仅当watch语句调用该命令时才运行该命令。
- script:下载一个脚本并使用指定的参数执行它。
1.2.6 状态模块user
1.2.6.1 功能
管理用户账户
1.2.6.2 常用方法
- present:确保指定的用户具有指定的属性,没有则创建用户
- absent:确保指定用户不在,有则删除用户
1.2.7 状态判断
- unless:如果条件为真(即$?=0)时,则不执行命令,可以用在所有命令后

我的微信
如果有技术上的问题可以扫一扫我的微信