Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional Salt State

Tags:

salt-stack

I have a scenario where I need to take an action if another service is already running. Specifically, I want to install snmp monitoring if, for example, mysql is already running.

I know the "right" way to do this is to install mysql and its monitoring both based on pillar data, grain data, or some other top file filtering. However, in this scenario, mysql is being installed outside of configuration management (eg, an MSP has clients that install mysql but then rely on the hosting provider to configure monitoring).

What are the best practices in this situation?

Some solutions I've thought of:

  1. Create a custom grain that lists services that are running.
  2. Use the unless/only if (and a map file for different the OS distributions)
  3. A Beacon (stating that the service is running) and a reactor (to deploy)
  4. Calling the service.status execution module in jinja in the state file, such as:

{% set mysqlrunning = salt['service.status'](mysql_service) %} {% if mysqlrunning %} <rest of state file> {% endif %}

While #4 seems simple enough, I am afraid it will be slow and use a lot of system resources in a large deployment (1000's of servers).

What are the best practices in this situation?

like image 382
Solomon Gifford Avatar asked Nov 18 '22 02:11

Solomon Gifford


1 Answers

Usually one should be easy on the jinja in states, but use jinja maps.

In your example case, I would use require

mysqld:
  service.running

insert_sql:
  cmd.run:
    - name: mysql < /tmp/src.sql
    - require:
      - service: mysqld 
like image 76
moestly Avatar answered Jan 03 '23 01:01

moestly