R1eppe
R1eppe

Reputation: 11

Saltstack sending event during state apply

I'm trying to register my minion in a central hosts file and then push this hosts file to all my connected minions.

Here is what I have in mind:

  1. Minion send an event 'register' with its ip and hostname to be registered on the master's central hosts file
  2. Master listening to the event 'register' and react with the reactor /srv/reactor/register.sls
  3. Reactor calls the state /srv/salt/register.sls on the minion installed on the master's host to modify the central file and send an event 'hosts_modified' after the modification is complete
  4. Master listening to the event 'hosts_modified' and react with the reactor /srv/reactor/deploy_hosts.sls which applies the state /srv/salt/hosts.sls on all connected minions to push the new modified hosts file

The first 3 steps are working fine but the master is not reacting to the last event 'hosts_modified'.

Command to initiate the register on the minion:

salt-call event.send register minion_host=somehostname minion_ip=1.1.1.1

Master reactor config (/etc/salt/master.d/reactor.conf):

reactor:
  - salt/beacon/*/inotify//etc/hosts:
    - /srv/reactor/revert.sls
  - 'deployment':
    - /srv/reactor/deployment.sls
  - 'register':
    - /srv/reactor/register.sls
  - 'hosts_modified':
    - /srv/reactor/deploy_hosts.sls

/srv/reactor/register.sls

{% set forwarded_data = data.data %}
test:
  local.state.sls:
    - tgt: 'master'
    - args:
      - mods: register
      - pillar:
          forwarded_data: {{ forwarded_data | json() }}

/srv/salt/register.sls

{% set data = salt.pillar.get('forwarded_data') %}

add_host:
   cmd.run:
     - name: /srv/scripts/hosts-manage.sh {{ data.minion_ip }} {{ data.minion_host }}

event_host_modified:
   event.send:
     - name: hosts_modified
     - require:
       - cmd: add_host

/srv/reactor/deploy_hosts.sls

deploy_hosts:
  local.state.sls:
    - tgt: '*'
    - name: hosts

/srv/salt/hosts.sls

# Hosts file management
/etc/hosts:
  file.managed:
    - source: salt://repo/conf/hosts

Am I doing it wrong?

Is it not possible to handle events sent while applying states?

EDIT

I finally did it with an Orchestrate Runner.

/srv/reactor/register.sls:

{% set forwarded_data = data.data %}
register:
  runner.state.orch:
    - args:
      - mods: orch.register
      - pillar:
          forwarded_data: {{ forwarded_data | json() }}

/srv/salt/orch/register.sls:

{% set data = salt.pillar.get('forwarded_data') %}

add_host:
   cmd.run:
     - name: /srv/scripts/hosts-manage.sh {{ data.minion_ip }} {{ data.minion_host }}
     - stateful: True

refresh hosts on minions:
  salt.state:
    - tgt: '*'
    - sls: hosts
    - watch:
      - cmd: add_host

/srv/salt/hosts.sls:

# Hosts file management
/etc/hosts:
  file.managed:
    - source: salt://repo/conf/hosts

It seems to be working this way.

Upvotes: 0

Views: 1069

Answers (1)

R1eppe
R1eppe

Reputation: 11

I finally did it with an Orchestrate Runner.

/srv/reactor/register.sls:

{% set forwarded_data = data.data %}
register:
  runner.state.orch:
    - args:
      - mods: orch.register
      - pillar:
          forwarded_data: {{ forwarded_data | json() }}

/srv/salt/orch/register.sls:

{% set data = salt.pillar.get('forwarded_data') %}

add_host:
   cmd.run:
     - name: /srv/scripts/hosts-manage.sh {{ data.minion_ip }} {{ data.minion_host }}
     - stateful: True

refresh hosts on minions:
  salt.state:
    - tgt: '*'
    - sls: hosts
    - watch:
      - cmd: add_host

/srv/salt/hosts.sls:

# Hosts file management
/etc/hosts:
  file.managed:
    - source: salt://repo/conf/hosts

It seems to be working this way.

Upvotes: 1

Related Questions