SaltStack入门【配置管理案例】

经过前几篇文章的介绍,我们已经大致了解saltstack 基本情况和安装,接下来我们将开始使用saltstack实现自动化管理部署和配置管理,本文使用httpd包和配置文件作为配置管理的案例介绍。
如果还有些不清楚的可以阅读SaltStack入门【安装】这篇文章,可以帮你快速回忆。
现在开始:
我们现在已经将2台minion加入到master管理中

[root@master pillar]# salt-key -L
Accepted Keys:
test01
test02
Denied Keys:
Unaccepted Keys:
Rejected Keys:

1、文件结构如下:

/srv/salt
[root@tmaster salt]# tree
.
├── http.sls
├── servers
│   └── httpd.conf
├── servers.sls
└── top.sls

2、定义top.sls文件

[root@master salt]# cat top.sls 
base:
 '*':
  - servers
  - http

3、编辑servers.sls文件

[root@master salt]# cat servers.sls 
httpd:   #要求httpd的包是安装的。
 pkg:
  - installed

4、编辑 http.sls文件

[root@master salt]# cat http.sls 
httpd:
 service.running:
  - watch:    ##监控文件状态,如果发生更改,就重启httpd服务。
    - file: /etc/httpd/conf/httpd.conf    ##minion端文件位置

/etc/httpd/conf/httpd.conf:
 file.managed:   ##文件存储位置,/srv/salt/servers 下
  - source: salt://servers/httpd.conf

5、执行命令

salt '*' state.highstate 或 salt -N test state.highstate

这里提前补充一个nodegroup 分组嵌套知识:
定义两个组,然后把两个组放入第三个组

[root@master master.d]# cat /etc/salt/master.d/nodegroups.conf 
nodegroups:
 group01: 'test01'
 group02: 'test02'
 group: 'N@group01 or N@group02'

6、查看命令执行情况。

[root@master master.d]# salt -N test state.highstate
test01:
----------
          ID: httpd
    Function: pkg.installed
      Result: True
     Comment: Package httpd is already installed.
     Started: 20:20:37.402694
    Duration: 2172.684 ms
     Changes:   
----------
          ID: /etc/httpd/conf/httpd.conf
    Function: file.managed
      Result: True
     Comment: File /etc/httpd/conf/httpd.conf is in the correct state
     Started: 20:20:39.702113
    Duration: 6.017 ms
     Changes:   
----------
          ID: httpd
    Function: service.running
      Result: True
     Comment: The service httpd is already running
     Started: 20:20:39.712873
    Duration: 34.233 ms
     Changes:   

Summary
------------
Succeeded: 3
Failed:    0
------------
Total states run:     3
test02:
----------
          ID: httpd
    Function: pkg.installed
      Result: True
     Comment: Package sysstat is already installed.
     Started: 20:21:18.031887
    Duration: 2069.664 ms
     Changes:   
----------
          ID: /etc/httpd/conf/httpd.conf
    Function: file.managed
      Result: True
     Comment: File /etc/httpd/conf/httpd.conf is in the correct state
     Started: 20:21:20.107793
    Duration: 5.763 ms
     Changes:   
----------
          ID: httpd
    Function: service.running
      Result: True
     Comment: The service httpd is already running
     Started: 20:21:20.114140
    Duration: 32.244 ms
     Changes:   

Summary
------------
Succeeded: 3
Failed:    0
------------
Total states run:     3

发表评论