Added password support for SSH and Ansible
[snaps.git] / snaps / playbook_runner.py
index 87321f5..03b7006 100644 (file)
 import argparse
 import ast
 import logging
+import os
 
 import re
 
+import yaml
+from jinja2 import Environment, FileSystemLoader
+
 from snaps.openstack.os_credentials import ProxySettings
 from snaps.provisioning import ansible_utils
 
@@ -41,20 +45,32 @@ def main(parsed_args):
                                        ssh_proxy_cmd=parsed_args.ssh_proxy_cmd)
 
     # Ensure can get an SSH client
-    ssh = ansible_utils.ssh_client(parsed_args.ip_addr, parsed_args.host_user,
-                                   parsed_args.priv_key, proxy_settings)
+    ssh = ansible_utils.ssh_client(
+        parsed_args.ip_addr, parsed_args.host_user,
+        private_key_filepath=parsed_args.priv_key,
+        proxy_settings=proxy_settings)
     if ssh:
         ssh.close()
 
-    vars = dict()
-    if args.vars:
-        vars = ast.literal_eval(args.vars)
-        if not isinstance(vars, dict):
-            vars = dict()
+    env = Environment(loader=FileSystemLoader(
+        searchpath=os.path.dirname(parsed_args.env_file)))
+    template = env.get_template(os.path.basename(parsed_args.env_file))
+
+    env_dict = dict()
+    if parsed_args.vars:
+        env_dict = ast.literal_eval(parsed_args.vars)
+
+    output = template.render(**env_dict)
+
+    variables = yaml.load(output)
+
+    if not variables.get('env_file'):
+        variables['env_file'] = parsed_args.env_file
 
     retval = ansible_utils.apply_playbook(
         parsed_args.playbook, [parsed_args.ip_addr], parsed_args.host_user,
-        parsed_args.priv_key, variables=vars,
+        ssh_priv_key_file_path=parsed_args.priv_key,
+        password=parsed_args.password, variables=variables,
         proxy_setting=proxy_settings)
     exit(retval)
 
@@ -63,19 +79,26 @@ if __name__ == '__main__':
     parser = argparse.ArgumentParser()
     parser.add_argument('-a', '--ip-addr', dest='ip_addr', required=True,
                         help='The Host IP Address')
-    parser.add_argument('-k', '--priv-key', dest='priv_key', required=True,
-                        help='The location of the private key file')
     parser.add_argument('-u', '--host-user', dest='host_user', required=True,
                         help='Host user account')
+    parser.add_argument('-k', '--priv-key', dest='priv_key', required=False,
+                        help='The location of the private key file')
+    parser.add_argument('-pw', '--password', dest='password', required=False,
+                        help='The host-user password')
     parser.add_argument('-b', '--playbook', dest='playbook', required=True,
                         help='Playbook Location')
     parser.add_argument('-p', '--http-proxy', dest='http_proxy',
                         required=False, help='<host>:<port>')
     parser.add_argument('-s', '--ssh-proxy-cmd', dest='ssh_proxy_cmd',
                         required=False)
+    parser.add_argument('-e', '--env-file', dest='env_file',
+                        help='Yaml file containing playbook substitution vals',
+                        required=False)
     parser.add_argument('-v', '--vars', dest='vars',
+                        help='String renditon of a dict to pass into '
+                             'playbook for additional subtitution values not '
+                             'found in env_file',
                         required=False)
     args = parser.parse_args()
 
     main(args)
-