在状态申请期间,Saltstack发送事件

时间:2019-03-05 09:45:09

标签: salt-stack

我试图在中央主机文件中注册我的奴才,然后将该主机文件推送到我所有连接的奴才。

这就是我要记住的:

  1. Minion发送事件“ register”及其IP和主机名,以将其注册到主机的中央主机文件中
  2. 主侦听事件“注册”并与反应堆/srv/reactor/register.sls反应
  3. Reactor在主节点主机上安装的奴才上调用状态/srv/salt/register.sls,以修改中央文件并在修改完成后发送事件“ hosts_modified”
  4. 主侦听事件'hosts_modified'并与反应堆/srv/reactor/deploy_hosts.sls反应,该反应堆将状态/srv/salt/hosts.sls应用于所有连接的奴才以推送新的修改后的宿主文件

前3个步骤工作正常,但主服务器未对最后一个事件“ hosts_modified”做出反应。

在小兵上启动注册的命令:

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

主反应堆配置(/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

我做错了吗?

是否可以处理在应用状态时发送的事件?

编辑

我终于用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

似乎是这样工作的。

1 个答案:

答案 0 :(得分:0)

我终于用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

似乎是这样工作的。