我想将多个现有的盐状态组合成一个新的,它们需要以特定的顺序执行。
SaltStackdocumentation解释了可以包含 salt states。据我了解,包含的状态将在 sls 文件的其余部分之前运行。例如:
include:
- config-pulled
- service-restarted
使用此示例,我希望service-restarted
在config-pulled
之后执行,并且仅在config-pulled
成功时执行。
但是不能保证多个包含状态的执行顺序。文档说:... If you need to guarantee order of execution, consider using requisites.
我可以想象直接在 include 上使用 requirements。例如:
include:
- config-pulled
- service-restarted:
require:
- config-pulled
但这不起作用。
Questions包括状态时如何使用必备条件?
我必须使用orchestrate script吗?
您不必使用 orch,您可以使用onchanges
和test.succeed_with_changes
。
在盐上测试3004.2
综上所述,onchanges
默认情况下禁止test.succeed_with_changes
的执行,除非给定状态(tests.config-pulled
)发生变化。onchanges_in
反过来也是如此,除非test.succeed_with_changes
被修改(在盐的意义上),否则禁止服务器重新加载。
例子:
/srv/salt
├── tests
│ ├── config-pulled.sls
│ ├── init.sls
│ └── service-restarted.sls
config-pulled.sls
config-pulled:
file.managed:
- name: /tmp/config
- contents: 1000
# uncomment random to simulate a change (or change 1000 just over)
# - contents: {{ range(1000, 9999) | random }}
service-restarted.sls
service-restarted:
cmd.run:
- name: echo service-restarted
init.sls
include:
- tests.config-pulled
- tests.service-restarted
demo:
test.succeed_with_changes:
- onchanges:
- sls: tests.config-pulled
- onchanges_in:
- sls: tests.service-restarted
这种方法可能变得有点难以维护。
一个完全不同的方法是 sls 的重组。我通常将服务器安装和重新加载与自定义分开(两个单独的 sls),因此当我必须处理配置时,我将“安装和重新加载”一个(通常是init.sls
)包含在任何 / 所有管理 conf 的 sls 之上,以导入状态并使用require_in
配置我的配置状态,例如:
myconf:
file.managed:
- [...]
- require_in:
- cmd: rndc-reload
或
myconf:
file.managed:
- [...]
- require_in:
- service: haproxy
注意:这种方法可以很好地扩展几个状态,甚至 sls 可以使用这种机制管理配置,并且在设置所有配置后,守护程序将仅重新加载一次。
更新
添加输出以显示特定顺序
无更改
# salt-call state.apply tests
local:
----------
ID: config-pulled
Function: file.managed
Name: /tmp/config
Result: True
Comment: File /tmp/config is in the correct state
Started: 14:49:20.363480
Duration: 15.688 ms
Changes:
----------
ID: demo
Function: test.succeed_with_changes
Result: True
Comment: State was not run because none of the onchanges reqs changed
Started: 14:49:20.380447
Duration: 0.004 ms
Changes:
----------
ID: service-restarted
Function: cmd.run
Name: echo service-restarted
Result: True
Comment: State was not run because none of the onchanges reqs changed
Started: 14:49:20.380536
Duration: 0.002 ms
Changes:
Summary for local
------------
Succeeded: 3
Failed: 0
------------
Total states run: 3
Total run time: 15.694 ms
有变化
# salt-call state.apply tests
local:
----------
ID: config-pulled
Function: file.managed
Name: /tmp/config
Result: True
Comment: File /tmp/config updated
Started: 14:49:15.757487
Duration: 18.292 ms
Changes:
----------
diff:
---
+++
@@ -1 +1 @@
-1001
+1002
----------
ID: demo
Function: test.succeed_with_changes
Result: True
Comment: Success!
Started: 14:49:15.777084
Duration: 0.549 ms
Changes:
----------
testing:
----------
new:
Something pretended to change
old:
Unchanged
----------
ID: service-restarted
Function: cmd.run
Name: echo service-restarted
Result: True
Comment: Command "echo service-restarted" run
Started: 14:49:15.777785
Duration: 7.69 ms
Changes:
----------
pid:
4130033
retcode:
0
stderr:
stdout:
service-restarted
Summary for local
------------
Succeeded: 3 (changed=3)
Failed: 0
------------
Total states run: 3
Total run time: 26.531 ms
includes 不是状态。因此,系统将无法使用它们。
至于你指向的票证,它变成了https://docs.saltproject.io/en/latest/ref/states/all/salt.states.test.html#salt.states.test.noptest.nop,它只是一个不做任何事情的状态。
处理你在说什么,你会做一些像
include:
- http
- libvirt
run_http:
test.nop:
- require:
- sls: http
run_libvirt:
test.nop:
- require:
- test: run_http
- sls: libvirt
本站系公益性非盈利分享网址,本文来自用户投稿,不代表码文网立场,如若转载,请注明出处
评论列表(32条)