我正在尝试用 Python + Jinja 创建一个 react 器来对某些事件使用react。该文档提供了关于如何使用 YAML 格式创建 react 器的相当好的示例。然而,用Python创建 react 器的文档却相当缺乏。
这是我到目前为止得到的:
#!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 {}
关于python - saltstack - 在用 python 编写的 react 器内执行状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39978293/