Andrew
Andrew

Reputation: 4418

How do I implement optional requisites?

I have two .sls state files. One sets up a system service. The other formats and mounts storage volumes.

The system service doesn't need to know or care about its underlying storage; it just writes data to a given directory. Likewise, the state that does the volume setup doesn't need to know or care what's using said volume, and will be reused in other contexts. It seems inappropriate for either .sls to include the other. However, if the intended data directory happens to be a mounted volume, I need to make sure that the mount happens before the service state runs.

The problem: If neither file includes the other, I don't think I can use require or require_in. I can use order: 0 in the mounting state, and that will ensure the mount goes first, but that doesn't express a dependency; if the mount state fails, the service will still try to run.

What's the Right Way to handle this case? My aim is to create a requisite if and only if it is relevant, without either sls being dependent on the other's implementation details (or even existence).

Upvotes: 0

Views: 37

Answers (1)

daks
daks

Reputation: 883

I can imagine two possibilities:

  • use a wrapper state "data-with-mount" with include, in the correct order, the two needed states, which is only called when there is this double need
  • use a parameter (in pillar) like "data-with-mount: true" with you can then use in you state to include or not the require, something like
[...]
  {% if salt['pillar.get']('data-with-mount', False) %}
  require:
    - sls: format-and-mount
  {% endif %}

Not sure it completely suits your case but based on the information given I think it can be done that way.

Upvotes: 1

Related Questions