2012-03-07 17:35:18 +01:00
< !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
< html xmlns = "http://www.w3.org/1999/xhtml" >
< head >
< meta http-equiv = "Content-Type" content = "text/html; charset=utf-8" / >
2012-03-09 13:42:53 +01:00
< title > Using the Python API — Ansible v0.0.1 documentation< / title >
2012-03-07 17:35:18 +01:00
< link rel = "stylesheet" href = "_static/default.css" type = "text/css" / >
< link rel = "stylesheet" href = "_static/pygments.css" type = "text/css" / >
< script type = "text/javascript" >
var DOCUMENTATION_OPTIONS = {
URL_ROOT: '',
VERSION: '0.0.1',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
< / script >
< script type = "text/javascript" src = "_static/jquery.js" > < / script >
< script type = "text/javascript" src = "_static/underscore.js" > < / script >
< script type = "text/javascript" src = "_static/doctools.js" > < / script >
< link rel = "top" title = "Ansible v0.0.1 documentation" href = "index.html" / >
2012-03-08 19:53:48 +01:00
< link rel = "next" title = "Man Pages" href = "man.html" / >
2012-03-09 04:50:12 +01:00
< link rel = "prev" title = "Playbooks: Ansible for Deployment, Configuration Management, and Orchestration" href = "playbooks.html" / >
2012-03-07 17:35:18 +01:00
< / head >
< body >
< div class = "related" >
< h3 > Navigation< / h3 >
< ul >
< li class = "right" style = "margin-right: 10px" >
< a href = "genindex.html" title = "General Index"
accesskey="I">index< / a > < / li >
< li class = "right" >
2012-03-08 19:53:48 +01:00
< a href = "man.html" title = "Man Pages"
2012-03-07 17:35:18 +01:00
accesskey="N">next< / a > |< / li >
< li class = "right" >
2012-03-09 04:50:12 +01:00
< a href = "playbooks.html" title = "Playbooks: Ansible for Deployment, Configuration Management, and Orchestration"
2012-03-07 17:35:18 +01:00
accesskey="P">previous< / a > |< / li >
< li > < a href = "index.html" > Ansible v0.0.1 documentation< / a > » < / li >
< / ul >
< / div >
< div class = "document" >
< div class = "documentwrapper" >
< div class = "bodywrapper" >
< div class = "body" >
2012-03-09 13:42:53 +01:00
< div class = "section" id = "using-the-python-api" >
< h1 > Using the Python API< a class = "headerlink" href = "#using-the-python-api" title = "Permalink to this headline" > ¶< / a > < / h1 >
2012-03-08 19:36:47 +01:00
< p > The Python API is very powerful, and is how the ansible CLI and ansible-playbook
are implemented.< / p >
< p > It’ s pretty simple:< / p >
< div class = "highlight-python" > < div class = "highlight" > < pre > < span class = "kn" > import< / span > < span class = "nn" > ansible.runner< / span >
< span class = "n" > runner< / span > < span class = "o" > =< / span > < span class = "n" > ansible< / span > < span class = "o" > .< / span > < span class = "n" > runner< / span > < span class = "o" > .< / span > < span class = "n" > Runner< / span > < span class = "p" > (< / span >
< span class = "n" > module_name< / span > < span class = "o" > =< / span > < span class = "s" > ' ping' < / span > < span class = "p" > ,< / span >
< span class = "n" > module_args< / span > < span class = "o" > =< / span > < span class = "s" > ' ' < / span > < span class = "p" > ,< / span >
< span class = "n" > pattern< / span > < span class = "o" > =< / span > < span class = "s" > ' web*' < / span > < span class = "p" > ,< / span >
< span class = "n" > forks< / span > < span class = "o" > =< / span > < span class = "mi" > 10< / span >
< span class = "p" > )< / span >
< span class = "n" > datastructure< / span > < span class = "o" > =< / span > < span class = "n" > runner< / span > < span class = "o" > .< / span > < span class = "n" > run< / span > < span class = "p" > ()< / span >
< / pre > < / div >
< / div >
< p > 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.:< / p >
< div class = "highlight-python" > < pre > {
"dark" : {
"web1.example.com" : "failure message"
}
"contacted" : {
"web2.example.com" : 1
}
}< / pre >
< / div >
< p > 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.< / p >
2012-03-09 13:42:53 +01:00
< div class = "section" id = "detailed-api-example" >
< h2 > Detailed API Example< a class = "headerlink" href = "#detailed-api-example" title = "Permalink to this headline" > ¶< / a > < / h2 >
< p > The following script prints out the uptime information for all hosts:< / p >
< div class = "highlight-python" > < div class = "highlight" > < pre > < span class = "c" > #!/usr/bin/python< / span >
< span class = "kn" > import< / span > < span class = "nn" > ansible.runner< / span >
< span class = "kn" > import< / span > < span class = "nn" > sys< / span >
< span class = "c" > # construct the ansible runner and execute on all hosts< / span >
< span class = "n" > results< / span > < span class = "o" > =< / span > < span class = "n" > ansible< / span > < span class = "o" > .< / span > < span class = "n" > runner< / span > < span class = "o" > .< / span > < span class = "n" > Runner< / span > < span class = "p" > (< / span >
< span class = "n" > pattern< / span > < span class = "o" > =< / span > < span class = "s" > ' *' < / span > < span class = "p" > ,< / span > < span class = "n" > forks< / span > < span class = "o" > =< / span > < span class = "mi" > 10< / span > < span class = "p" > ,< / span >
< span class = "n" > module_name< / span > < span class = "o" > =< / span > < span class = "s" > ' command' < / span > < span class = "p" > ,< / span > < span class = "n" > module_args< / span > < span class = "o" > =< / span > < span class = "p" > [< / span > < span class = "s" > ' /usr/bin/uptime' < / span > < span class = "p" > ],< / span >
< span class = "p" > )< / span > < span class = "o" > .< / span > < span class = "n" > run< / span > < span class = "p" > ()< / span >
< span class = "k" > if< / span > < span class = "n" > results< / span > < span class = "ow" > is< / span > < span class = "bp" > None< / span > < span class = "p" > :< / span >
< span class = "k" > print< / span > < span class = "s" > " No hosts found" < / span >
< span class = "n" > sys< / span > < span class = "o" > .< / span > < span class = "n" > exit< / span > < span class = "p" > (< / span > < span class = "mi" > 1< / span > < span class = "p" > )< / span >
< span class = "k" > print< / span > < span class = "s" > " UP ***********" < / span >
< span class = "k" > for< / span > < span class = "p" > (< / span > < span class = "n" > hostname< / span > < span class = "p" > ,< / span > < span class = "n" > result< / span > < span class = "p" > )< / span > < span class = "ow" > in< / span > < span class = "n" > results< / span > < span class = "p" > [< / span > < span class = "s" > ' contacted' < / span > < span class = "p" > ]< / span > < span class = "o" > .< / span > < span class = "n" > items< / span > < span class = "p" > ():< / span >
< span class = "k" > if< / span > < span class = "ow" > not< / span > < span class = "s" > ' failed' < / span > < span class = "ow" > in< / span > < span class = "n" > result< / span > < span class = "p" > :< / span >
< span class = "k" > print< / span > < span class = "s" > " < / span > < span class = "si" > %s< / span > < span class = "s" > > > > < / span > < span class = "si" > %s< / span > < span class = "s" > " < / span > < span class = "o" > %< / span > < span class = "p" > (< / span > < span class = "n" > hostname< / span > < span class = "p" > ,< / span > < span class = "n" > result< / span > < span class = "p" > [< / span > < span class = "s" > ' stdout' < / span > < span class = "p" > ])< / span >
< span class = "k" > print< / span > < span class = "s" > " FAILED *******" < / span >
< span class = "k" > for< / span > < span class = "p" > (< / span > < span class = "n" > hostname< / span > < span class = "p" > ,< / span > < span class = "n" > result< / span > < span class = "p" > )< / span > < span class = "ow" > in< / span > < span class = "n" > results< / span > < span class = "p" > [< / span > < span class = "s" > ' contacted' < / span > < span class = "p" > ]< / span > < span class = "o" > .< / span > < span class = "n" > items< / span > < span class = "p" > ():< / span >
< span class = "k" > if< / span > < span class = "s" > ' failed' < / span > < span class = "ow" > in< / span > < span class = "n" > result< / span > < span class = "p" > :< / span >
< span class = "k" > print< / span > < span class = "s" > " < / span > < span class = "si" > %s< / span > < span class = "s" > > > > < / span > < span class = "si" > %s< / span > < span class = "s" > " < / span > < span class = "o" > %< / span > < span class = "p" > (< / span > < span class = "n" > hostname< / span > < span class = "p" > ,< / span > < span class = "n" > result< / span > < span class = "p" > [< / span > < span class = "s" > ' msg' < / span > < span class = "p" > ])< / span >
< span class = "k" > print< / span > < span class = "s" > " DOWN *********" < / span >
< span class = "k" > for< / span > < span class = "p" > (< / span > < span class = "n" > hostname< / span > < span class = "p" > ,< / span > < span class = "n" > result< / span > < span class = "p" > )< / span > < span class = "ow" > in< / span > < span class = "n" > results< / span > < span class = "p" > [< / span > < span class = "s" > ' dark' < / span > < span class = "p" > ]< / span > < span class = "o" > .< / span > < span class = "n" > items< / span > < span class = "p" > ():< / span >
< span class = "k" > print< / span > < span class = "s" > " < / span > < span class = "si" > %s< / span > < span class = "s" > > > > < / span > < span class = "si" > %s< / span > < span class = "s" > " < / span > < span class = "o" > %< / span > < span class = "p" > (< / span > < span class = "n" > hostname< / span > < span class = "p" > ,< / span > < span class = "n" > result< / span > < span class = "p" > )< / span >
< / pre > < / div >
< / div >
< p > Advanced programmers may also wish to read the source to ansible itself, for
it uses the Runner() API (with all available options) to implement the
command line tools < tt class = "docutils literal" > < span class = "pre" > ansible< / span > < / tt > and < tt class = "docutils literal" > < span class = "pre" > ansible-playbook< / span > < / tt > .< / p >
< / div >
2012-03-07 17:35:18 +01:00
< / div >
< / div >
< / div >
< / div >
< div class = "sphinxsidebar" >
< div class = "sphinxsidebarwrapper" >
2012-03-09 13:42:53 +01:00
< h3 > < a href = "index.html" > Table Of Contents< / a > < / h3 >
< ul >
< li > < a class = "reference internal" href = "#" > Using the Python API< / a > < ul >
< li > < a class = "reference internal" href = "#detailed-api-example" > Detailed API Example< / a > < / li >
< / ul >
< / li >
< / ul >
2012-03-07 17:35:18 +01:00
< h4 > Previous topic< / h4 >
2012-03-09 04:50:12 +01:00
< p class = "topless" > < a href = "playbooks.html"
title="previous chapter">Playbooks: Ansible for Deployment, Configuration Management, and Orchestration< / a > < / p >
2012-03-07 17:35:18 +01:00
< h4 > Next topic< / h4 >
2012-03-08 19:53:48 +01:00
< p class = "topless" > < a href = "man.html"
title="next chapter">Man Pages< / a > < / p >
2012-03-07 17:35:18 +01:00
< h3 > This Page< / h3 >
< ul class = "this-page-menu" >
< li > < a href = "_sources/api.txt"
rel="nofollow">Show Source< / a > < / li >
< / ul >
< div id = "searchbox" style = "display: none" >
< h3 > Quick search< / h3 >
< form class = "search" action = "search.html" method = "get" >
2012-03-08 19:36:47 +01:00
< input type = "text" name = "q" / >
2012-03-07 17:35:18 +01:00
< input type = "submit" value = "Go" / >
< input type = "hidden" name = "check_keywords" value = "yes" / >
< input type = "hidden" name = "area" value = "default" / >
< / form >
< p class = "searchtip" style = "font-size: 90%" >
Enter search terms or a module, class or function name.
< / p >
< / div >
< script type = "text/javascript" > $ ( '#searchbox' ) . show ( 0 ) ; < / script >
< / div >
< / div >
< div class = "clearer" > < / div >
< / div >
< div class = "related" >
< h3 > Navigation< / h3 >
< ul >
< li class = "right" style = "margin-right: 10px" >
< a href = "genindex.html" title = "General Index"
>index< / a > < / li >
< li class = "right" >
2012-03-08 19:53:48 +01:00
< a href = "man.html" title = "Man Pages"
2012-03-07 17:35:18 +01:00
>next< / a > |< / li >
< li class = "right" >
2012-03-09 04:50:12 +01:00
< a href = "playbooks.html" title = "Playbooks: Ansible for Deployment, Configuration Management, and Orchestration"
2012-03-07 17:35:18 +01:00
>previous< / a > |< / li >
< li > < a href = "index.html" > Ansible v0.0.1 documentation< / a > » < / li >
< / ul >
< / div >
< div class = "footer" >
© Copyright 2012 Michael DeHaan.
2012-03-09 13:42:53 +01:00
Last updated on Mar 09, 2012.
2012-03-08 19:36:47 +01:00
Created using < a href = "http://sphinx.pocoo.org/" > Sphinx< / a > 1.0.8.
2012-03-07 17:35:18 +01:00
< / div >
< / body >
< / html >