aboutsummaryrefslogtreecommitdiffstats
path: root/func/ansible_api.py
blob: 57224eb706a2e81743ab717c6db8d50724993134 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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