1,修改配置文件
[root@study02 ~]# vim /etc/salt/master # The file server works on environments passed to the master, each environment # can have multiple root directories, the subdirectories in the multiple file # roots cannot match, otherwise the downloaded files will not be able to be # reliably ensured. A base environment is required to house the top file. # Example: # file_roots: # base: # - /srv/salt/ # dev: # - /srv/salt/dev/services # - /srv/salt/dev/states # prod: # - /srv/salt/prod/services # - /srv/salt/prod/states # file_roots: base: - /srv/salt
base:基础版 ---默认使用这个版本
dev:开发版
prod:产品版
2,创建目录
手工创建,配置文件配置的目录。
测试1:salt远程执行脚本
- 创建脚本
[root@study02 ~]# mkdir /srv/salt [root@study02 salt]# mkdir etc/script [root@study02 script]# vim test.sh [root@study02 script]# cat test.sh #!/bin/sh while true do sleep 1 echo 1 >> /tmp/1.log done
- 执行脚本
[root@study02 script]# salt 'study02' cmd.script salt://etc/script/test.sh
注意:由于这个脚本会一直执行,master在执行这个脚本的时候命令不会退出,需要ctrl+c退出。minion端会生成一个tmp***.sh的脚本,minion实际运行的是这个脚本。所以master退出后不影响minion的运行。
3,创建sls文件
创建top.sls
[root@study02 salt]# cat top.sls base: #与base,prod,dev 对应 'study02': #同步的minion_id - test #调用的sls文件名
创建test.sls
[root@study02 salt]# cat test.sls syncfile: #名称,可以自定义。如果不写name的话,这里需写成目标地址 file.managed: #file模块的managed方法 - source: salt://etc/file/passwd #原文件路径 - user: root #源文件属主 - group: root #源文件属组 - mode: 600 #源文件权限 - name: /tmp/passwd #同步目标地址
同步测试
salt 'study02' state.highstate 执行top.sls
salt 'study02' state.sls test 根目录
salt 'study02' state.sls sls.test 相对路径
salt 'study02' state.sls sls 默认init.sls
方式一:
[root@study02 salt]# salt 'study02' state.highstate study02: ---------- ID: syncfile Function: file.managed Name: /tmp/passwd Result: True Comment: File /tmp/passwd is in the correct state Started: 16:48:10.320748 Duration: 8.941 ms Changes: Summary ------------ Succeeded: 1 Failed: 0 ------------ Total states run: 1
方式二:
[root@study02 salt]# salt 'study02' state.sls test study02: ---------- ID: syncfile Function: file.managed Name: /tmp/passwd Result: True Comment: File /tmp/passwd updated Started: 16:55:38.798378 Duration: 14.808 ms Changes: ---------- diff: --- +++ @@ -1,4 +1,5 @@ 1111111111 +22222222222222222 root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin Summary ------------ Succeeded: 1 (changed=1) Failed: 0 ------------ Total states run: 1
方式三:将所有的sls文件放到一个目录
[root@study02 salt]# tree . ├── etc │ ├── file │ │ └── passwd │ └── script │ └── test.sh ├── sls │ └── test.sls ├── test.sls └── top.sls
执行同步命令
[root@study02 salt]# salt 'study02' state.sls sls.test study02: ---------- ID: syncfile Function: file.managed Name: /tmp/passwd Result: True Comment: File /tmp/passwd updated Started: 16:59:41.890034 Duration: 14.619 ms Changes: ---------- diff: --- +++ @@ -37,3 +37,4 @@ mysql:x:496:493:MySQL server:/var/lib/mysql:/bin/bash zabbix:x:495:492:Zabbix Monitoring System:/var/lib/zabbix:/sbin/nologin exim:x:93:93::/var/spool/exim:/sbin/nologin +333 Summary ------------ Succeeded: 1 (changed=1) Failed: 0 ------------ Total states run: 1
方式4:默认执行init.sls,程序会自动补充文件后缀(.sls)
[root@study02 salt]# tree . ├── etc │ ├── file │ │ └── passwd │ └── script │ └── test.sh ├── sls │ ├── init.sls │ └── test.sls ├── test.sls └── top.sls
init.sls功能查询主机名:
[root@study02 sls]# cat init.sls hostname: cmd.run
测试结果:
[root@study02 sls]# salt 'study02' state.sls sls study02: ---------- ID: hostname Function: cmd.run Result: True Comment: Command "hostname" run Started: 17:10:26.074874 Duration: 9.38 ms Changes: ---------- pid: 30307 retcode: 0 stderr: stdout: study02 Summary ------------ Succeeded: 1 (changed=1) Failed: 0 ------------ Total states run: 1