Inject static grains with salt-bootstrap

234 views
Skip to first unread message

Tony Hays

unread,
Aug 16, 2016, 10:47:19 AM8/16/16
to Salt-users
I am currently running salt-bootstrap as a hook in opennebula.  I have a user-input chooser with salt_role that is passed to the script running salt-bootstrap.  A reactor kicks off a highstate once auth is complete.  I want to inject the role as a grain after packages are installed but before daemon starts the first time so it will be present for the highstate.  Any thought on easiest way to get this done?

Thanks,

Tony

David Boucha

unread,
Aug 16, 2016, 11:39:49 AM8/16/16
to salt users list
One option you could take is to use the `-j` option to the
salt-bootstrap script and pass in json with for the "grains"
subsection of the minion config.

As you can see here you can add grains directly to the minion config:

https://github.com/saltstack/salt/blob/3b6f39eb4e4f7a1ad9c70b55e282b0215dc75d8e/conf/minion#L120

And here you can see the `-j` option:

https://github.com/saltstack/salt-bootstrap/blob/e9328eddd0a7b4a22df3920101d288c9fbeeacd3/bootstrap-salt.sh#L352
> --
> You received this message because you are subscribed to the Google Groups
> "Salt-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to salt-users+...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Tony Hays

unread,
Aug 16, 2016, 12:29:30 PM8/16/16
to Salt-users
This looks like a great option. I was having a hard time with the formatting of the json behind it, but I think I have it dialed in now.  Might be helpful for a syntax example somewhere.  Never enough single quotes it seems :)

Thanks for the suggestion, this is going to work great!

Tony Hays

unread,
Aug 16, 2016, 4:55:41 PM8/16/16
to Salt-users
Well I was wrong.  I thought I had the syntax but not quite there.  Can someone see the error in my syntax?  Is there an example out there of a valid json string that will be converted properly?

My Script:

JSONSTRING="{\"grains.roles[0]\":\"test\"}
salt-bootstrap.sh -j $JSONSTRING

results in 

/etc/salt/minion
grains.roles: test

instead of
grains:
  roles: test



Thanks!

David Boucha

unread,
Aug 16, 2016, 5:14:37 PM8/16/16
to salt users list
Generally I have the most success with json like that if I use single
quotes outside the actual json and double quotes inside.

Something like this:

JSONSTRING='{"mykey":"myval"}'


So to get:

grains:
roles: test

I'd do:

JSONSTRING='{"grains":{"roles":"test"}}'

There's a bunch of great yaml to json and json to yaml converters
online to double check.

Tony Hays

unread,
Aug 17, 2016, 5:29:44 PM8/17/16
to Salt-users
Awesome!  You guys are such json ninjas.  Your solution worked great for hard coded vals so I decided to kick it up a notch with variables.  This required more quoting, but itt works now end to end.


Thanks Again!
Reply all
Reply to author
Forward
0 new messages