0

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?

5

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
  • I took solution 1 ({%- if salt['file.directory_exists' ]('/var/log/backup') %}). Thank you for this answer! – guettli Nov 21 '17 at 8:43
3

Onlyif should work:

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

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.