Search code examples
salt-projectconfigurationmanager

How to fail the whole state.sls if one statement inside fails


I'm just started with saltstack, so could anyone help with this question: How to fail the whole state.sls if one statement inside fails? Are requisites

require/watch

suitable for this?


Solution

  • Well. You can specify that a certain state will only be applied if another state is successfully applied as well. Like this:

    vim:
      pkg.installed: []
    
    /etc/vimrc:
      file.managed:
        - source: salt://edit/vimrc
        - require:
          - pkg: vim
    

    The vimrc file will only be managed if the package vim is successfully is installed. It doesn't mater if vim was already installed or that it is a new installed package after this state. If the pkg: vim is successfully installed it will run the second state.

    This is useful if you have a particularly state that might fail. You can add the require to all other states to make sure they will only be applied if this particularly state is successful.

    To answer your question: It's not possible to make all states inside the state.sls fail if one of them fails. You can work around this issue by running your state.sls with salt '*' state.apply state test=True to see what would happen. If one of the states would fail you can decide to not actually apply the state.

    I hope this answers your question. If it's still unclear you might want to come up with an example :-)