aboutsummaryrefslogtreecommitdiffstats
path: root/func
diff options
context:
space:
mode:
authorzhifeng.jiang <jiang.zhifeng@zte.com.cn>2016-08-08 23:33:33 +0800
committerzhifeng.jiang <jiang.zhifeng@zte.com.cn>2016-08-21 13:28:16 +0800
commit60cee12c6ea236516da78438f43aa7afb4efd375 (patch)
treedea138d61906701401926f2cc35b99d7eb3a4e92 /func
parent9d742933abcbce7ad604a4ba4c9611e81a2cf286 (diff)
Add ansible_api so that python call ansible playbook by api.
modification: Add ansible_api so that we can get ansible running results easily. Add UT for ansible_api.py JIRA:QTIP-99 Change-Id: I6e7b0b97e4d9ef184978833c61c17e944c1eb20c Signed-off-by: zhifeng.jiang <jiang.zhifeng@zte.com.cn>
Diffstat (limited to 'func')
-rw-r--r--func/ansible_api.py64
1 files changed, 64 insertions, 0 deletions
diff --git a/func/ansible_api.py b/func/ansible_api.py
new file mode 100644
index 00000000..57224eb7
--- /dev/null
+++ b/func/ansible_api.py
@@ -0,0 +1,64 @@
+##############################################################################
+# Copyright (c) 2016 ZTE Corp and others.
+#
+# All rights reserved. This program and the accompanying materials
+# are made available under the terms of the Apache License, Version 2.0
+# which accompanies this distribution, and is available at
+# http://www.apache.org/licenses/LICENSE-2.0
+##############################################################################
+import os
+from collections import namedtuple
+import logging
+
+from ansible.parsing.dataloader import DataLoader
+from ansible.vars import VariableManager
+from ansible.inventory import Inventory
+from ansible.executor.playbook_executor import PlaybookExecutor
+
+
+class AnsibleApi:
+
+ def __init__(self):
+ self.variable_manager = VariableManager()
+ self.loader = DataLoader()
+ self.passwords = {}
+ self.pbex = None
+
+ def _check_path(self, file_path):
+ if not os.path.exists(file_path):
+ logging.error('The playbook %s does not exist' % file_path)
+ return False
+ else:
+ return True
+
+ def execute_playbook(self, hosts_file, playbook_path, pub_key_file, vars):
+ if not self._check_path(hosts_file):
+ return False
+
+ inventory = Inventory(loader=self.loader, variable_manager=self.variable_manager,
+ host_list=hosts_file)
+ Options = namedtuple('Options', ['listtags', 'listtasks', 'listhosts', 'syntax',
+ 'connection', 'module_path', 'forks', 'remote_user',
+ 'private_key_file', 'ssh_common_args', 'ssh_extra_args',
+ 'sftp_extra_args', 'scp_extra_args', 'become',
+ 'become_method', 'become_user', 'verbosity', 'check'])
+ options = Options(listtags=False, listtasks=False, listhosts=False, syntax=False,
+ connection='ssh', module_path=None, forks=100, remote_user='root',
+ private_key_file=pub_key_file, ssh_common_args=None,
+ ssh_extra_args=None, sftp_extra_args=None, scp_extra_args=None,
+ become=True, become_method=None, become_user='root', verbosity=None,
+ check=False)
+ self.variable_manager.extra_vars = vars
+
+ self.pbex = PlaybookExecutor(playbooks=[playbook_path], inventory=inventory,
+ variable_manager=self.variable_manager, loader=self.loader,
+ options=options, passwords=self.passwords)
+
+ return self.pbex.run()
+
+ def get_detail_playbook_stats(self):
+ if self.pbex:
+ stats = self.pbex._tqm._stats
+ return map(lambda x: (x, stats.summarize(x)), stats.processed.keys())
+ else:
+ return None