SaltStack: How one can execute a state only once?

Solution 1:

You can set a grain on the minion that indicates if the MySQL-State has run before. Just add this to your State:

mysql_master_status:
  mystate.query:
    - query: SHOW MASTER STATUS
  grains.present:
    - name: mysql
    - value: master

Then you can wrap things in your State you only want to run once between an if Statement:

{% if salt['grains.get']('mysql') != 'master' %}
...
{% endif %}

The advantage of this method is that you can now also keep track of your Minions who are MySQL-Masters. The downside is that you have to keep an eye on your grains so that the information won't get lost.

Solution 2:

I think the easiest way would be to put this state into another state file (mysql_master_status.sls) and not referencing it in the top.sls. Instead you execute this state once using salt 'target' state.sls mysql_master_status

Thomas