标签:root passwords als named val 就会 plugin tip runner
AnsibleAPI.py
1 #!/usr/bin/env python 2 # coding=utf-8 3 import json 4 import logging 5 from ansible.parsing.dataloader import DataLoader 6 from ansible.vars import VariableManager 7 from ansible.inventory import Inventory 8 from ansible.playbook.play import Play 9 from ansible.executor.task_queue_manager import TaskQueueManager 10 from ansible.executor.playbook_executor import PlaybookExecutor 11 from ansible.plugins.callback import CallbackBase 12 from collections import namedtuple 13 from ansible import constants as C 14 import ansible.executor.task_result 15 import multiprocessing 16 17 18 class ResultsCollector(CallbackBase): 19 20 def v2_runner_on_ok(self, result): 21 host = result._host 22 logging.warning(‘===v2_runner_on_ok====host=%s===result=%s‘ % (host, result._result)) 23 24 25 def v2_runner_on_failed(self, result, ignore_errors=False): 26 host = result._host 27 logging.warning(‘===v2_runner_on_failed====host=%s===result=%s‘ % (host, result._result)) 28 29 def v2_runner_on_unreachable(self, result): 30 host = result._host 31 logging.warning(‘===v2_runner_on_unreachable====host=%s===result=%s‘ % (host, result._result)) 32 33 34 class AnsibleAPI(object): 35 def __init__(self, hostlist, playbooks, *args, **kwargs): 36 self.hostlist = hostlist 37 self.playbooks = playbooks 38 self.passwords = None 39 self.callback = None 40 Options = namedtuple(‘Options‘, [‘connection‘, ‘remote_user‘, ‘ask_sudo_pass‘, ‘verbosity‘, ‘ack_pass‘, 41 ‘module_path‘, ‘forks‘, ‘become‘, ‘become_method‘, ‘become_user‘, 42 ‘check‘, ‘listhosts‘, ‘listtasks‘, ‘listtags‘, ‘syntax‘, 43 ‘sudo_user‘, ‘sudo‘, ‘display_skipped_hosts‘]) 44 45 self.options = Options(connection=‘smart‘, remote_user=‘root‘, ack_pass=None, sudo_user=‘root‘, 46 forks=200, sudo=‘yes‘, ask_sudo_pass=False, verbosity=5, module_path=None, 47 become=True, become_method=‘su‘, become_user=‘root‘, check=None, listhosts=False, 48 listtasks=False, listtags=None, syntax=None, display_skipped_hosts=False) 49 self.loader = DataLoader() 50 self.variable_manager = VariableManager() 51 self.inventory = Inventory(loader=self.loader, variable_manager=self.variable_manager, 52 host_list=self.hostlist) 53 self.variable_manager.set_inventory(self.inventory) 54 self.variable_manager.extra_vars = {"ansible_ssh_user": "root"} # 额外参数,包括playbook参数 key:value 55 56 def runplaybook(self): 57 playbook = PlaybookExecutor( 58 playbooks=self.playbooks, 59 inventory=self.inventory, 60 variable_manager=self.variable_manager, 61 loader=self.loader, 62 options=self.options, 63 passwords=None) 64 playbook._tqm._stdout_callback = ResultsCollector() 65 playbook.run() 66 67 if __name__ == ‘__main__‘: 68 #创建对象 69 anl = AnsibleAPI([‘10.26.222.216‘], [‘/root/ansibleInventory_cl/df.yml‘]) 70 an2 = AnsibleAPI([‘10.26.222.210‘, ‘10.26.222.213‘], [‘/root/ansibleInventory_cl/ifconfig.yml‘]) 71 #多进程 72 processes = [] 73 p1 = multiprocessing.Process(name=‘process_one‘, target=anl.runplaybook) 74 p2 = multiprocessing.Process(name=‘process_two‘, target=an2.runplaybook) 75 processes.append(p1) 76 processes.append(p2) 77 for p in processes: 78 p.start() 79 80 print(‘tasks completed‘)
python-ansible api2.0 多进程执行不同的playbook
标签:root passwords als named val 就会 plugin tip runner
原文地址:http://www.cnblogs.com/swim/p/7427386.html