Search code examples
automationsalt-project

Require one state or another state with salt


I'm looking since several hours for a technic to write a salt state wich requires requirement1 or requirement2.

That's what I would like to do with cmd.run for example:

Run myscript:
  cmd.run:
    - name: /path/to/myscript
    - require: 
      - pkgs: pkg1|pkg2
    - cwd: /

Which means The script my_script will be executed if the state installing the package pkg1 or the state installing the pkg2 is satisfied.

Can this be really be done with salt? If so how?

I know I can make all my checks in my script directly, but I wanted to know if salt requisites could do that cleverly.

Thanks.


Solution

  • Salt states do not support branching if/else/while/ etc. The states file redered into a data which describes static dependency trees. Once rendered, the execution is performed in order.

    The only way to do some sort of branching is to use unless/onlyif keywords. But they only work with specific state types and do not accept state ids (AFAIK, they only accept executable shell commands). See: http://docs.saltstack.com/en/latest/ref/states/requisites.html#unless