#salt的配置文件要遵守YAML格式;
#添加file root
[root@linux-node1 salt]# vim master
file_roots: base: - /srv/salt/base dev: - /ser/salt/dev test: - /srv/salt/test prod: - /srv/salt/prod
#手敲,不建议复制,注意空格不要用tab键
#创建相应的目录
[root@linux-node1 salt]# mkdir -p /srv/salt/{base,dev,test,prod}
[root@linux-node1 salt]# systemctl restart salt-master
#重启后检查服务是否正常
[root@linux-node1 salt]# salt '*' test.ping
linux-node2:
True
linux-node1:
True
#如果服务异常查看日志
[root@linux-node1 salt]# tailf /var/log/salt/master
#通过salt安装apache
#创建状态文件
[root@linux-node1 base]# vim apache.sls
apache-install: pkg.installed: - name: httpd apache-service: service.running: - name: httpd - enable: True
[root@linux-node1 base]# salt 'linux-node2' state.sls apache
#此时master端会等待返回结果,可以查看minion端信息(minion端会执行yum check-update 检查可更新的程序)
[root@linux-node1 base]# salt 'linux-node2' state.sls apache linux-node2: ---------- ID: apache-install Function: pkg.installed Name: httpd Result: True Comment: The following packages were installed/updated: httpd Started: 08:00:19.333442 Duration: 288520.895 ms Changes: ---------- apr: ---------- new: 1.4.8-5.el7 old: apr-util: ---------- new: 1.5.2-6.el7 old: httpd: ---------- new: 2.4.6-90.el7.centos old: httpd-tools: ---------- new: 2.4.6-90.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: 08:05:07.867717 Duration: 451.056 ms Changes: ---------- httpd: True Summary ------------ Succeeded: 2 (changed=2) Failed: 0 ------------ Total states run: 2
#上面是通过手动方式执行模块,salt可以通过编写模块对应文件执行高级状态执行相应 模块
#添加状态文件执行(top file放在base目录中)
[root@linux-node1 base]# vim top.sls
base: 'linux-node2': - web.apache 'linux-node1': - web.apache
#salt高级状态,系统会去找top.sls文件
#生产环境执行前先测试
[root@linux-node1 base]# salt '*' state.highstate test=True
[root@linux-node1 base]# salt '*' state.highstate
[root@linux-node1 base]# salt '*' state.highstate linux-node2: ---------- ID: apache-install Function: pkg.installed Name: httpd Result: True Comment: Package httpd is already installed. Started: 09:21:46.008792 Duration: 623.939 ms Changes: ---------- ID: apache-service Function: service.running Name: httpd Result: True Comment: Service httpd is already enabled, and is in the desired state Started: 09:21:46.633214 Duration: 151.824 ms Changes: Summary ------------ Succeeded: 2 Failed: 0 ------------ Total states run: 2 linux-node1: ---------- ID: apache-install Function: pkg.installed Name: httpd Result: True Comment: The following packages were installed/updated: httpd Started: 09:21:48.889559 Duration: 166699.047 ms Changes: ---------- apr: ---------- new: 1.4.8-5.el7 old: apr-util: ---------- new: 1.5.2-6.el7 old: httpd: ---------- new: 2.4.6-90.el7.centos old: httpd-tools: ---------- new: 2.4.6-90.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: 09:24:35.599222 Duration: 610.787 ms Changes: ---------- httpd: True Summary ------------ Succeeded: 2 (changed=2) Failed: 0 ------------ Total states run: 2