1

I want to execute this state:

backup_log_readable:
  acl.present:
    - name: /var/log/backup
    - acl_type: user
    - acl_name: monitor
    - perms: rx
    - recurse: True

But this should happen only if /var/log/backup exists.

How to do conditions like this in salt?

2 Answers 2

10

Alternatively to the onlyif: test -d /var/log/backup approach (that is probably the best one) you can:

1) Wrap everything with a jinja if:

{%- if salt['file.directory_exists' ]('/var/log/backup') %}
backup_log_readable:
  acl.present:
    - name: /var/log/backup
    - acl_type: user
    - acl_name: monitor
    - perms: rx
    - recurse: True
{%- endif %}

2) use the file.exists state as a requisite:

backup_log_exists:
  file.exists:
    - name: /var/log/backup

backup_log_readable:
  acl.present:
    - name: /var/log/backup
    - acl_type: user
    - acl_name: monitor
    - perms: rx
    - recurse: True
    - require:
      - file: backup_log_exists

This way you'll see a failed state in the summary, so it won't be totally transparent as with the onlyif and jinja approaches

3) create the directory before running the acl state:

backup_log_dir:
  file.directory:
    - name: /var/log/backup

backup_log_readable:
  acl.present:
    - name: /var/log/backup
    - acl_type: user
    - acl_name: monitor
    - perms: rx
    - recurse: True
    - require:
      - file: backup_log_dir
1
  • I took solution 1 ({%- if salt['file.directory_exists' ]('/var/log/backup') %}). Thank you for this answer!
    – guettli
    Nov 21, 2017 at 8:43
4

Onlyif should work:

- onlyif:
  - test -d /var/log/backup

See: https://docs.saltstack.com/en/latest/ref/states/requisites.html#onlyif

1
  • This works. Thank you. I am curious... is there a solution where no subprocess and no shell call is needed?
    – guettli
    Nov 17, 2017 at 22:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .