Skip to content

Instantly share code, notes, and snippets.

@hemebond
Created August 25, 2018 03:21
Show Gist options
  • Save hemebond/bcad3ebd8cada0acf0cafb3c651c4e89 to your computer and use it in GitHub Desktop.
Save hemebond/bcad3ebd8cada0acf0cafb3c651c4e89 to your computer and use it in GitHub Desktop.
Reactor system for initialising new minions (jinja extensions just for formatting)
{#
Requires Salt 2016.11 as the syntax changes for these functions
https://docs.saltstack.com/en/2016.11/topics/orchestrate/orchestrate_runner.html
Using salt.function instead of salt.state for highstate because of bug https://github.com/saltstack/salt/issues/26448
#}
{% set tag = salt.pillar.get('event_tag') %}
{% set data = salt.pillar.get('event_data') %}
{# minion IDs start with a three-letter code that is their environment #}
{% set env = data.id[:3] %}
set_hosts:
salt.function:
- name: state.apply
- tgt: {{ data.id }}
- arg:
- hosts
sync_all:
salt.function:
- name: saltutil.sync_all
- tgt: {{ data.id }}
- require:
- salt: set_hosts
refresh_pillar:
salt.function:
- name: saltutil.refresh_pillar
- tgt: {{ data.id }}
- require:
- salt: sync_all
mine_update:
salt.function:
- name: mine.update
- tgt: {{ data.id }}
- require:
- salt: refresh_pillar
highstate:
salt.function:
- name: state.highstate
- tgt: {{ data.id }}
- require:
- salt: mine_update
set_grain:
salt.function:
- name: grains.set
- tgt: {{ data.id }}
- arg:
- initialised
- True
- require:
- salt: highstate
update_hosts:
salt.function:
- name: state.apply
- tgt: "{{ env }}* and not {{ data.id }}"
- tgt_type: compound
- arg:
- hosts
- require:
- salt: highstate
update_icinga:
salt.function:
- name: state.highstate
- tgt: icinga
- require:
- salt: highstate
{%- set id = data.id %}
{%- set grains_cache = salt.saltutil.runner('cache.grains', tgt=id) %}
{%- set grns = grains_cache.get(id, {}) %}
{%- set first_run = grns.get('first_run', True) %}
{%- set initialised = grns.get('initialised', False) %}
{# migrating from the old grain "first_run", to the new grain "initialised" #}
{%- if first_run and not initialised %}
invoke_orchestrate_file:
runner.state.orchestrate:
- mods: orchestration.new_instance
- saltenv: mysaltenv
- pillar:
event_tag: {{ tag }}
event_data: {{ data|json }}
{% endif %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment