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?

guettli
  • 3,113
  • 14
  • 59
  • 110

2 Answers2

8

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

Onlyif should work:

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

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

pyhimys
  • 1,267
  • 10
  • 10
  • 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