28

So far as I can see in the Salt documentation (e.g. here) there are two main types of modules supported: state modules and execution modules (I know there are also renderers, returners, and so on). Most of examples of SLS files contain statements related only to state modules (under the salt.state namespace) whereas for execution modules only command line examples are shown. For example, we have two modules named "service": salt.states.service and salt.modules.service. Right now I have problems using execution modules in SLS files and it seems that either they're not available at all or I'm missing something to make them available. My question is: is it possible to use execution modules in SLS files and how ,for example, can I restart a service on Ubuntu machine using salt.modules.service.restart function? Also I don't clearly get the difference between these modules types.

My service name is selenium-node and I tried several combinations and all of them failed:

# First attempt:
selenium-node:
  service.restart

# Another one:
service:
  - restart
  - name: selenium-node

# Or even:
selenium-node:
  service:
    - restart

I faced the same issue when working with git state and execution modules. However when I run the following command on the minion (as shown in the documentation) it succeeds:

sudo salt-call service.restart selenium-node

1 Answer 1

53

You are correct that there are significant differences between execution modules and state modules. Unfortunately the term module is a bit overloaded.

An execution module is a command sent to a Salt Minion to be executed immediately. Examples are "install apache" or "restart memcached".

A state module tells the Salt Minion what the end result, or "state" should be. Examples would be "make sure apache is installed" or "make sure this specific config file exists on the filesystem". The important difference is that a state module will check the system to see if the machine conforms with the desired state before doing anything. So in the case of "make sure apache is installed" the Salt Minion will check to see if Apache is installed and do nothing if Apache is installed. If it's not obvious, Salt will install Apache if needed.

Now to accomplish restarting the selenium-node like you noted in your question you'll want to have your service watch something; usually a package change and/or config change. That way Selenium will only restart when needed. Here's a rough example. I'm not familiar with installation of selenium-node so please consider the following sls file an example. I'm assuming selenium-node can be installed from your system's package repo.

cat /srv/salt/selenium-node.sls

selenium-node:
  pkg:
    - installed
  service:
    - running
    - watch:
      - pkg: selenium-node
      - file: /etc/selenium-node.conf
  file:
    - managed
    - name: /etc/selenium-node.conf
    - source: salt://selenium/selenium-node.conf # assuming config located at /srv/salt/selenium/selenium-node.conf  on the Salt Master

Here we have 3 states under the "selenium-node" ID Declaration. We're managing the system package, the service and a config file. You'll notice that the service is watching both the selenium-node package and the config file. When a service is watching something the service will restart, by default, when the "watched" thing reports a change.

This is generally how you want to handle causing a service restart when using Salt States. This way the service will only get restarted when needed. This helps your Salt States be idempotent and only cause changes to your system when actually needed.

Now, to answer the second part of your question. Yes, it is possible to run an execution module from within a state or sls file. You can accomplish this through the "module.run" state. Docs are here: http://docs.saltstack.com/ref/states/all/salt.states.module.html#module-salt.states.module

Here's how you would cause your service to restart every time you run this state or sls file:

cat /srv/salt/selenium/selenium-restart.sls

restart_selenium:
  module.run:
    - name: service.restart
    - m_name: selenium-node   # m_name gets passed to the execution module as "name"
9
  • Thank you for a quick reply. What is not clear without reading the entire Salt Modules documentation is that any execution module should be wrapped with module.run instruction in the SLS file. The rest is clear. Probably I missed something. Hope this question helps somebody.
    – vania-pooh
    Dec 26, 2013 at 19:36
  • 1
    I agree that it's not clear. Generally, you'll want to use an existing state module or create a state module, if possible, instead of using module.run.
    – Utah_Dave
    Jan 6, 2014 at 16:29
  • Is there a way to run multiple execution module in a single state
    – Akshay
    Feb 2, 2015 at 22:35
  • 1
    Akshay, you can create an sls file that can contain as many execution module calls as you'd like.
    – Utah_Dave
    Feb 6, 2015 at 16:33
  • 1
    @DanCat, I wouldn't consider it a bad practice, but more of a workaround. It's always preferable to use a state module in a state, but sometimes only an execution module exists to do what you want to do.
    – Utah_Dave
    Nov 22, 2016 at 0:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.