SaltStack - Salt File Server


The Salt file server is a stateless ZeroMQ server. It is built into the Salt master. A Salt file server is used for distributing files from master to minions. It contains different modules. Let us understand the salt file server, its configuration, modules related to the salt file server, how to access the salt file server in python, etc., in this chapter.

File Server Backend

The file server backend allows the Salt file server to act as a transparent interface to other file server like a local file system, Git version control system, etc.

A Git file server backend can be enabled by using the following configuration in the master file.

fileserver_backend:
   - git

To enable multiple backend file system, we can use the following configuration.

fileserver_backend:
   - roots
   - git

We can also specify the additional option for a different backend server using the specific section of the corresponding backend server.

Local File System

For using this system, we have to use the following code.

file_roots:
   base:
      - /srv/salt/prod

Git File System

For using this system, we have to use the following code.

gitfs_remotes:
   - https://github.com/sample/sample1.git

Requesting Files

Salt has the option to request files for specific environments.

salt://path/to/file?saltenv = base

Here, the environment is defined using the roots option.

File Server Configuration

Salt files can be allocated within many root directories and accessed by specifying both the file path and the environment to search. The individual environments can span across multiple directory roots.

Environment

The default environment is base. This environment is defined and is used to download files when no other environment is specified.

file_roots:
   base:
      - /srv/salt/base

You can also use multiple environments as shown in the code below.

file_roots:
   base:
      - /srv/salt/base
   dev:
      - /srv/salt/dev
      - /srv/salt/base

CP Module

The CP module is the main module to manipulate the Salt file server. The salt-cp command can also be used to distribute files presented by the Salt file server.

GET_FILE

The cp.get_file function can be used on the minion to download a file from the master. It is defined as shown in the following code block.

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

The above command instructs all Salt minions to download the vimrc file and copy it to /etc/vimrc.

Enable Template

You can enable template option in get_file as follows −

salt '*' cp.get_file "salt://vimrc" /etc/vimrc template = jinja

Apply Compression

To use compression, use the gzip named argument. The valid values are integers from 1 to 9, where 1 is the minimum compression and 9 is maximum value.

The command is defined as follows −

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

GET_DIR

The cp.get_dir function can be used on the minion to download an entire directory from the master. It is defined in the following code block.

salt '*' cp.get_dir salt://etc/mysql /etc

The cp.get_dir supports template rendering and gzip compression arguments. If you want, you can assign as well.

FILECLIENT Module

Salt provides a python module that helps to access the salt file server. The salt/fileclient.py module is used to set up the communication from the minion to the master.

The sample code to get files is as follows −

import salt.minion
import salt.fileclient

def get_file(path, dest, saltenv = ‘base'):
   client = salt.fileclient.get_file_client(__opts__)
   return client.get_file(path, dest, true, saltenv)

Here,

  • opts is available when the module is run in the salt environment. Otherwise, we should provide the configuration path – /etc/salt/minion.

  • path refers to the path of the source file in salt file server.

  • dest refers the destination path of the file.

  • saltenv refers to the environment

In the next chapter, we will understand how to use Git as the file server.

Advertisements