saltstack - 在用 python 编写的反应器中执行一个状态

saltstack - execute a state inside a reactor written in python

我正在尝试在 Python + Jinja 中创建一个反应器来对某些事件做出反应。该文档有关于如何使用 YAML 格式创建反应器的很好的示例。但是,在 Python 中创建反应器的文档非常缺乏。

这是我目前得到的:

#!jinja|py

"""
reactors can be also complete Python programs instead of Jinja + YAML
"""


def run():
    '''
    I can have fairly complex logic here, but it is all executed on the
    master. 
    '''
    # I can define variable like this
    var = "{{ data['id'] }}"  # this is the id of the minion that sent the signal

    # I can call a salt module like this:
    __salt__['pkg.install']('figlet')  # this will install figlet on the master 
    return {}

文档说明

The SLS file should contain a function called run which returns high state data.

但到目前为止,我还没有看到如何从这本词典中定位所需的随从。 我知道我可以使用 salt API,但我现在想避免这种情况。

这里有人可以举例说明如何通过返回正确的高状态数据来调用状态和目标以及 minion 吗?

事实证明,我访问上下文数据的方式是错误的。不需要在 Jinja 中包装 python 脚本。不幸的是,目前没有记录。 如果你在 Python 里面的所有内容中写一个状态或返回者

{{ data }} # in Jinja + YAML

现在可以使用名为(不足为奇)data 的全局变量。这个变量也是一个字典。

#!py

def run():
    '''
    '''
    # I can define variable like this
    var = data['id']   # this is the id of the minion that sent the signal

    return {}