Python-ansible api2.0 Multi-Process execution of different playbook

Source: Internet
Author: User
Tags dataloader

    1. Automated operations Tools: ansible
    2. Application scenario for multi-process call Ansible API: Application System Check
      • An application system may have a cluster of 20-50 servers, and a preliminary system-level check can be checked with a unified playbook, such as (Df-h command). But deep into the application layer, there are some personalized applications, such as 2 HTTP servers, 20 middleware servers in 4 database servers, and so on, check the items are different. If you want to do a batch system check now, you can't use the same playbook. In addition, if the sequential execution, the wait time will be very long, and not necessary, because the middleware inspection and database inspection does not have the necessary sequencing requirements, can be executed synchronously.
      • So because of the scenario requirements above, we need multiple processes to invoke the Ansible API to dynamically generate inventory and perform different playbook.
    3. Implementation steps
      1. Based on the Ansible Python API 2.0 Two development, encapsulation interface
      2. Create multiple Ansible objects, different inventory different playbook
      3. Create multi-process, execute program
    4. Precautions
      1. The Ansible api2.0 is much more complex and more flexible than the 1.0. This needs to read the original code, need to spend a little more time and patience
      2. Before using multi-process, I tried to use multi-threading, this block seems ansible does not support threading, so we use multiprocessing instead
    5. Code

ansibleapi.py

1 #!/usr/bin/env python2 #Coding=utf-83 ImportJSON4 ImportLogging5  fromAnsible.parsing.dataloaderImportDataloader6  fromAnsible.varsImportVariablemanager7  fromAnsible.inventoryImportInventory8  fromAnsible.playbook.playImportPlay9  fromAnsible.executor.task_queue_managerImportTaskqueuemanagerTen  fromAnsible.executor.playbook_executorImportPlaybookexecutor One  fromAnsible.plugins.callbackImportCallbackbase A  fromCollectionsImportNamedtuple -  fromAnsibleImportConstants as C - ImportAnsible.executor.task_result the ImportMultiprocessing -  -  - classResultscollector (callbackbase): +  -     defV2_runner_on_ok (self, result): +Host =Result._host ALogging.warning ('===v2_runner_on_ok====host=%s===result=%s'%(host, Result._result)) at  -  -     defV2_runner_on_failed (self, result, ignore_errors=False): -Host =Result._host -Logging.warning ('===v2_runner_on_failed====host=%s===result=%s'%(host, Result._result)) -  in     defv2_runner_on_unreachable (self, result): -Host =Result._host toLogging.warning ('===v2_runner_on_unreachable====host=%s===result=%s'%(host, Result._result)) +  -  the classAnsibleapi (object): *     def __init__(Self, hostlist, playbooks, *args, * *Kwargs): $Self.hostlist =hostlistPanax NotoginsengSelf.playbooks =playbooks -Self.passwords =None theSelf.callback =None +Options = Namedtuple ('Options', ['Connection','Remote_user','Ask_sudo_pass','verbosity','Ack_pass', A                                          'Module_path','Forks','become','Become_method','Become_user', the                                          'Check','listhosts','Listtasks','Listtags','Syntax', +                                          'Sudo_user','sudo','display_skipped_hosts']) -  $self.options = Options (connection='Smart', remote_user='Root', Ack_pass=none, sudo_user='Root', $forks=200, sudo='Yes', Ask_sudo_pass=false, Verbosity=5, module_path=None, -Become=true, become_method='su', become_user='Root', Check=none, listhosts=False, -Listtasks=false, Listtags=none, Syntax=none, display_skipped_hosts=False) theSelf.loader =Dataloader () -Self.variable_manager =Variablemanager ()WuyiSelf.inventory = Inventory (loader=self.loader, variable_manager=Self.variable_manager, thehost_list=self.hostlist) - self.variable_manager.set_inventory (self.inventory) WuSelf.variable_manager.extra_vars = {"Ansible_ssh_user":"Root"}#additional parameters, including playbook parameters Key:value -  About     defRunplaybook (self): $Playbook =Playbookexecutor ( -playbooks=Self.playbooks, -inventory=Self.inventory, -Variable_manager=Self.variable_manager, ALoader=Self.loader, +options=Self.options, thepasswords=None) -Playbook._tqm._stdout_callback =Resultscollector () $ Playbook.run () the  the if __name__=='__main__': the     #Creating Objects theanl = Ansibleapi (['10.26.222.216'], ['/root/ansibleinventory_cl/df.yml']) -an2 = Ansibleapi (['10.26.222.210','10.26.222.213'], ['/root/ansibleinventory_cl/ifconfig.yml']) in     #Multi-Process theprocesses = [] theP1 = multiprocessing. Process (name='Process_one', target=Anl.runplaybook) AboutP2 = multiprocessing. Process (name='Process_two', target=An2.runplaybook) the Processes.append (p1) the processes.append (p2) the      forPinchprocesses: + P.start () -  the     Print('Tasks completed')

   

     

Python-ansible api2.0 multiple processes perform different playbook

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.