'check if file exists in salt file system salt://

I would like to check if a file exist in salt file system (salt://) and add an instruction depending on that. I precise that I use gitfs as a mount point in the salt file system and don't use /srv directory.

So more concretely I want to do something like that :

{% if salt['file.directory_exists']('salt://a_directory') %}
 file.recurse:
    - name: dest
    - source: salt://a_directory
    - template: jinja
{% endif %}

but it seem not to work.



Solution 1:[1]

I wanted to load yaml-files with package-lists for pkg.installed, but only if the yaml is on the master (file names are constructed by variables).

I'm using salt.modules.cp.list_master:

# Search path on the master:
{% set found = salt.cp.list_master(prefix='my/path/example.yaml') | count %}
{% if found %}

{# Do something… #}

{% endif %}

If you want to check a Directory, you can use this:

{% set found = salt.cp.list_master_dirs(prefix='my/path') | count %}

Solution 2:[2]

after seeing Mayr's answer, I did this:

{%- if not salt.cp.list_master(saltenv=saltenv, prefix='some/path/' ~ some_version ~ '/server.conf') | count %}
config_not_found_failure:
  test.fail_without_changes:
    - name: "some/path/{{some_version}}/server.conf not found in salt"
    - failhard: True
{%- endif %}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2