Search code examples
ubuntu-14.04webhookssalt-project

How can I run state.sls from a reactor


TL;DR

I want to use the reactor, to call something similar to a simple salt '*' state.sls examplestate


I'm new(ish) to saltstack reactors, and I'm having issues with one of the features.

In the reactor documents located here, under "Advanced State System Capabilities", there is the following example:

/etc/salt/master.d/reactor.conf

# A custom event containing: {"foo": "Foo!", "bar: "bar*", "baz": "Baz!"}
reactor:
  - myco/custom/event:
    - /srv/reactor/some_event.sls

/srv/reactor/some_event.sls

invoke_orchestrate_file:
  runner.state.orchestrate:
    - mods: orch.do_complex_thing
    - pillar:
        event_tag: {{ tag }}
        event_data: {{ data | json() }}

/srv/salt/orch/do_complex_thing.sls

{% set tag = salt.pillar.get('event_tag') %}
{% set data = salt.pillar.get('event_data') %}

# Pass data from the event to a custom runner function.
# The function expects a 'foo' argument.
do_first_thing:
  salt.runner:
    - name: custom_runner.custom_function
    - foo: {{ data.foo }}

# Wait for the runner to finish then send an execution to minions.
# Forward some data from the event down to the minion's state run.
do_second_thing:
  salt.state:
    - tgt: {{ data.bar }}
    - sls:
      - do_thing_on_minion
    - pillar:
        baz: {{ data.baz }}
    - require:
      - salt: do_first_thing

In this example, assuming that I'm following it correctly, the reactor event sets off the some_event.sls located in the reactor directory. The some_event.sls then uses runner.state.orchestrate to run do_complex_thing.sls.

What I'm trying to do is very similar but I've been unable to make it work. I'd like to have the reactor event set off some_event.sls. In some_event.sls I'd just like it to call a state that I've written. For example, a simple state that uses file.managed to move a file from the master, to the minion. I've attempted this below:

/etc/salt/master.d/reactor.conf

reactor:
  - 'salt/netapi/hook/test':
    - /srv/reactor/testdirectory/configure.sls

/srv/reactor/testdirectory/configure.sls

{% set postdata = data.get('post', {}) %}
{% if grains['os_family']=="Debian" %}
testifthisworks:
  salt.state:
    - mods: transferfile.init
    - tgt: {{ postdata.tgt }}
{% endif %}

/srv/salt/transferfile/init.sls

/root/testfile.txt:
  file.managed:
    - source: salt://testfiles/testfile.txt
    - makedirs: True
    - mode: 700
    - template: jinja

In the configure.sls file, I'm trying to use salt.state to kick off the state.sls, this isn't working with the error "ReactWrap" object has no attribute salt

When I try to do the same thing, but using the runner.state.orchestrate from the original example (I dont need orchestrate), it works, but it moves the file to /root/ on my master.

I'm not sure what I can use other than salt.state to just run state.sls. Any help is appreciated.


Solution

  • I think you want this:

    /srv/reactor/testdirectory/configure.sls

    {% set postdata = data.get('post', {}) %}
    {% if grains['os_family']=="Debian" %}
    testifthisworks:
      local.state.sls:
        - tgt: {{ postdata.tgt }}
        - arg:
          - transferfile
    {% endif %}