Ansible library module for new Ansible-based Observer backend
[plstackapi.git] / planetstack / openstack_observer / ansible.py
1 #!/usr/bin/python
2 import jinja2
3 import tempfile
4 import os
5 import json
6
7 try:
8     step_dir = Config().observer_steps_dir
9 except:
10     step_dir = '/opt/planetstack/observer/steps'
11
12 os_template_loader = jinja2.FileSystemLoader( searchpath=step_dir)
13 os_template_env = jinja2.Environment(loader=os_template_loader)
14
15 def parse_output(msg):
16     lines = msg.splitlines()
17     results = []
18     print msg
19
20     for l in lines:
21         magic_str = 'ok: [127.0.0.1] => '
22         if (l.startswith(magic_str)):
23             w = len(magic_str)
24             str = l[w:]
25             d = json.loads(str)
26             results.append(d)
27
28     return results
29             
30 def run_template(name, opts):
31     template = os_template_env.get_template(name)
32     buffer = template.render(opts)
33     
34     f = tempfile.NamedTemporaryFile(mode='w')
35     f.write(buffer)
36     f.flush()
37     
38     run = os.popen('/opt/planetstack/observer/run_ansible '+f.name)
39     msg = run.read()
40     status = run.close()
41
42     ok_results = parse_output(msg)
43     return ok_results
44
45 def main():
46         run_template('ansible/sync_user_deployments.yaml',{ "endpoint" : "http://172.31.38.128:5000/v2.0/",
47                  "name" : "Sapan Bhatia",
48                  "email": "gwsapan@gmail.com",
49                  "password": "foobar",
50                  "admin_user":"admin",
51                  "admin_password":"6a789bf69dd647e2",
52                  "admin_tenant":"admin",
53                  "tenant":"demo",
54                  "roles":['user','admin'] })