2012-03-07 17:35:18 +01:00
|
|
|
API
|
2012-03-08 19:36:47 +01:00
|
|
|
```
|
|
|
|
|
|
|
|
The Python API is very powerful, and is how the ansible CLI and ansible-playbook
|
|
|
|
are implemented.
|
|
|
|
|
|
|
|
It's pretty simple::
|
|
|
|
|
|
|
|
import ansible.runner
|
|
|
|
|
|
|
|
runner = ansible.runner.Runner(
|
|
|
|
module_name='ping',
|
|
|
|
module_args='',
|
|
|
|
pattern='web*',
|
|
|
|
forks=10
|
|
|
|
)
|
|
|
|
datastructure = runner.run()
|
|
|
|
|
|
|
|
The run method returns results per host, grouped by whether they
|
|
|
|
could be contacted or not. Return types are module specific, as
|
|
|
|
expressed in the 'ansible-modules' documentation.::
|
|
|
|
|
|
|
|
{
|
|
|
|
"dark" : {
|
|
|
|
"web1.example.com" : "failure message"
|
|
|
|
}
|
|
|
|
"contacted" : {
|
|
|
|
"web2.example.com" : 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
A module can return any type of JSON data it wants, so Ansible can
|
|
|
|
be used as a framework to rapidly build powerful applications and scripts.
|
|
|
|
|
|
|
|
|