Wojtas.Zet
Wojtas.Zet

Reputation: 816

how to execute sls rule and take conditional action?

I want to have saltstack sls rule that periodically executes bash command. Depending on the command output, I want to send notification.

I already have the sls rule, and it executes periodically as expected. I have as well configuration to send out notifications and it works with salt-run:

 salt-run mattermost.post_message message='Test'

Question is - how to make it work with .sls rule? I would appreciate some example.

Otherwise I can write some python script for this. But maybe salt already support this? Do I need reactor? https://docs.saltstack.com/en/latest/topics/reactor/

Upvotes: 0

Views: 538

Answers (2)

Utah_Dave
Utah_Dave

Reputation: 4581

If you want to send a message if the state fails you can use the onfail requisite to send your mattermost message.

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

needs-restarting -r:
  cmd.run: []

notify failure:
  module.run:
    - mattermost.post_message:
      - message: Reboot is required
    - onfail:
      - cmd: needs-restarting -r

Upvotes: 0

OrangeDog
OrangeDog

Reputation: 38777

If you want to periodically do something, then use the job scheduler.

If you want to do something in response to an asynchronous event, use the reactor system.

If you want to handle the result of a job in a particular way, use a different returner.

The state system is only for putting a system into a known state, not for running generic jobs. You can use a state to set up one of the above, however.

You can also write custom modules for all of the above, if the builtins are not sufficient.

It sounds like you want something like:

my-daily-check:
  schedule.present:
    - function: cmd.run
    - job_args: 
      - /path/to/script.sh
    - returner: mattermost
    - when: 6pm

Upvotes: 0

Related Questions