pythonsalt-project

SaltStack custom state: How to download file from "salt:" URI


I'm trying to write a custom SaltStack state. It will be used like this:

Example of my.state usage:
  my.state:
    - name: /some/path
    - source: salt://path/to/dsl/file

This state is planned to grab a file from the Salt Master file server, transfer it to the minion, then execute the file using a program in the minion, analyze the output, delete the downloaded file, and return a proper state dict based on success/failure of the program that executes the DSL file.

My question:

Note: I do not envision the need to download the file from anywhere else but the Salt Master.

Edit: In the interest of full disclosure of facts, "success/failure" is gleaned by analyzing the output of the program, not a simple exitcode analysis. Just FYI.


Solution

  • The cp module is in charge of fetching files, as opposed to file, which is for managing local files.

    As you only want the file for working on intermediately, you'll want something like this:

    file_path = __salt__["cp.cache_file"](source)
    output = __salt__["cmd.run_stdout"](f"the-program '{file_path}'")