python - saltstack - 在用 python 编写的 react 器内执行状态

标签 python salt-stack

我正在尝试用 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/

相关文章:

python - enumerate() 会生成生成器对象吗?

python - 如何在 python 中使用变量名访问数据文件中的变量值(观察值)

salt-stack - SaltStack gitfs 问题

jinja2 - 带有 curl 和 json 主体的 SaltStack cmd.run

Python - 我不知道如何在这段代码中工作

python - many2many 字段上的 Odoo 特定表单 View

python - 如何使用 Pandas 数据框更新 Postgres 表列?

postgresql - SLS 中的 ID 不是字典

python - 定位嵌套节点组失败并显示 "Minion did not return. [No response]",尽管主机响应如果通过其他标准定位

ssh - 使用 saltstack ssh