首页 > 解决方案 > 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

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"?

标签: salt-stack

解决方案


引用必备文档:

必要目标的广义形式是<state name>: <ID or name>

如果我们破坏了您的my_state_idID:

  1. my_state_id是身份证
  2. pkg并且service是州名
  3. pkgstate 没有name参数,但servicestate 有,而且是y

由于pkgstate 没有name参数,我们需要使用它的 ID 来指定它:

  • 在左侧,我们将有pkg
  • 在右侧它将是 IDmy_state_id
  - require:
      - pkg: my_state_id

另一种写法是:

# give the package name in 'name' parameter

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

所以这是一种告诉 Saltstack 有条件地采取行动的方法。在这种情况下,如果包安装失败,那么它不应该尝试启动服务(并且失败)。


推荐阅读