Skip to content

Instantly share code, notes, and snippets.

@markuskont
Last active September 5, 2017 08:11
Show Gist options
  • Save markuskont/c8f9db81766707ed1bad8d829fc0054b to your computer and use it in GitHub Desktop.
Save markuskont/c8f9db81766707ed1bad8d829fc0054b to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
from os import listdir
from os import path
import yaml
role = 'logs'
base_dir = '/var/cache/salt/master/minions'
minions_dirs = listdir(base_dir)
for dir in minions_dirs:
grain_file = base_dir + '/' + dir + '/files/etc/salt/grains'
if path.isfile(grain_file):
with open(grain_file, 'r') as infile:
grains = yaml.load(infile)
grains['roles'].append(role)
with open(grain_file, 'w') as outfile:
yaml.dump(grains, outfile, default_flow_style=False)
else:
print("%s does not exist") % (grain_file)
# return comma separated list of some minions, good for creating host lists
salt -C 'G@location:asd and G@kernel:windows' test.ping | grep -v True | sed 's/:/,/g' | sed ':a;N;$!ba;s/\n/ /g'
# copy edited grains files from master to respective minion
find ./ -mindepth 1 -maxdepth 1 -type d | while read line ; do echo $line | salt-cp "`pcregrep -o1 --color '\./(.+)'`" $line/files/etc/salt/grains /etc/salt/grains ; done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment