5

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?

2 Answers2

1

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 
moestly
  • 1,681
  • 15
  • 19
-1

I have a similar situation in my SLS file:

ipa-client-automount:
  cmd.run:
    - names: 
      {% if salt['cmd.run']('hostname -f | grep domain1') %}
      - ipa-client-automount --location=domain1 -U
      {% elif salt['cmd.run']('hostname -f | grep domain2') %}
      - ipa-client-automount --location=domain2 -U
      {% endif %}
    - unless: ipa-client-automount 2>&1 | grep already

You can do the same structure but in the condition grep the status of the service:

{% if salt['cmd.run']('systemctl status mysql | grep active'%}
Kevin Lemaire
  • 949
  • 2
  • 12
  • 36