英文:
How to trigger a reactor if there is a change in the grain in saltstack?
问题
使用情况 - 我有一个名为os_family
的粒度值,这个家族通过后端的自动化脚本进行更新,我想检测这个更改并在更改时触发自定义的反应器状态。
问题 - 我已经添加了反应器配置并编写了一个状态,但事件没有生成,似乎触发器不起作用。
我附上了我的代码文件如下:
/etc/salt/master/reactor.conf
reactor:
- 'os_family_changed':
- /srv/salt/reactor/os_family_changed.sls
/srv/salt/reactor/os_family_changed.sls:
{% set old_os_family = grains['os_family'] %}
{% if grains['os_family'] != old_os_family %}
my_grain_changed:
module.run:
- name: state.apply
- tgt: {{ data['id'] }}
- arg:
- test_os_family_changed.sls
{% endif %}
test_os_family_changed.sls:
test:
cmd.run:
- name: |
echo 'hi' >> /tmp/test
注意:我希望将这个用例扩展到将来能够从EC2 AWS元数据中读取粒度值。
我不确定如何实现这一点或者我漏掉了什么,任何帮助都将不胜感激。
英文:
Usecase- I have a grain value say os_family
and this family gets updated using some automated script in the backend, I want to detect this change and trigger a custom reactor state on this change.
Problem- I have added the reactor conf as well as written a state but the event is not being generated and it seems like the trigger is not working
I am attaching my code files below
/etc/salt/master/reactor.conf
reactor:
- 'os_family_changed':
- /srv/salt/reactor/os_family_changed.sls
/srv/salt/reactor/os_family_changed.sls:
{% set old_os_family = grains['os_family'] %}
{% if grains['os_family'] != old_os_family %}
my_grain_changed:
module.run:
- name: state.apply
- tgt: {{ data['id'] }}
- arg:
- test_os_family_changed.sls
{% endif %}
test_os_family_changed.sls:
test:
cmd.run:
- name: |
echo 'hi' >> /tmp/test
>NOTE: I want to extend this usecase to be able to read grains from EC2 AWS metadata in future.
I am not sure how to achieve this or what I am missing, any help would be appreciated.
答案1
得分: 1
{% set old_os_family = grains['os_family'] %}
{% if grains['os_family'] != old_os_family %}
这个检查永远不会为真,因为你将这两个值设置为相等。
在执行检查之后,你需要更新old_os_family
并将其存储在某个持久化的位置。
还要注意,grains不太可能改变,因此它们被大量缓存。可能需要多达几天才能看到变化。
<details>
<summary>英文:</summary>
```python
{% set old_os_family = grains['os_family'] %}
{% if grains['os_family'] != old_os_family %}
The check can never be true, because you set the two values to be equal.
You need to update old_os_family
and store it somewhere persistent after performing the check.
Note also that grains are not expected to change, and thus are heavily cached. You may not see a change for up to days.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论