Run a reactor or state on one minion in a glob of minions?

231 views
Skip to first unread message

Mike

unread,
Jul 13, 2016, 11:55:40 AM7/13/16
to Salt-users
Lets say I want to run a reactor or a salt '*' command and target a bunch of minions, but I only want it to run on the first minion targeted (or some random minion in the target list).  How would I do that?
Message has been deleted

Christopher Baklid

unread,
Jul 13, 2016, 3:44:49 PM7/13/16
to Salt-users
If I understand you right, you just want to target a single random minion?

You could do that in a runner, essentially pull a list of minions and use random.choice() to select one to run a command on

When I've had some sleep I can write you a full example, otherwise this is a good place to start:

Christopher Baklid

unread,
Jul 14, 2016, 2:21:18 AM7/14/16
to Salt-users
Here's what I had in mind:

A custom runner in the '/srv/salt/_runners/'

# /srv/salt/_runners/rnd.py
import salt.client
import salt.config
import salt.runner
from random import choice


def target():
 
# load master config
 opts
= salt.config.master_config('/etc/salt/master')


 
# load runner client with master config
 runner
= salt.runner.RunnerClient(opts)


 
# run manage.up to fetch active minions
 list_of_minions
= runner.cmd('manage.up', [])


 
# let python randomly choose a minion
 random_target
= choice(list_of_minions)


 
# load the execution module client
 
local = salt.client.LocalClient()


 
# run execution module on randomly targeted minion
 
return local.cmd(random_target, 'test.ping')

The reactor conf file
# /etc/salt/master.d/reactor.conf
reactor
:
 
- 'my/custom/event/':
   
- /srv/reactor/random_target.sls

and lastly the reactor state
# /srv/reactor/random_target.sls
run_command_on_random_target
:
  runner
.rnd.target

I hope that's somewhat what you had in mind?

Mike

unread,
Jul 14, 2016, 8:25:10 AM7/14/16
to Salt-users
Yes, this looks great.  Thank you.
Reply all
Reply to author
Forward
0 new messages