ansible-cloudstack
ansible-cloudstack copied to clipboard
Add support for generating additional groups based on hostname
Cloudstack only stores a limited amount of grouping information. For example, it is not possible to assign multiple host groups to a host.
This script can be used alongside cloudstack.py to generate additional groups based on hostname regex matches:
import sys
import json
import re
hosts = json.load(sys.stdin)
for host in hosts['all']['hosts']:
for check in sys.argv[1:]:
(group, sep, rex) = check.partition('=')
#print >> sys.stderr, "{} ? {} -> {}".format(host, rex, group)
if rex and re.search(rex, host):
#print >> sys.stderr, "{} -> {}".format(host, group)
if group not in hosts:
hosts[group] = {
'hosts': []
}
hosts[group]['hosts'].append(host)
print(json.dumps(hosts, indent=2))
It would be nice to have this feature added to cloudstack.py, for example with a multi-option like --hostgroup <group_name>=<regex>