diff options
-rw-r--r-- | func/ansible_api.py | 64 | ||||
-rw-r--r-- | requirements.txt | 1 | ||||
-rw-r--r-- | tests/ansible_api_test.py | 19 | ||||
-rw-r--r-- | tests/data/hosts | 2 | ||||
-rw-r--r-- | tests/data/test.yml | 4 |
5 files changed, 90 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 diff --git a/requirements.txt b/requirements.txt index af07083d..cdaeef70 100644 --- a/requirements.txt +++ b/requirements.txt @@ -10,3 +10,4 @@ reportlab==3.0 Flask==0.11.1 Flask-RESTful==0.3.5 flask-restful-swagger==0.19 +ansible==2.1.1.0 diff --git a/tests/ansible_api_test.py b/tests/ansible_api_test.py new file mode 100644 index 00000000..e9f0a77d --- /dev/null +++ b/tests/ansible_api_test.py @@ -0,0 +1,19 @@ +############################################################################## +# 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 +############################################################################## +from func.ansible_api import AnsibleApi + + +class TestClass: + def test_call_ansible_api_success(self): + ansible_api = AnsibleApi() + ret = ansible_api.execute_playbook('tests/data/hosts', + 'tests/data/test.yml', + 'data/QtipKey', + {'keys': 'test'}) + assert ret == 3 diff --git a/tests/data/hosts b/tests/data/hosts new file mode 100644 index 00000000..0a0ac539 --- /dev/null +++ b/tests/data/hosts @@ -0,0 +1,2 @@ +[sample_group_name] +127.0.0.1 diff --git a/tests/data/test.yml b/tests/data/test.yml new file mode 100644 index 00000000..270e86fd --- /dev/null +++ b/tests/data/test.yml @@ -0,0 +1,4 @@ +- hosts: sample_group_name + tasks: + - name: just an uname + command: uname -a |