用saltstack cp模块实现文件管理

本文介绍SaltStack中文件管理、分发及回滚的方法,包括如何利用cp模块实现文件上传下载,详解Salt master配置文件及minionFS的使用,并提供公钥分发实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
1.先看配置,2.配置,看日志3,日志不行,只能源代码了
 
 
基础篇
 
官网 http://salt.readthedocs.io/en/stable/topics/tutorials/minionfs.html#tutorial-minionfs
 
第一看卡壳 Saltstack的配置Salt master配置文件
http://blog.youkuaiyun.com/zhs2014150551/article/details/48951581
 
fileserver_backend:
    – roots
 
salt支持模块化的后端文件系统服务器,它允许salt通过第三方的系统来管理收集文件并提供给minions使用,可以配置多个后端文件系统,这里支持gitfs、hgfs、roots、s3fs文件调用的搜索顺序按照后台文件系统的配置顺序来搜索,默认的设置只开启了标准的后端服务器roots,具体的根选项配置通过file_roots参数设置
 
分发到minion
salt '*' cp.get_file  salt://test.sls /root/test.sls --log-level=all
 
上传到master
salt '*' cp.push  /root/install.log --log-level=all
上传的文件存放在master端的 /var/cache/salt/master/minions/<minion_id>/files/ 目录下
 
 
 
 
源代码解析
http://liuping0906.blog.51cto.com/2516248/1553252
 
def push(path):
    '''
    Push a file from the minion up to the master, the file will be saved to
    the salt master in the master's minion files cachedir
    (defaults to /var/cache/salt/master/minions/files)
    Since this feature allows a minion to push a file up to the master server
    it is disabled by default for security purposes. To enable add the option:
    file_recv: True
    to the master configuration and restart the master
    CLI Example::
        salt '*' cp.push /etc/fstab
    '''
    path=os.path.basename(path)
    if '../' in path or not os.path.isabs(path):
        return False
    if not os.path.isfile(path):
        return False
    auth = _auth()
    load = {'cmd''_file_recv',
            'id': __opts__['id'],
            'path': path.lstrip(os.sep)}
    sreq = salt.payload.SREQ(__opts__['master_uri'])
    with salt.utils.fopen(path) as fp_:
        while True:
            load['loc'= fp_.tell()
            load['data'= fp_.read(__opts__['file_buffer_size'])
            if not load['data']:
                return True
            ret = sreq.send('aes', auth.crypticle.dumps(load))
            if not ret:
                return ret
 
进阶
http://rfyiamcool.blog.51cto.com/1030776/1360468/
用saltstack cp模块实现文件管理、拉取和回滚下发
 
要实现的效果:
通过mongodb记录数据,然后web api接口渲染配置文件来的更方便。
 
web-api http://salt-api.readthedocs.io/en/latest/ref/netapis/all/saltapi.netapi.rest_cherrypy.html#a-rest-api-for-salt


cp.push 好像有点问题,老是返回false,不知道什么原因。 


。。。


1
2
3
4
5
6
7
8
9
10
11
12
13
MINIONFS master端配置
fileserver_backend:
- roots
- minion
file_recv: True
fileserver_backend在文件服务器那一部分讲过,roots是设置本地的文件服务器的目录环境,
同样的fileserver_backend可以将从minion端拉取过来的文件通过文件服务器发布出去,pull
到其他minion上。
以下有两个常用的方法:
1)查看master端有哪些可以推送的文件
    salt '*' cp.list_master_dirs
2)将某一minion下的文件或目录pull到其他minion上
    salt 'minion2' cp.get_file salt://minion1/opt/1.txt /opt/1.txt
1
2
3
4
5
6
7
8
9
10
11
使用minionFS进行公钥分发实例:
被连接方:minion-destination,存放公钥,存放在/root/.ssh/authorized_keys
连接方:minion-source,存放私钥,存放在/root/.ssh/id_rsa
1)在目标主机上创建密钥存放目录
    salt 'XX' file.mkdir dir_path=/root/.ssh user=root group=root mode=700
2)在minion-source主机上生成密钥对
    salt 'minion-source' cmd.run 'ssh-keygen -N "" -f /root/.ssh/id_rsa'
3)将minion-source上的公钥push到master端
    salt 'minion-source' cp.push /root/.ssh/id_rsa.pub
4)分发minion-source主机上的公钥到minion-destination主机上的指定目录
    salt 'minion-destination' cp.get_file salt://minion-source/root/.ssh/id_rsa.pub /root/.ssh/authorized_keys




本文转自 liqius 51CTO博客,原文链接:http://blog.51cto.com/szgb17/1961438,如需转载请自行联系原作者
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值