Batch execution playbooks
Another way of remote batch command execution is to use playbooks;
This is Playbooks's Official document: http://docs.ansible.com/playbooks.html
Here are playbooks examples of ansible: https://github.com/ansible/ansible-examples
Using the Ansbile API in Python
The above methods of executing the Ansible module are called directly on the command line, and if further processing is required for the returned results, the Ansible module can be used in the program through API calls:
For example, the above method calls the scripts module in the command line in the API call:
import ansible.runnerresults = ansible.runner.Runner(pattern=‘myservers‘, forks=5,module_name=‘script‘, module_args=‘/opt/app/target.sh‘,).run()
Here is a detailed example of the official given, run directly, the result will be printed out, there will be an intuitive understanding:
#!/usr/bin/pythonimport ansible.runnerimport sys# construct the ansible runner and execute on all hostsresults = ansible.runner.Runner(pattern=‘*‘, forks=10,module_name=‘command‘, module_args=‘/usr/bin/uptime‘,).run()if results is None: print "No hosts found"
Ansible Automation (3)