salt.pillar.file_tree

Recursively iterate over directories and add all files as Pillar data

New in version 2015.5.0.

Example Configuration

ext_pillar:
  - file_tree:
      root_dir: /path/to/root/directory
      follow_dir_links: False
      keep_newline: True

The root_dir parameter is required and points to the directory where files for each host are stored. The follow_dir_links parameter is optional and defaults to False. If follow_dir_links is set to True, this external pillar will follow symbolic links to other directories.

Warning

Be careful when using follow_dir_links, as a recursive symlink chain will result in unexpected results.

If keep_newline is set to True, then the pillar values for files ending in newlines will keep that newline. The default behavior is to remove the end-of-file newline. keep_newline should be turned on if the pillar data is intended to be used to deploy a file using contents_pillar with a file.managed state.

Changed in version 2015.8.4: The raw_data parameter has been renamed to keep_newline. In earlier releases, raw_data must be used. Also, this parameter can now be a list of globs, allowing for more granular control over which pillar values keep their end-of-file newline. The globs match paths relative to the directories named for minion IDs and nodegroups underneath the root_dir (see the layout examples in the below sections).

ext_pillar:
  - file_tree:
      root_dir: /path/to/root/directory
      keep_newline:
        - files/testdir/*

Note

In earlier releases, this documentation incorrectly stated that binary files would not affected by the keep_newline configuration. However, this module does not actually distinguish between binary and text files.

Assigning Pillar Data to Individual Hosts

To configure pillar data for each host, this external pillar will recursively iterate over root_dir/hosts/id (where id is a minion ID), and compile pillar data with each subdirectory as a dictionary key and each file as a value.

For example, the following root_dir tree:

./hosts/
./hosts/test-host/
./hosts/test-host/files/
./hosts/test-host/files/testdir/
./hosts/test-host/files/testdir/file1.txt
./hosts/test-host/files/testdir/file2.txt
./hosts/test-host/files/another-testdir/
./hosts/test-host/files/another-testdir/symlink-to-file1.txt

will result in the following pillar tree for minion with ID test-host:

test-host:
    ----------
    files:
        ----------
        another-testdir:
            ----------
            symlink-to-file1.txt:
                Contents of file #1.

        testdir:
            ----------
            file1.txt:
                Contents of file #1.

            file2.txt:
                Contents of file #2.

Note

Subdirectories underneath root_dir/hosts/id become nested dictionaries, as shown above.

Assigning Pillar Data to Entire Nodegroups

To assign Pillar data to all minions in a given nodegroup, this external pillar recursively iterates over root_dir/nodegroups/nodegroup (where nodegroup is the name of a nodegroup), and like for individual hosts, compiles pillar data with each subdirectory as a dictionary key and each file as a value.

Important

If the same Pillar key is set for a minion both by nodegroup and by individual host, then the value set for the individual host will take precedence.

For example, the following root_dir tree:

./nodegroups/
./nodegroups/test-group/
./nodegroups/test-group/files/
./nodegroups/test-group/files/testdir/
./nodegroups/test-group/files/testdir/file1.txt
./nodegroups/test-group/files/testdir/file2.txt
./nodegroups/test-group/files/another-testdir/
./nodegroups/test-group/files/another-testdir/symlink-to-file1.txt

will result in the following pillar data for minions in the node group test-group:

test-host:
    ----------
    files:
        ----------
        another-testdir:
            ----------
            symlink-to-file1.txt:
                Contents of file #1.

        testdir:
            ----------
            file1.txt:
                Contents of file #1.

            file2.txt:
                Contents of file #2.
salt.pillar.file_tree.ext_pillar(minion_id, pillar, root_dir=None, follow_dir_links=False, debug=False, raw_data=None, keep_newline=False)

Compile pillar data for the specified minion ID