guettli
guettli

Reputation: 27816

salt-ssh: ls -lS over all minions

I want to run ls -lS /var/log/somebase.log-* over all SaltStack minions.

I want the result to look roughly like this:

Size     Host  File
2345678  foo   /var/log/somebase.log-20161112.gz
1234567  bar   /var/log/somebase.log-20161110.gz
1045678  foo   /var/log/somebase.log-20160912.gz
...

Upvotes: 2

Views: 208

Answers (1)

Mostafa Hussein
Mostafa Hussein

Reputation: 11940

I have created a script which gives similar output, currently tested on one minion only but i guess it will work correctly but it needs to be tested again to ensure.

$ { echo Host Size File ; for minion in local-pc; do sudo salt $minion cmd.run 'ls -lS /var/log/dnf.rpm.log-*' --out=json |  python -c "import sys, json; ret = [{'host': k, 'result' : v} for k,v in json.load(sys.stdin).items()]; print json.dumps(ret)" | jq -r '.[].result' | awk -v host=$minion {'print host" "$5" "$9'};done } | column -t
Host      Size  File
local-pc  5646  /var/log/dnf.rpm.log-20161113
local-pc  4165  /var/log/dnf.rpm.log-20161120
local-pc  4067  /var/log/dnf.rpm.log-20161030
local-pc  3682  /var/log/dnf.rpm.log-20161107

Dependencies:

  • jq
  • python

Limitations:

Minions must be connected. You may need to create a list of connected minions using manage.up then loop through this list by modifying the above code.

I will try to do more tests in order to provide a full solution.

Update:

Just noticed salt-ssh in the title, the code above works for minions, I didn't work with salt-ssh before. but if you got an issue let me know

Upvotes: 2

Related Questions