本文介绍了Salt Stack:在 SLS 中使用执行模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

就我在 Salt 文档中看到的而言(例如 此处) 支持两种主要类型的模块:状态模块执行模块(我知道还有渲染器、返回器等).大多数 SLS 文件示例仅包含与状态模块(在 salt.state 命名空间下)相关的语句,而对于执行模块,仅显示命令行示例.例如,我们有两个名为service"的模块:salt.states.service 和 salt.modules.service.现在我在 SLS 文件中使用执行模块时遇到问题,似乎它们根本不可用,或者我缺少一些东西来使它们可用.我的问题是:是否可以在 SLS 文件中使用执行模块,例如如何使用 salt.modules.service.restart 函数在 Ubuntu 机器上重新启动服务?我也不清楚这些模块类型之间的区别.

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're also renderers, returners and so on). Most of examples of SLS files contain statements related only to state modules (under 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.

我的服务名称是 selenium-node,我尝试了几种组合,但都失败了:

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

我在使用 git 状态和执行模块时遇到了同样的问题.但是,当我在 minion 上运行以下命令时(如文档中所示),它成功了:

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

推荐答案

执行模块状态模块之间存在显着差异是正确的.不幸的是,模块这个词有点过载了.

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

执行模块是发送到 Salt Minion 以立即执行的命令.例如安装 apache"或重新启动 memcached".

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

状态模块告诉 Salt Minion 最终结果或状态"应该是什么.示例是确保安装了 apache"或确保文件系统上存在此特定配置文件".重要的区别在于,状态模块会在做任何事情之前检查系统以查看机器是否符合所需的状态.因此,在确保安装了 apache"的情况下,Salt Minion 将检查是否安装了 Apache,如果安装了 Apache,则不执行任何操作.如果不明显,Salt 会在需要时安装 Apache.

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.

现在要像您在问题中指出的那样完成重新启动 selenium 节点,您需要让您的服务监视某些内容;通常是包更改和/或配置更改.这样 Selenium 只会在需要时重新启动.这是一个粗略的例子.我不熟悉 selenium-node 的安装,所以请考虑以下 sls 文件的示例.我假设 selenium-node 可以从您系统的软件包存储库中安装.

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

这里我们在selenium-node"ID声明下有3个状态.我们正在管理系统包、服务和配置文件.您会注意到该服务同时监视 selenium-node 包和配置文件.默认情况下,当服务正在监视某物时,该服务将在被监视"的事物报告更改时重新启动.

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.

这通常是您在使用 Salt States 时想要处理导致服务重启的方式.这样,服务只会在需要时重新启动.这有助于您的 Salt States 具有幂等性,并且仅在实际需要时才对您的系统进行更改.

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.

现在,回答您问题的第二部分.是的,可以从状态或 sls 文件中运行执行模块.您可以通过module.run"状态完成此操作.文档在这里:http://docs.saltstack.com/ref/states/all/salt.states.module.html#module-salt.states.module

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

这是每次运行此状态或 sls 文件时导致服务重新启动的方法:

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

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"

这篇关于Salt Stack:在 SLS 中使用执行模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 00:43