diff options
Diffstat (limited to 'bin/ansibleutil.py')
-rw-r--r-- | bin/ansibleutil.py | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/bin/ansibleutil.py b/bin/ansibleutil.py new file mode 100644 index 000000000..7ba2c461e --- /dev/null +++ b/bin/ansibleutil.py @@ -0,0 +1,49 @@ +# vim: expandtab:tabstop=4:shiftwidth=4 + +import subprocess +import sys +import os +import json +import re + +class AnsibleUtil(object): + def __init__(self): + self.file_path = os.path.join(os.path.dirname(os.path.realpath(__file__))) + self.multi_ec2_path = os.path.realpath(os.path.join(self.file_path, '..','inventory','multi_ec2.py')) + + def get_inventory(self): + cmd = [self.multi_ec2_path] + env = {} + p = subprocess.Popen(cmd, stderr=subprocess.PIPE, + stdout=subprocess.PIPE, env=env) + + out,err = p.communicate() + + if p.returncode != 0: + raise RuntimeError(err) + + return json.loads(out) + + def get_environments(self): + pattern = re.compile(r'^tag_environment_(.*)') + + envs = [] + inv = self.get_inventory() + for key in inv.keys(): + m = pattern.match(key) + if m: + envs.append(m.group(1)) + + return envs + + def get_security_groups(self): + pattern = re.compile(r'^security_group_(.*)') + + groups = [] + inv = self.get_inventory() + for key in inv.keys(): + m = pattern.match(key) + if m: + groups.append(m.group(1)) + + return groups |