ansible-cloudstack icon indicating copy to clipboard operation
ansible-cloudstack copied to clipboard

Add support for generating additional groups based on hostname

Open onitake opened this issue 8 years ago • 0 comments

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>

onitake avatar Dec 11 '17 10:12 onitake