mirror of https://github.com/ansible/ansible.git
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
491 lines
33 KiB
HTML
491 lines
33 KiB
HTML
13 years ago
|
|
||
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
|
||
|
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
||
|
|
||
12 years ago
|
|
||
13 years ago
|
<html xmlns="http://www.w3.org/1999/xhtml">
|
||
|
<head>
|
||
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||
|
|
||
12 years ago
|
<title>API & Integrations — Ansible Documentation</title>
|
||
12 years ago
|
|
||
13 years ago
|
<link rel="stylesheet" href="_static/default.css" type="text/css" />
|
||
|
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
|
||
13 years ago
|
<link rel="stylesheet" href="_static/bootstrap.css" type="text/css" />
|
||
|
<link rel="stylesheet" href="_static/bootstrap-sphinx.css" type="text/css" />
|
||
12 years ago
|
|
||
13 years ago
|
<script type="text/javascript">
|
||
|
var DOCUMENTATION_OPTIONS = {
|
||
|
URL_ROOT: '',
|
||
13 years ago
|
VERSION: '0.01',
|
||
13 years ago
|
COLLAPSE_INDEX: false,
|
||
|
FILE_SUFFIX: '.html',
|
||
13 years ago
|
HAS_SOURCE: false
|
||
13 years ago
|
};
|
||
|
</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>
|
||
13 years ago
|
<script type="text/javascript" src="_static/bootstrap-dropdown.js"></script>
|
||
|
<script type="text/javascript" src="_static/bootstrap-scrollspy.js"></script>
|
||
13 years ago
|
<link rel="shortcut icon" href="_static/favicon.ico"/>
|
||
12 years ago
|
<link rel="top" title="Ansible Documentation" href="index.html" />
|
||
13 years ago
|
<link rel="next" title="Module Development" href="moduledev.html" />
|
||
13 years ago
|
<link rel="prev" title="Best Practices" href="bestpractices.html" />
|
||
13 years ago
|
<script type="text/javascript">
|
||
|
(function () {
|
||
|
/**
|
||
|
* Patch TOC list.
|
||
|
*
|
||
|
* Will mutate the underlying span to have a correct ul for nav.
|
||
|
*
|
||
|
* @param $span: Span containing nested UL's to mutate.
|
||
|
* @param minLevel: Starting level for nested lists. (1: global, 2: local).
|
||
|
*/
|
||
|
var patchToc = function ($span, minLevel) {
|
||
|
var $tocList = $("<ul/>").attr('class', "dropdown-menu"),
|
||
|
findA;
|
||
|
|
||
|
// Find all a "internal" tags, traversing recursively.
|
||
|
findA = function ($elem, level) {
|
||
|
var level = level || 0,
|
||
|
$items = $elem.find("> li > a.internal, > ul, > li > ul");
|
||
|
|
||
|
// Iterate everything in order.
|
||
|
$items.each(function (index, item) {
|
||
|
var $item = $(item),
|
||
|
tag = item.tagName.toLowerCase(),
|
||
|
pad = 10 + ((level - minLevel) * 10);
|
||
|
|
||
|
if (tag === 'a' && level >= minLevel) {
|
||
|
// Add to existing padding.
|
||
|
$item.css('padding-left', pad + "px");
|
||
|
// Add list element.
|
||
|
$tocList.append($("<li/>").append($item));
|
||
|
} else if (tag === 'ul') {
|
||
|
// Recurse.
|
||
|
findA($item, level + 1);
|
||
|
}
|
||
|
});
|
||
|
};
|
||
|
|
||
|
// Start construction and return.
|
||
|
findA($span);
|
||
|
|
||
|
// Wipe out old list and patch in new one.
|
||
|
return $span.empty("ul").append($tocList);
|
||
|
};
|
||
|
|
||
|
$(document).ready(function () {
|
||
|
// Patch the global and local TOC's to be bootstrap-compliant.
|
||
|
patchToc($("span.globaltoc"), 1);
|
||
|
patchToc($("span.localtoc"), 2);
|
||
|
|
||
|
// Activate.
|
||
|
$('#topbar').dropdown();
|
||
|
});
|
||
|
}());
|
||
13 years ago
|
</script>
|
||
13 years ago
|
|
||
13 years ago
|
<script type="text/javascript">
|
||
|
|
||
|
var _gaq = _gaq || [];
|
||
|
_gaq.push(['_setAccount', 'UA-29861888-1']);
|
||
|
_gaq.push(['_trackPageview']);
|
||
|
|
||
|
(function() {
|
||
|
var ga = document.createElement('script'); ga.type =
|
||
|
'text/javascript'; ga.async = true;
|
||
|
ga.src = ('https:' == document.location.protocol ? 'https://ssl' :
|
||
|
'http://www') + '.google-analytics.com/ga.js';
|
||
|
var s = document.getElementsByTagName('script')[0];
|
||
|
s.parentNode.insertBefore(ga, s);
|
||
|
})();
|
||
|
|
||
13 years ago
|
</script>
|
||
13 years ago
|
|
||
|
<script type="text/javascript">
|
||
|
(function() {
|
||
|
var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
|
||
|
po.src = 'https://apis.google.com/js/plusone.js';
|
||
|
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
|
||
|
})();
|
||
|
</script>
|
||
|
|
||
13 years ago
|
<script>(function(d, s, id) {
|
||
|
var js, fjs = d.getElementsByTagName(s)[0];
|
||
|
if (d.getElementById(id)) return;
|
||
|
js = d.createElement(s); js.id = id;
|
||
|
js.src = "//connect.facebook.net/en_US/all.js#xfbml=1";
|
||
|
fjs.parentNode.insertBefore(js, fjs);
|
||
|
}(document, 'script', 'facebook-jssdk'));</script>
|
||
13 years ago
|
|
||
13 years ago
|
</head>
|
||
|
<body>
|
||
13 years ago
|
<div class="topbar" data-scrollspy="scrollspy" >
|
||
|
<div class="topbar-inner">
|
||
|
<div class="container">
|
||
12 years ago
|
<a class="brand" href="index.html">Ansible Documentation</a>
|
||
13 years ago
|
<ul class="nav">
|
||
|
|
||
12 years ago
|
<li><a href="/">Home</A>
|
||
|
<li class="dropdown" data-dropdown="dropdown">
|
||
13 years ago
|
<a href="index.html"
|
||
13 years ago
|
class="dropdown-toggle">Chapter</a>
|
||
13 years ago
|
<span class="globaltoc"><ul class="current">
|
||
13 years ago
|
<li class="toctree-l1"><a class="reference internal" href="gettingstarted.html">Getting Started</a></li>
|
||
|
<li class="toctree-l1"><a class="reference internal" href="patterns.html">Inventory & Patterns</a></li>
|
||
12 years ago
|
<li class="toctree-l1"><a class="reference internal" href="examples.html">Command Line Examples And Next Steps</a></li>
|
||
13 years ago
|
<li class="toctree-l1"><a class="reference internal" href="modules.html">Ansible Modules</a></li>
|
||
13 years ago
|
<li class="toctree-l1"><a class="reference internal" href="YAMLSyntax.html">YAML Syntax</a></li>
|
||
13 years ago
|
<li class="toctree-l1"><a class="reference internal" href="playbooks.html">Playbooks</a></li>
|
||
13 years ago
|
<li class="toctree-l1"><a class="reference internal" href="playbooks2.html">Advanced Playbooks</a></li>
|
||
13 years ago
|
<li class="toctree-l1"><a class="reference internal" href="bestpractices.html">Best Practices</a></li>
|
||
13 years ago
|
<li class="toctree-l1 current"><a class="current reference internal" href="">API & Integrations</a></li>
|
||
13 years ago
|
<li class="toctree-l1"><a class="reference internal" href="moduledev.html">Module Development</a></li>
|
||
13 years ago
|
</ul>
|
||
|
</span>
|
||
13 years ago
|
</li>
|
||
|
<li class="dropdown" data-dropdown="dropdown">
|
||
|
<a href="#"
|
||
|
class="dropdown-toggle">Page</a>
|
||
|
<span class="localtoc"><ul>
|
||
|
<li><a class="reference internal" href="#">API & Integrations</a><ul>
|
||
|
<li><a class="reference internal" href="#python-api">Python API</a><ul>
|
||
|
<li><a class="reference internal" href="#detailed-api-example">Detailed API Example</a></li>
|
||
|
</ul>
|
||
|
</li>
|
||
12 years ago
|
<li><a class="reference internal" href="#plugins-repository">Plugins Repository</a></li>
|
||
|
<li><a class="reference internal" href="#external-inventory-scripts">External Inventory Scripts</a><ul>
|
||
13 years ago
|
<li><a class="reference internal" href="#script-conventions">Script Conventions</a></li>
|
||
|
<li><a class="reference internal" href="#example-the-cobbler-external-inventory-script">Example: The Cobbler External Inventory Script</a></li>
|
||
13 years ago
|
<li><a class="reference internal" href="#example-aws-ec2-external-inventory-script">Example: AWS EC2 External Inventory Script</a></li>
|
||
12 years ago
|
<li><a class="reference internal" href="#example-openstack">Example: OpenStack</a></li>
|
||
13 years ago
|
</ul>
|
||
|
</li>
|
||
12 years ago
|
<li><a class="reference internal" href="#callback-plugins">Callback Plugins</a></li>
|
||
|
<li><a class="reference internal" href="#connection-type-plugins">Connection Type Plugins</a></li>
|
||
13 years ago
|
</ul>
|
||
|
</li>
|
||
|
</ul>
|
||
|
</span>
|
||
13 years ago
|
</li>
|
||
|
|
||
|
</ul>
|
||
|
<ul class="nav secondary-nav">
|
||
|
|
||
|
|
||
|
<form class="pull-left" action="search.html" method="get">
|
||
|
<input type="text" name="q" placeholder="Search" />
|
||
|
<input type="hidden" name="check_keywords" value="yes" />
|
||
|
<input type="hidden" name="area" value="default" />
|
||
|
</form>
|
||
|
|
||
|
</ul>
|
||
|
</div>
|
||
|
</div>
|
||
|
</div>
|
||
|
|
||
13 years ago
|
|
||
13 years ago
|
<div class="container">
|
||
|
|
||
13 years ago
|
<div class="section" id="api-integrations">
|
||
12 years ago
|
<h1><a class="toc-backref" href="#table-of-contents">API & Integrations</a><a class="headerlink" href="#api-integrations" title="Permalink to this headline">¶</a></h1>
|
||
12 years ago
|
<p>There are several interesting ways to use Ansible from an API perspective. You can use
|
||
|
the Ansible python API to control nodes, you can extend Ansible to respond to various python events,
|
||
|
and you can plug in inventory data from external data sources. Ansible is written in its own
|
||
|
API so you have a considerable amount of power across the board.</p>
|
||
12 years ago
|
<div class="contents topic" id="table-of-contents">
|
||
|
<p class="topic-title first"><cite>Table of contents</cite></p>
|
||
|
<ul class="simple">
|
||
|
<li><a class="reference internal" href="#api-integrations" id="id2">API & Integrations</a><ul>
|
||
|
<li><a class="reference internal" href="#python-api" id="id3">Python API</a></li>
|
||
12 years ago
|
<li><a class="reference internal" href="#plugins-repository" id="id4">Plugins Repository</a></li>
|
||
|
<li><a class="reference internal" href="#external-inventory-scripts" id="id5">External Inventory Scripts</a></li>
|
||
|
<li><a class="reference internal" href="#callback-plugins" id="id6">Callback Plugins</a></li>
|
||
|
<li><a class="reference internal" href="#connection-type-plugins" id="id7">Connection Type Plugins</a></li>
|
||
12 years ago
|
</ul>
|
||
|
</li>
|
||
|
</ul>
|
||
|
</div>
|
||
13 years ago
|
<div class="section" id="python-api">
|
||
12 years ago
|
<h2><a class="toc-backref" href="#table-of-contents">Python API</a><a class="headerlink" href="#python-api" title="Permalink to this headline">¶</a></h2>
|
||
13 years ago
|
<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>
|
||
13 years ago
|
<div class="section" id="detailed-api-example">
|
||
13 years ago
|
<h3>Detailed API Example<a class="headerlink" href="#detailed-api-example" title="Permalink to this headline">¶</a></h3>
|
||
13 years ago
|
<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>
|
||
12 years ago
|
<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="s">'/usr/bin/uptime'</span><span class="p">,</span>
|
||
13 years ago
|
<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>
|
||
13 years ago
|
</div>
|
||
12 years ago
|
<div class="section" id="plugins-repository">
|
||
|
<h2><a class="toc-backref" href="#table-of-contents">Plugins Repository</a><a class="headerlink" href="#plugins-repository" title="Permalink to this headline">¶</a></h2>
|
||
|
<p>The remainder of features in the API docs have components available in the <a class="reference external" href="http://github.com/ansible/ansible-plugins">ansible-plugins</a> repository. Please consult this repository for more information. Send us a github pull request if you develop any interesting features.</p>
|
||
|
</div>
|
||
|
<div class="section" id="external-inventory-scripts">
|
||
|
<h2><a class="toc-backref" href="#table-of-contents">External Inventory Scripts</a><a class="headerlink" href="#external-inventory-scripts" title="Permalink to this headline">¶</a></h2>
|
||
13 years ago
|
<p>Often a user of a configuration management system will want to keep inventory
|
||
|
in a different system. Frequent examples include LDAP, <a class="reference external" href="http://cobbler.github.com">Cobbler</a>,
|
||
|
or a piece of expensive enterprisey CMDB software. Ansible easily supports all
|
||
12 years ago
|
of these options via an external inventory system. The ansible-plugins repo contains some of these already – including options for EC2/Eucalyptus and OpenStack, which will be detailed below.</p>
|
||
12 years ago
|
<p>It’s possible to write an external inventory script in any language. If you are familiar with Puppet terminology, this concept is basically the same as ‘external nodes’, with the slight difference that it also defines which hosts are managed.</p>
|
||
13 years ago
|
<div class="section" id="script-conventions">
|
||
|
<h3>Script Conventions<a class="headerlink" href="#script-conventions" title="Permalink to this headline">¶</a></h3>
|
||
13 years ago
|
<p>When the external node script is called with the single argument ‘–list’, the script must return a JSON hash/dictionary of all the groups to be managed, with a list of each host/IP as the value for each hash/dictionary element, like so:</p>
|
||
13 years ago
|
<div class="highlight-python"><div class="highlight"><pre><span class="p">{</span>
|
||
|
<span class="s">'databases'</span> <span class="p">:</span> <span class="p">[</span> <span class="s">'host1.example.com'</span><span class="p">,</span> <span class="s">'host2.example.com'</span> <span class="p">],</span>
|
||
|
<span class="s">'webservers'</span> <span class="p">:</span> <span class="p">[</span> <span class="s">'host2.example.com'</span><span class="p">,</span> <span class="s">'host3.example.com'</span> <span class="p">],</span>
|
||
|
<span class="s">'atlanta'</span> <span class="p">:</span> <span class="p">[</span> <span class="s">'host1.example.com'</span><span class="p">,</span> <span class="s">'host4.example.com'</span><span class="p">,</span> <span class="s">'host5.example.com'</span> <span class="p">]</span>
|
||
|
<span class="p">}</span>
|
||
|
</pre></div>
|
||
|
</div>
|
||
13 years ago
|
<p>When called with the arguments ‘–host <hostname>’ (where <hostname> is a host from above), the script must return either an empty JSON
|
||
|
hash/dictionary, or a list of key/value variables to make available to templates or playbooks. Returning variables is optional,
|
||
13 years ago
|
if the script does not wish to do this, returning an empty hash/dictionary is the way to go:</p>
|
||
|
<div class="highlight-python"><div class="highlight"><pre><span class="p">{</span>
|
||
|
<span class="s">'favcolor'</span> <span class="p">:</span> <span class="s">'red'</span><span class="p">,</span>
|
||
|
<span class="s">'ntpserver'</span> <span class="p">:</span> <span class="s">'wolf.example.com'</span><span class="p">,</span>
|
||
|
<span class="s">'monitoring'</span> <span class="p">:</span> <span class="s">'pack.example.com'</span>
|
||
|
<span class="p">}</span>
|
||
|
</pre></div>
|
||
|
</div>
|
||
13 years ago
|
</div>
|
||
|
<div class="section" id="example-the-cobbler-external-inventory-script">
|
||
|
<h3>Example: The Cobbler External Inventory Script<a class="headerlink" href="#example-the-cobbler-external-inventory-script" title="Permalink to this headline">¶</a></h3>
|
||
13 years ago
|
<p>It is expected that many Ansible users will also be <a class="reference external" href="http://cobbler.github.com">Cobbler</a> users. Cobbler has a generic
|
||
|
layer that allows it to represent data for multiple configuration management systems (even at the same time), and has
|
||
|
been referred to as a ‘lightweight CMDB’ by some admins. This particular script will communicate with Cobbler
|
||
|
using Cobbler’s XMLRPC API.</p>
|
||
12 years ago
|
<p>To tie Ansible’s inventory to Cobbler (optional), copy <a class="reference external" href="https://github.com/ansible/ansible-plugins/blob/master/inventory/cobbler.py">this script</a> to /etc/ansible/hosts and <cite>chmod +x</cite> the file. cobblerd will now need
|
||
13 years ago
|
to be running when you are using Ansible.</p>
|
||
|
<p>Test the file by running <cite>./etc/ansible/hosts</cite> directly. You should see some JSON data output, but it may not have
|
||
|
anything in it just yet.</p>
|
||
|
<p>Let’s explore what this does. In cobbler, assume a scenario somewhat like the following:</p>
|
||
|
<div class="highlight-python"><pre>cobbler profile add --name=webserver --distro=CentOS6-x86_64
|
||
|
cobbler profile edit --name=webserver --mgmt-classes="webserver" --ksmeta="a=2 b=3"
|
||
|
cobbler system edit --name=foo --dns-name="foo.example.com" --mgmt-classes="atlanta" --ksmeta="c=4"
|
||
|
cobbler system edit --name=bar --dns-name="bar.example.com" --mgmt-classes="atlanta" --ksmeta="c=5"</pre>
|
||
|
</div>
|
||
|
<p>In the example above, the system ‘foo.example.com’ will be addressable by ansible directly, but will also be addressable when using the group names ‘webserver’ or ‘atlanta’. Since Ansible uses SSH, we’ll try to contract system foo over ‘foo.example.com’, only, never just ‘foo’. Similarly, if you try “ansible foo” it wouldn’t find the system... but “ansible ‘foo*’” would, because the system DNS name starts with ‘foo’.</p>
|
||
|
<p>The script doesn’t just provide host and group info. In addition, as a bonus, when the ‘setup’ module is run (which happens automatically when using playbooks), the variables ‘a’, ‘b’, and ‘c’ will all be auto-populated in the templates:</p>
|
||
|
<div class="highlight-python"><pre># file: /srv/motd.j2
|
||
|
Welcome, I am templated with a value of a={{ a }}, b={{ b }}, and c={{ c }}</pre>
|
||
|
</div>
|
||
|
<p>Which could be executed just like this:</p>
|
||
|
<div class="highlight-python"><pre>ansible webserver -m setup
|
||
|
ansible webserver -m template -a "src=/tmp/motd.j2 dest=/etc/motd"</pre>
|
||
|
</div>
|
||
12 years ago
|
<div class="admonition note">
|
||
|
<p class="first admonition-title">Note</p>
|
||
|
<p class="last">The name ‘webserver’ came from cobbler, as did the variables for
|
||
|
the config file. You can still pass in your own variables like
|
||
|
normal in Ansible, but variables from the external inventory script
|
||
|
will override any that have the same name.</p>
|
||
|
</div>
|
||
13 years ago
|
<p>So, with the template above (motd.j2), this would result in the following data being written to /etc/motd for system ‘foo’:</p>
|
||
|
<div class="highlight-python"><pre>Welcome, I am templated with a value of a=2, b=3, and c=4</pre>
|
||
|
</div>
|
||
|
<p>And on system ‘bar’ (bar.example.com):</p>
|
||
|
<div class="highlight-python"><pre>Welcome, I am templated with a value of a=2, b=3, and c=5</pre>
|
||
|
</div>
|
||
|
<p>And technically, though there is no major good reason to do it, this also works too:</p>
|
||
|
<div class="highlight-python"><pre>ansible webserver -m shell -a "echo {{ a }}"</pre>
|
||
|
</div>
|
||
|
<p>So in other words, you can use those variables in arguments/actions as well. You might use this to name
|
||
13 years ago
|
a conf.d file appropriately or something similar. Who knows?</p>
|
||
13 years ago
|
<p>So that’s the Cobbler integration support – using the cobbler script as an example, it should be trivial to adapt Ansible to pull inventory, as well as variable information, from any data source. If you create anything interesting, please share with the mailing list, and we can keep it in the source code tree for others to use.</p>
|
||
13 years ago
|
</div>
|
||
|
<div class="section" id="example-aws-ec2-external-inventory-script">
|
||
|
<h3>Example: AWS EC2 External Inventory Script<a class="headerlink" href="#example-aws-ec2-external-inventory-script" title="Permalink to this headline">¶</a></h3>
|
||
12 years ago
|
<p>If you use Amazon Web Services EC2, maintaining an inventory file might not be the best approach. For this reason, you can use the <a class="reference external" href="https://github.com/ansible/ansible-plugins/blob/master/inventory/ec2.py">EC2 external inventory</a> script.</p>
|
||
13 years ago
|
<p>You can use this script in one of two ways. The easiest is to use Ansible’s <tt class="docutils literal"><span class="pre">-i</span></tt> command line option and specify the path to the script.</p>
|
||
|
<blockquote>
|
||
12 years ago
|
<div>ansible -i ec2.py -u ubuntu us-east-1d -m ping</div></blockquote>
|
||
13 years ago
|
<p>The second option is to copy the script to <cite>/etc/ansible/hosts</cite> and <cite>chmod +x</cite> it. You will also need to copy the <tt class="docutils literal"><span class="pre">ec2.ini</span></tt> file to <cite>/etc/ansible/ec2.ini</cite>. Then you can run ansible as you would normally.</p>
|
||
|
<p>To successfully make an API call to AWS, you will need to configure Boto (the Python interface to AWS). There are a <a class="reference external" href="http://docs.pythonboto.org/en/latest/boto_config_tut.html">variety of methods</a> available, but the simplest is just to export two environment variables:</p>
|
||
|
<blockquote>
|
||
|
<div>export AWS_ACCESS_KEY_ID=’AK123’
|
||
|
export AWS_SECRET_ACCESS_KEY=’abc123’</div></blockquote>
|
||
|
<p>You can test the script by itself to make sure your config is correct</p>
|
||
|
<blockquote>
|
||
|
<div>cd examples/scripts
|
||
|
./ec2_external_inventory.py –list</div></blockquote>
|
||
|
<p>After a few moments, you should see your entire EC2 inventory across all regions in JSON.</p>
|
||
|
<p>Since each region requires its own API call, if you are only using a small set of regions, feel free to edit <tt class="docutils literal"><span class="pre">ec2.ini</span></tt> and list only the regions you are interested in. There are other config options in <tt class="docutils literal"><span class="pre">ec2.ini</span></tt> including cache control, and destination variables.</p>
|
||
|
<p>At their heart, inventory files are simply a mapping from some name to a destination address. The default <tt class="docutils literal"><span class="pre">ec2.ini</span></tt> settings are configured for running Ansible from outside EC2 (from your laptop for example). If you are running Ansible from within EC2, internal DNS names and IP addresses may make more sense than public DNS names. In this case, you can modify the <tt class="docutils literal"><span class="pre">destination_variable</span></tt> in <tt class="docutils literal"><span class="pre">ec2.ini</span></tt> to be the private DNS name of an instance. This is particularly important when running Ansible within a private subnet inside a VPC, where the only way to access an instance is via its private IP address. For VPC instances, <cite>vpc_destination_variable</cite> in <tt class="docutils literal"><span class="pre">ec2.ini</span></tt> provides a means of using which ever <a class="reference external" href="http://docs.pythonboto.org/en/latest/ref/ec2.html#module-boto.ec2.instance">boto.ec2.instance variable</a> makes the most sense for your use case.</p>
|
||
|
<p>The EC2 external inventory provides mappings to instances from several groups:</p>
|
||
|
<dl class="docutils">
|
||
|
<dt>Instance ID</dt>
|
||
|
<dd>These are groups of one since instance IDs are unique.
|
||
|
e.g.
|
||
|
<tt class="docutils literal"><span class="pre">i-00112233</span></tt>
|
||
|
<tt class="docutils literal"><span class="pre">i-a1b1c1d1</span></tt></dd>
|
||
|
<dt>Region</dt>
|
||
|
<dd>A group of all instances in an AWS region.
|
||
|
e.g.
|
||
|
<tt class="docutils literal"><span class="pre">us-east-1</span></tt>
|
||
|
<tt class="docutils literal"><span class="pre">us-west-2</span></tt></dd>
|
||
|
<dt>Availability Zone</dt>
|
||
|
<dd>A group of all instances in an availability zone.
|
||
|
e.g.
|
||
|
<tt class="docutils literal"><span class="pre">us-east-1a</span></tt>
|
||
|
<tt class="docutils literal"><span class="pre">us-east-1b</span></tt></dd>
|
||
|
<dt>Security Group</dt>
|
||
|
<dd>Instances belong to one or more security groups. A group is created for each security group, with all characters except alphanumerics, dashes (-) converted to underscores (_). Each group is prefixed by <tt class="docutils literal"><span class="pre">security_group_</span></tt>
|
||
|
e.g.
|
||
|
<tt class="docutils literal"><span class="pre">security_group_default</span></tt>
|
||
|
<tt class="docutils literal"><span class="pre">security_group_webservers</span></tt>
|
||
|
<tt class="docutils literal"><span class="pre">security_group_Pete_s_Fancy_Group</span></tt></dd>
|
||
|
<dt>Tags</dt>
|
||
|
<dd>Each instance can have a variety of key/value pairs associated with it called Tags. The most common tag key is ‘Name’, though anything is possible. Each key/value pair is its own group of instances, again with special characters converted to underscores, in the format <tt class="docutils literal"><span class="pre">tag_KEY_VALUE</span></tt>
|
||
|
e.g.
|
||
|
<tt class="docutils literal"><span class="pre">tag_Name_Web</span></tt>
|
||
|
<tt class="docutils literal"><span class="pre">tag_Name_redis-master-001</span></tt>
|
||
|
<tt class="docutils literal"><span class="pre">tag_aws_cloudformation_logical-id_WebServerGroup</span></tt></dd>
|
||
|
</dl>
|
||
|
<p>When the Ansible is interacting with a specific server, the EC2 inventory script is called again with the <tt class="docutils literal"><span class="pre">--host</span> <span class="pre">HOST</span></tt> option. This looks up the HOST in the index cache to get the instance ID, and then makes an API call to AWS to get information about that specific instance. It then makes information about that instance available as variables to your playbooks. Each variable is prefixed by <tt class="docutils literal"><span class="pre">ec2_</span></tt>. Here are some of the variables available:</p>
|
||
|
<ul class="simple">
|
||
|
<li>ec2_architecture</li>
|
||
|
<li>ec2_description</li>
|
||
|
<li>ec2_dns_name</li>
|
||
|
<li>ec2_id</li>
|
||
|
<li>ec2_image_id</li>
|
||
|
<li>ec2_instance_type</li>
|
||
|
<li>ec2_ip_address</li>
|
||
|
<li>ec2_kernel</li>
|
||
|
<li>ec2_key_name</li>
|
||
|
<li>ec2_launch_time</li>
|
||
|
<li>ec2_monitored</li>
|
||
|
<li>ec2_ownerId</li>
|
||
|
<li>ec2_placement</li>
|
||
|
<li>ec2_platform</li>
|
||
|
<li>ec2_previous_state</li>
|
||
|
<li>ec2_private_dns_name</li>
|
||
|
<li>ec2_private_ip_address</li>
|
||
|
<li>ec2_public_dns_name</li>
|
||
|
<li>ec2_ramdisk</li>
|
||
|
<li>ec2_region</li>
|
||
|
<li>ec2_root_device_name</li>
|
||
|
<li>ec2_root_device_type</li>
|
||
|
<li>ec2_security_group_ids</li>
|
||
|
<li>ec2_security_group_names</li>
|
||
|
<li>ec2_spot_instance_request_id</li>
|
||
|
<li>ec2_state</li>
|
||
|
<li>ec2_state_code</li>
|
||
|
<li>ec2_state_reason</li>
|
||
|
<li>ec2_status</li>
|
||
|
<li>ec2_subnet_id</li>
|
||
|
<li>ec2_tag_Name</li>
|
||
|
<li>ec2_tenancy</li>
|
||
|
<li>ec2_virtualization_type</li>
|
||
|
<li>ec2_vpc_id</li>
|
||
|
</ul>
|
||
|
<p>Both <tt class="docutils literal"><span class="pre">ec2_security_group_ids</span></tt> and <tt class="docutils literal"><span class="pre">ec2_security_group_names</span></tt> are comma-separated lists of all security groups. Each EC2 tag is a variable in the format <tt class="docutils literal"><span class="pre">ec2_tag_KEY</span></tt>.</p>
|
||
|
<p>To see the complete list of variables available for an instance, run the script by itself:</p>
|
||
12 years ago
|
<div class="highlight-python"><pre>cd examples/scripts
|
||
|
./ec2_external_inventory.py --host ec2-12-12-12-12.compute-1.amazonaws.com</pre>
|
||
|
</div>
|
||
12 years ago
|
</div>
|
||
|
<div class="section" id="example-openstack">
|
||
|
<h3>Example: OpenStack<a class="headerlink" href="#example-openstack" title="Permalink to this headline">¶</a></h3>
|
||
|
<p>Though not detailed here in as much depth as the EC2 module, there’s also a OpenStack Nova external inventory source in the ansible-plugins repository. See the inline comments in the module source.</p>
|
||
|
</div>
|
||
|
</div>
|
||
|
<div class="section" id="callback-plugins">
|
||
|
<h2><a class="toc-backref" href="#table-of-contents">Callback Plugins</a><a class="headerlink" href="#callback-plugins" title="Permalink to this headline">¶</a></h2>
|
||
|
<p>Ansible can be configured via code to respond to external events. This can include enhancing logging, signalling an external software
|
||
|
system, or even (yes, really) making sound effects. Some examples are contained in the ansible-plugins repository.</p>
|
||
|
</div>
|
||
|
<div class="section" id="connection-type-plugins">
|
||
|
<h2><a class="toc-backref" href="#table-of-contents">Connection Type Plugins</a><a class="headerlink" href="#connection-type-plugins" title="Permalink to this headline">¶</a></h2>
|
||
|
<p>By default, ansible ships with a ‘paramiko’ SSH, native ssh (just called ‘ssh’), and ‘local’ connection type, which can be used
|
||
|
in playbooks and with /usr/bin/ansible to decide how you want to talk to remote machines. The basics of these connection types
|
||
|
are covered in the ‘getting started’ section. Should you want to extend Ansible to support other transports (SNMP? Message bus?
|
||
|
Carrier Pigeon?) it’s as simple as copying the format of one of the existing modules and dropping it into the connection plugins
|
||
|
directory.</p>
|
||
13 years ago
|
<div class="admonition-see-also admonition seealso">
|
||
|
<p class="first admonition-title">See also</p>
|
||
|
<dl class="last docutils">
|
||
|
<dt><a class="reference internal" href="modules.html"><em>Ansible Modules</em></a></dt>
|
||
|
<dd>List of built-in modules</dd>
|
||
13 years ago
|
<dt><a class="reference external" href="http://groups.google.com/group/ansible-project">Mailing List</a></dt>
|
||
|
<dd>Questions? Help? Ideas? Stop by the list on Google Groups</dd>
|
||
|
<dt><a class="reference external" href="http://irc.freenode.net">irc.freenode.net</a></dt>
|
||
|
<dd>#ansible IRC chat channel</dd>
|
||
13 years ago
|
</dl>
|
||
|
</div>
|
||
13 years ago
|
</div>
|
||
|
</div>
|
||
13 years ago
|
|
||
|
|
||
13 years ago
|
<br/>
|
||
13 years ago
|
</div>
|
||
13 years ago
|
<footer class="footer">
|
||
12 years ago
|
<div class="container">
|
||
12 years ago
|
<p>
|
||
13 years ago
|
<p>
|
||
13 years ago
|
© Copyright 2012 Michael DeHaan.<br/>
|
||
12 years ago
|
Last updated on Oct 08, 2012.<br/>
|
||
13 years ago
|
</p>
|
||
12 years ago
|
</div>
|
||
13 years ago
|
</footer>
|
||
13 years ago
|
</body>
|
||
|
</html>
|