如何在SaltStack中粮食变化时触发反应器?

huangapple go评论41阅读模式
英文:

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[&#39;os_family&#39;] %}
{% if grains[&#39;os_family&#39;] != 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.

huangapple
  • 本文由 发表于 2023年6月27日 18:48:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76564094.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定