saltstack - execute a state inside a reactor written in python
<p>I am trying to create a reactor in Python + Jinja to react to some event. The documentation has fairly good examples on how to create reactors using the YAML format. However, the documentation for creating a reactor in Python is quite lacking. </p>

<p>Here is what I got so far:</p>

<pre><code>#!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 {}
</code></pre>

<p>The documentation states </p>

<blockquote>
  <p>The SLS file should contain a function called run which returns high state data.</p>
</blockquote>

<p>But so far I fail to see how I can target the desired minion from this dictionary. 
I know I can use the <a href="https://docs.saltstack.com/en/latest/ref/clients/index.html" rel="nofollow">salt API</a>, but I would like to avoid this at the moment.</p>

<p>Can someone here give an example how you can call a state and target and a minion by returning the correct high state data?</p>

<p>So, it turns out, my way of accessing the context data was wrong. There is no need to wrap the python script in Jinja. Unfortunately this is undocumented at the moment.
If you write a state or a returner in Python everything that was inside</p>

<pre><code>{{ data }} # in Jinja + YAML
</code></pre>

<p>Is now available with a global variable called (not suprisingly) <code>data</code>. This variable is a dictionary  too. </p>

<pre><code>#!py

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

    return {}
</code></pre>

python salt-stack