码迷,mamicode.com
首页 > 其他好文 > 详细

saltstack学习-6:文件系统

时间:2018-01-06 20:05:47      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:es2017   direct   tac   文件名   gpo   require   分享   cee   远程   

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

saltstack学习-6:文件系统

标签:es2017   direct   tac   文件名   gpo   require   分享   cee   远程   

原文地址:https://www.cnblogs.com/snailshadow/p/8214963.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!