SaltStack Configuration Management

Get Started Tutorial

Manage Files

You'll learn how to:

  • Deliver files and folders to manages systems
  • Understand Salt file templates

Estimated time: 10 minutes 

Difficulty

Getting the correct files on a system is often more challenging than installing the correct software. Salt has a built-in file server that can deliver files and folders to managed systems.

salt://

You are familiar with placing Salt state files in the srv/salt directory, but what you might not know is that any other files and folders you place in this directory are also available to your Salt minions. You can reference these files in Salt states using the salt:// URL followed by the path to the file relative to the srv/salt directory.

File.Managed

This Salt state function lets you manage a local file by specifying the source file on the Salt master.

deploy the http.conf file:
  file.managed:
    - name: /etc/http/conf/http.conf
    - source: salt://apache/http.conf

Since the source path starts with salt://, we can determine that the source for this file is /srv/salt/apache/http.conf on our Salt master.

Each time this Salt state is applied, Salt makes sure that the local file matches the version on the server. This helps prevent configuration drift and helps make sure that applications are configured identically on different systems.

For example, if you want to distribute a global configuration file for lftp that includes a custom setting that limits download rates, we can do this using file.managed:

install network packages:
  pkg.installed:
    - pkgs:
      - rsync
      - lftp
      - curl

copy lftp config file:
  file.managed:
    - name: /etc/lftp.conf
    - source: salt://_tmpl_lftp.conf

Another option, since our configuration change is a single line, is to use file.append to simply insert the new line we want to add:

install network packages:
  pkg.installed:
    - pkgs:
      - rsync
      - lftp
      - curl

update lftp conf:
  file.append:
    - name: /etc/lftp.conf
    - text: set net:limit-rate 100000:500000

File.Recurse

This Salt state function copies an entire directory.

copy some files to the web server:
  file.recurse:
    - name: /var/www
    - source: salt://apache/www

Summary

You should now have a better understanding of SaltStack’s configuration management system, with enough knowledge to start writing your own Salt states. If you are looking for inspiration, the SaltStack Formula Repo contains many examples.