get minion ip by reactor from salt/key event

391 views
Skip to first unread message

Yong Zhang

unread,
Mar 15, 2018, 2:15:48 AM3/15/18
to Salt-users
Hi, all

I want to add a DNS A record when new minion added to salt.

So I use reactor salt/key to cache accept event, but I'm stuck to get minion ip, I tried to use saltutil.sync_grains before {% set ip = salt['saltutil.runner']('cache.grains', tgt=name)[name]['fqdn_ip4'][0] %} in reactor sls file, but grains were not synced so variable ip got none value. Any ideas? Thanks.

Jeremy McMillan

unread,
Mar 15, 2018, 10:11:14 AM3/15/18
to Salt-users
It makes sense that a new minion might not have cached any grains on the master. 

Try getting the minion to provide its grain from within a runner?

{% set ip = salt['saltutil.runner']('salt.execute', arg=[name, 'grains.get'], kwarg={'arg':'fqdn_ip4:0'}) %}

This, IMHO,  is getting into the territory where it makes sense to write the reactor.sls with a python renderer instead of JINJA+YAML.

Reactor sets up a runner which sets up a salt execution which fetches a grain from a minion: Not a friendly one-liner to debug.

Yong Zhang

unread,
Mar 16, 2018, 4:36:17 AM3/16/18
to Salt-users
Hi, Jeremy

Here's the error:

2018-03-16 16:04:38,187 [salt.utils.reactor][ERROR   ][9827] Failed to render "/etc/salt/srv/reactor/key.sls":

Traceback (most recent call last):

  File "/usr/lib/python2.7/dist-packages/salt/utils/reactor.py", line 70, in render_reaction

    data=data)

  File "/usr/lib/python2.7/dist-packages/salt/state.py", line 309, in render_template

    **kwargs)

  File "/usr/lib/python2.7/dist-packages/salt/template.py", line 101, in compile_template

    ret = render(input_data, saltenv, sls, **render_kwargs)

  File "/usr/lib/python2.7/dist-packages/salt/renderers/jinja.py", line 70, in render

    **kws)

  File "/usr/lib/python2.7/dist-packages/salt/utils/templates.py", line 178, in render_tmpl

    output = render_str(tmplstr, context, tmplpath)

  File "/usr/lib/python2.7/dist-packages/salt/utils/templates.py", line 415, in render_jinja_tmpl

    trace=tracestr)

SaltRenderError: Jinja error: 'salt.execute'

Traceback (most recent call last):

  File "/usr/lib/python2.7/dist-packages/salt/utils/templates.py", line 368, in render_jinja_tmpl

    output = template.render(**decoded_context)

  File "/usr/lib/python2.7/dist-packages/jinja2/environment.py", line 989, in render

    return self.environment.handle_exception(exc_info, True)

  File "/usr/lib/python2.7/dist-packages/jinja2/environment.py", line 754, in handle_exception

    reraise(exc_type, exc_value, tb)

  File "<template>", line 2, in top-level template code

  File "/usr/lib/python2.7/dist-packages/salt/modules/saltutil.py", line 1218, in runner

    full_return=full_return)

  File "/usr/lib/python2.7/dist-packages/salt/runner.py", line 149, in cmd

    full_return)

  File "/usr/lib/python2.7/dist-packages/salt/client/mixins.py", line 225, in cmd

    self.functions[fun], arglist, pub_data

  File "/usr/lib/python2.7/dist-packages/salt/loader.py", line 1088, in __getitem__

    func = super(LazyLoader, self).__getitem__(item)

  File "/usr/lib/python2.7/dist-packages/salt/utils/lazy.py", line 101, in __getitem__

    raise KeyError(key)

KeyError: 'salt.execute'

 

; line 2

 

---

{% if data['act'] == 'accept' %}

{% set vars  = salt['saltutil.runner']('salt.execute', arg=[data['id'], 'grains.get'], kwarg={'arg':'fqdn_ip4:0'}) %}    <======================

xuecheng li

unread,
Mar 16, 2018, 9:59:29 AM3/16/18
to Salt-users
hi Jeremy:
      salt.execute is one module in salt?, i have the some question as Yong Zhang, but, at the end, i find one result that all mine, pillar, grains' infomation can only be g
otten after the event /salt/minion/*/start. maybe it's a misunderstanding.

Daniel Wallace

unread,
Mar 16, 2018, 10:03:06 AM3/16/18
to Salt-users
There is no reason to call the salt.execute runner, you can just use the saltutil.cmd module and send commands to minions without using a runner.

https://docs.saltstack.com/en/latest/ref/modules/all/salt.modules.saltutil.html#salt.modules.saltutil.cmd

{%- set ip = salt.saltutil.cmd(name, fun='grains.get', kwarg={'key': 'fqdn_ip'})|first %}

This module only works on the master, so it won't be usable in other states, but it should work fine in the reactor.



Reply all
Reply to author
Forward
0 new messages