Access pillar.get indices from command line?

258 views
Skip to first unread message

Matthew Kettlewell

unread,
Feb 23, 2018, 1:35:15 PM2/23/18
to Salt-users


In Salt ( using Jinja ), I can do something like this:
DNS2={{ salt['pillar.get']('nameservers')[1] }}

and on the minion, I can do this:
sudo salt-call pillar.get nameservers

and it will retrieve all of them ( plus a Local: heading)

Is there a way to directly access the values at a particular index?

something along the lines of this ( clearly not valid, but for demonstration) 
sudo salt-call pillar.get nameservers[1]


something tells me there's either a salt-call flag I'm missing, or a special syntax to access it that I've not found.

I know that I can do something like this, it just seems... clunky and wrong:
sudo salt-call pillar.get nameservers --out=key | awk 'NR==3 {print}'




Lead me a better path if you know it.


Thanks

Matt

Daniel Wallace

unread,
Feb 23, 2018, 1:40:03 PM2/23/18
to Salt-users
the best way to do it would be to use something like `jq` to query the json output.

https://stedolan.github.io/jq/

[root@1a43ca0ff65e /]# salt-call --local grains.get fqdn
local:
    1a43ca0ff65e
[root@1a43ca0ff65e /]# salt-call --local grains.get fqdn --out=json | jq '.local'
"1a43ca0ff65e"

You can also use yq if you don't want to output as json.

https://github.com/kislyuk/yq

Alternatively, if you don't want to install something else, you could import the Python Client client and write a script in python to pull the information out.

Daniel



--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/salt-users/1ab862df-5612-490c-8830-96cca260d58e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages