aa384d
#!/usr/bin/python3
aa384d
aa384d
# Collects all the assigned roles of all the minions, and checks if the
aa384d
# declared {salt,pillar}/role/*.sls files actually match a minion-assigned role
aa384d
aa384d
import yaml
aa384d
import os
aa384d
import sys
aa384d
aa384d
all_roles = ['base']
aa384d
aa384d
for sls in os.listdir('pillar/id'):
aa384d
    with open("pillar/id/%s" % sls) as f:
aa384d
        try:
aa384d
            _roles = yaml.load(f)['grains']['roles']
aa384d
        except KeyError:
aa384d
            continue
aa384d
aa384d
        for item in _roles:
aa384d
            all_roles.append(item)
aa384d
aa384d
all_roles = sorted(set(all_roles))
aa384d
aa384d
for directory in ['salt', 'pillar']:
aa384d
    for sls in os.listdir('%s/role' % directory):
aa384d
        if sls.endswith('.sls'):
aa384d
            with open('salt/role/%s' % sls) as f:
aa384d
                if sls.split('.sls')[0] not in all_roles:
aa384d
                    print ('%s/role/%s not in roles' % (directory, sls))
aa384d
                    status = 1
aa384d
aa384d
sys.exit(status)