Salt文件服务器

Salt内置一个简单的文件服务器用于分发文件给Salt minions. 文件服务器是一个构建于Salt master的无状态的ZeroMQ服务器。

Salt文件服务器的主要意图是使用在Salt state系统中展示文件的。这样看来,Salt文件服务器可以用于任何master到minions的通用文件传输。

CP模块

The cp module is the home of minion side file server operations. The cp module is used by the Salt state system, salt-cp, and can be used to distribute files presented by the Salt file server.

1 环境变量

Since the file server is made to work with the Salt state system, it supports environments. The environments are defined in the master config file and when referencing an environment the file specified will be based on the root directory of the environment.

file_roots

Default:

base:
  - /srv/salt
 

Salt runs a lightweight file server written in ZeroMQ to deliver files to minions. This file server is built into the master daemon and does not require a dedicated port.

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.

范例:

  base:
    - /srv/salt
  dev:
    - /srv/salt/dev/services
    - /srv/salt/dev/states
  prod:
    - /srv/salt/prod/services
    - /srv/salt/prod/states
 

 

注意:

/srv/salt 这个路径为salt的根目录,可以自定义。

当需要拷贝东西的时候,需要将文件放入到此目录下。

 

2 get_file

cp.get_file功能可以用于minion从master下载一个文件,该语法像这样:

# salt '*' cp.get_file salt://vimrc /etc/vimrc

该指令将会通知所有Salt minions下载vimrc文件并且拷贝到/etc/vimrc

eg: 

master:

[root@master /]# du -s /srv/salt/boot_bak.tar.gz              
48272    /srv/salt/boot_bak.tar.gz

[root@master /]# mv boot_bak.tar.gz  /srv/salt/

[root@master /]# salt '*' cp.get_file salt://boot_bak.tar.gz /tmp/boot.bak      
centos_minion_01:              
    /tmp/boot.bak

minion:

[root@minion1 tmp]# ll /tmp/boot.bak              
-rw-r--r--. 1 root root 49429171 Jan 14 07:54 /tmp/boot.bak

[root@minion1 tmp]# du -s /tmp/boot.bak              
48272    /tmp/boot.bak


 

模板渲染功能可以同时在源和目标文件启用,命名像这样:

# salt '*' cp.get_file "salt://`grains`.`os`/vimrc" /etc/vimrc template=jinja

该范例将会通知所有Salt minions下载vimrc从同名目录名下载,作为他们的OS grain并且拷贝到/etc/vimrc

大文件传输,可以使用gzip进行压缩,等级:1 -- 9:

For larger files, the cp.get_file module also supports gzip compression. Because gzip is CPU-intensive, this should only be used in scenarios where the compression ratio is very high (e.g. pretty-printed JSON or YAML files).

To use compression, use the gzip named argument. Valid values are integers from 1 to 9, where 1 is the lightest compression and 9 the heaviest. In other words, 1 uses the least CPU on the master (and minion), while 9 uses the most.

# salt '*' cp.get_file salt://vimrc /etc/vimrc gzip=5

makedirs :在文件夹不存在时,自动创建minion端文件夹

Finally, note that by default cp.get_file does not create new destination directories if they do not exist. To change this, use the makedirs argument:

# salt '*' cp.get_file salt://vimrc /etc/vim/vimrc makedirs=True

In this example, /etc/vim/ would be created if it didn't already exist.

 

3 get_dir

The cp.get_dir function can be used on the minion to download an entire directory from the master. The syntax is very similar to get_file:

# salt '*' cp.get_dir salt://etc/apache2 /etc

cp.get_dir supports template rendering and gzip compression arguments just like get_file:

# salt '*' cp.get_dir salt://etc/`pillar`.`webserver` /etc gzip=5 template=jinja
eg:

master:

[root@master salt]# du -s lib/              
242460    lib/

cp -a  /lib .

salt '*' cp.get_dir salt://lib  /tmp -v

minion:

[root@minion1 tmp]# du -s lib/              
242460    lib/



参考路径:
http://docs.saltstack.cn/zh_CN/latest/ref/file_server/index.html