如何在状态改变时触发动作?

How to trigger an action upon change of state?

我正在测试 salt 作为管理系统,到目前为止使用 ansible

如何在状态发生变化时触发操作(特别是服务重新加载)?

在 Ansible 中,这是通过 notify 完成的,但是浏览 salt 文档我找不到任何类似的东西。

举个例子,以下场景在 salt 中的工作方式:检查 git 存储库(= 如果不存在则创建它,否则从中提取),如果它已更改,重新加载服务? Ansible 等价物是

- name: clone my service
  git:
    clone: yes
    dest: /opt/myservice
    repo: http://git.example.com/myservice.git
    version: master
    force: yes
  notify:
    - restart my service if needed

- name: restart my service if needed
  systemd:
    name: myservice
    state: restarted
    enabled: True
    daemon_reload: yes

你的例子:

ensure my service:
  git.latest:
    - name: http://git.example.com/myservice.git
    - target: /opt/myservice
  service.running:
    - watch:
      - git: http://git.example.com/myservice.git

回购什么时候会有变化(第一次克隆,更新等) 该状态将被标记为 "having changes" 因此依赖状态 - service.running 在这种情况下 - 将需要更改,因为 service 意味着重新启动

你问的内容在salt quickstart