python执行playbook

 1 from collections import namedtuple
 2 from ansible.parsing.dataloader import DataLoader
 3 from ansible.vars.manager import VariableManager
 4 from ansible.inventory.manager import InventoryManager
 5 from ansible.executor.playbook_executor import PlaybookExecutor
 6 
 7 Options = namedtuple(
 8     'Options',
 9     [
10         'connection',
11         'remote_user',
12         'ask_sudo_pass',
13         'verbosity',
14         'ask_pass',
15         'module_path',
16         'forks',
17         'become',
18         'become_method',
19         'become_user',
20         'check',
21         'listhosts',
22         'listtasks',
23         'listtags',
24         'syntax',
25         'sudo_user',
26         'sudo',
27         'diff'
28     ]
29 )
30 options = Options(
31     connection='smart',
32     remote_user=None,
33     ask_pass=None,
34     sudo_user=None,
35     forks=5,
36     sudo=None,
37     ask_sudo_pass=False,
38     verbosity=5,
39     module_path=None,
40     become=None,
41     become_method=None,
42     become_user=None,
43     check=False,
44     diff=False,
45     listhosts=None,
46     listtasks=None,
47     listtags=None,
48     syntax=None
49 )
50 
51 def run_pb(sources, pb_path):
52     loader = DataLoader()
53     passwords = dict()
54     inventory = InventoryManager(loader=loader, sources=sources)
55     variable_manager = VariableManager(loader=loader, inventory=inventory)
56     pb = PlaybookExecutor(
57         playbooks=pb_path,
58         inventory=inventory,
59         variable_manager=variable_manager,
60         loader=loader,
61         options=options,
62         passwords=passwords
63     )
64     result = pb.run()
65     return result
66 
67 if __name__ == '__main__':
68     sources = ['myansible/hosts']
69     playbook = ['myansible/lamp.yml']
70     run_pb(sources, playbook)

 

posted @ 2019-03-29 09:13  Ray_chen  阅读(953)  评论(0编辑  收藏  举报