Search code examples
salt-project

Salt: install package and run service (name different than package)


Currently trying to wrap my head around Salt.

In essence I'd like to install a package (rpm) and enable and run the service (if the package installed successfully).

Surprise: The service is called differently than the package.

Let's say

  • the package is called x
  • but the (systemd/init) service this package installs is called y

This does not work

my_state_id:
  pkg.installed:
    - pkgs:
      - x
  service.running:
    - name: y
    - enable: true
    - require:
      - pkg: x

Result:

Comment: The following requisites were not found:
                             require:
                                 pkg: x

It looks like I have to write it like this and reference the state and not the package:

my_state_id:
  pkg.installed:
    - pkgs:
      - x
  service.running:
    - name: y
    - enable: true
    - require:
      - pkg: my_state_id

But: What does require: pkg: my_state_id mean? =D "if the state up to this point didn't fail, then run the current module"?


Solution

  • Quoting from the requisites documentation:

    The generalized form of a requisite target is <state name>: <ID or name>.

    If we break up your my_state_id ID:

    1. my_state_id is the ID
    2. pkg and service are the state names
    3. pkg state does not have a name parameter, but service state has it, and it is y

    Since pkg state does not have name parameter, we need to use its ID to specify it as requisite:

    • On the left side we will have pkg
    • On the right side it will be the ID my_state_id
      - require:
          - pkg: my_state_id
    

    The other way to write the same would be:

    # give the package name in 'name' parameter
    
    my_state_id:
      pkg.installed:
        - name: x
      service.running:
        - name: y
        - enable: true
        - require:
            - pkg: x
    

    So it is a way to tell Saltstack to take actions conditionally. In this case if package install failed, then it should not try to start the service (and fail).