Step1
Environment Master:centos 7
RPM-IVH https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
Yum install gcc libffi-devel Python-devel openssl-devel
yum-y Install ansible
Step2
# vim/etc/ansible/hosts
[remote-host]
192.168.1.241
...
Step3
. py Script File
#!/usr/bin/python Import ansible.runner Import sys # construct the Ansible runner and execute on all hosts DEF command (PA Ttern,module,args,fork): results = Ansible.runner.Runner (Pattern=pattern, forks=fork, MoD
Ule_name=module, Module_args=args,). Run () If results is None:print "No hosts found"
Sys.exit (1) print "Up ***********" for (hostname, result) in results[' contacted '].items (): Try:if not ' failed ' in Result:print '%s >>>%s '% (hostname, result[' s Tdout ']) except Exception,e:print e sys.exit (1) print "FAILED *******
"For [hostname, result] in results[' contacted '].items (): Try:if ' failed ' in result:
Print '%s >>>%s '% (hostname, result[' msg ') except Exception,e:
Print E Sys.exit (1) print "Down *********" for (hostname, result) in results[' Dark '].items (): Try
: print "%s >>>%s"% (hostname, result) except Exception,e:print E
Sys.exit (1) if __name__ = = "__main__": #command (' remote-host ', ' command ', '/usr/bin/uptime ', 5)
Command (' remote-host ', ' Copy ', ' src=/root/zabbix-2.4.6.tar.gz dest=/root/zabbix-2.4.6.tar.gz mode=0644 ', 5)
Ansible V2.0
#!/usr/bin/env python #-*-coding:utf-8-*-import JSON from collections import Namedtuple from Ansible.parsing.dataload Er import dataloader from ansible.vars import Variablemanager to ansible.inventory import inventory from Ansible.playboo K.play Import play from Ansible.executor.task_queue_manager import Taskqueuemanager from Ansible.plugins.callback Import Callbackbase # Create A callback object so we can capture the output class Resultcallback (callbackbase): "" "A Sample callback plugin used for performing an action as results come in If you want to collect all results into a sin GLE object for processing at the "End of" execution, look into utilizing the ' JSON ' callback plugin or writing Your own custom callback plugin "" "Def V2_RUNNER_ON_OK (self, result, **kwargs):" "" Print a JSON Represen
Tation of the "result" is could store the result in a instance attribute for retrieval later "" "
Host = Result._host Print (Json.dumps ({host.name:result._result}, indent=4)) def ansiblerun (host_list,task_list): Options = NAMEDTU
ple (' Options ', [' Connection ', ' module_path ', ' forks ', ' remote_user ', ' private_key_file ', ' Ssh_common_args ', ' Ssh_extra_args ', ' Sftp_extra_args ', ' Scp_extra_args ', ' become ', ' B Ecome_method ', ' become_user ', ' verbosity ', ' Check '] # Initialize needed objects = Variable_manager ( Loader = Dataloader () options = options (connection= ' smart ', Module_path=none, forks=100 , Remote_user=none,private_key_file=none,ssh_common_args=none,ssh_extra_args=none, sftp_extra_args= None,scp_extra_args=none,become=none,become_method=none, Become_user=none,verbosity=none,check=fal SE) passwords = Dict () # Instantiate our resultcallback for handling results as they come In Results_callback = Resultcallback () # Create inventory and pass to var manager inventory = Inventory (Loader=loader, Variable_mana
Ger=variable_manager, Host_list=host_list) variable_manager.set_inventory (inventory) # Create play with tasks
Play_source = dict (name = "Ansible Play", hosts = host_list, gather_facts = ' no ', tasks = task_list) play = Play (). Load (Play_source, Variable_manager=variable_manager, Loader=loade R) # actually run it TQM = None TRY:TQM = Taskqueuemanager (Inventory=inventory,vari Able_manager=variable_manager, Loader=loader,options=options,passwords=passwords, Stdout_c Allback=results_callback, # Use our custom callback instead of the ' default ' callback plugin) ResU lt = Tqm.run (play) Finally:if TQM isn't None:tqm.cleanup () if __name__ = = ' __main__ ': Host _list = [' 10.14.86.177 ', ' 10.14.86.178 '] tasks_list = [Dict (action=dict (module= ' command ', args= ' pwd ')), # dict (Action=dict (Mo Dule= ' Shell ', args= ' Python sleep.py '), # dict (action=dict module= ' Synchronize ', args= ' src=/home/op/test Dest=/hom ') E/op/delete=yes ')),] Ansiblerun (host_list,tasks_list)