ansible/patterns.html

400 lines
19 KiB
HTML
Raw Normal View History

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-05-13 17:00:02 +02:00
<title>Inventory &amp; Patterns &mdash; Ansible - SSH-Based Configuration Management &amp; Deployment</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" />
<link rel="stylesheet" href="_static/bootstrap.css" type="text/css" />
<link rel="stylesheet" href="_static/bootstrap-sphinx.css" type="text/css" />
2012-03-07 17:35:18 +01:00
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: '',
VERSION: '0.01',
2012-03-07 17:35:18 +01:00
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
2012-03-09 17:50:07 +01:00
HAS_SOURCE: false
2012-03-07 17:35:18 +01:00
};
</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>
<script type="text/javascript" src="_static/bootstrap-dropdown.js"></script>
<script type="text/javascript" src="_static/bootstrap-scrollspy.js"></script>
2012-04-01 04:44:02 +02:00
<link rel="shortcut icon" href="_static/favicon.ico"/>
<link rel="top" title="Ansible - SSH-Based Configuration Management &amp; Deployment" href="index.html" />
2012-08-17 03:12:36 +02:00
<link rel="next" title="Command Line Examples And Next Steps" href="examples.html" />
2012-05-13 17:00:02 +02:00
<link rel="prev" title="Getting Started" href="gettingstarted.html" />
<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();
});
}());
</script>
2012-03-10 04:31:54 +01:00
<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);
})();
</script>
<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>
2012-04-28 16:42:32 +02:00
<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>
2012-03-07 17:35:18 +01:00
</head>
<body>
<div class="topbar" data-scrollspy="scrollspy" >
<div class="topbar-inner">
<div class="container">
2012-04-18 02:47:29 +02:00
<!-- <a class="brand" href="index.html">Ansible</a> -->
<ul class="nav">
<li class="dropdown" data-dropdown="dropdown">
<a href="index.html"
class="dropdown-toggle">Chapter</a>
<span class="globaltoc"><ul class="current">
2012-05-13 17:00:02 +02:00
<li class="toctree-l1"><a class="reference internal" href="gettingstarted.html">Getting Started</a></li>
<li class="toctree-l1 current"><a class="current reference internal" href="">Inventory &amp; Patterns</a></li>
2012-08-17 03:12:36 +02:00
<li class="toctree-l1"><a class="reference internal" href="examples.html">Command Line Examples And Next Steps</a></li>
<li class="toctree-l1"><a class="reference internal" href="modules.html">Ansible Modules</a></li>
<li class="toctree-l1"><a class="reference internal" href="YAMLSyntax.html">YAML Syntax</a></li>
<li class="toctree-l1"><a class="reference internal" href="playbooks.html">Playbooks</a></li>
2012-05-13 17:00:02 +02:00
<li class="toctree-l1"><a class="reference internal" href="playbooks2.html">Advanced Playbooks</a></li>
<li class="toctree-l1"><a class="reference internal" href="bestpractices.html">Best Practices</a></li>
<li class="toctree-l1"><a class="reference internal" href="api.html">API &amp; Integrations</a></li>
2012-05-13 17:00:02 +02:00
<li class="toctree-l1"><a class="reference internal" href="moduledev.html">Module Development</a></li>
<li class="toctree-l1"><a class="reference internal" href="faq.html">FAQ</a></li>
2012-07-26 05:33:32 +02:00
<li class="toctree-l1"><a class="reference internal" href="who_uses_ansible.html">Who Uses Ansible</a></li>
</ul>
</span>
</li>
<li class="dropdown" data-dropdown="dropdown">
<a href="#"
class="dropdown-toggle">Page</a>
<span class="localtoc"><ul>
2012-05-13 17:00:02 +02:00
<li><a class="reference internal" href="#">Inventory &amp; Patterns</a><ul>
<li><a class="reference internal" href="#hosts-and-groups">Hosts and Groups</a></li>
<li><a class="reference internal" href="#selecting-targets">Selecting Targets</a></li>
<li><a class="reference internal" href="#host-variables">Host Variables</a></li>
<li><a class="reference internal" href="#group-variables">Group Variables</a></li>
2012-05-10 07:41:31 +02:00
<li><a class="reference internal" href="#groups-of-groups-and-group-variables">Groups of Groups, and Group Variables</a></li>
2012-07-20 17:51:03 +02:00
<li><a class="reference internal" href="#splitting-out-host-and-group-specific-data">Splitting Out Host and Group Specific Data</a></li>
<li><a class="reference internal" href="#yaml-inventory">YAML Inventory</a></li>
</ul>
</li>
</ul>
</span>
</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>
<a href="http://github.com/ansible/ansible"><img style="position: absolute; right: 0; border: 0;" src="http://ansible.github.com/github.png" alt="Fork me on GitHub"></a>
2012-03-10 16:39:34 +01:00
<div class="container">
2012-04-19 15:24:45 +02:00
<a href="http://ansible.github.com"><img src="http://ansible.github.com/ansible-logo.png" alt="Ansible"/></a><br/>
2012-04-28 16:42:32 +02:00
<br/>
2012-05-13 17:00:02 +02:00
<div class="section" id="inventory-patterns">
<span id="patterns"></span><h1>Inventory &amp; Patterns<a class="headerlink" href="#inventory-patterns" title="Permalink to this headline"></a></h1>
2012-03-08 19:36:47 +01:00
<p>Ansible works against multiple systems in your infrastructure at the
same time. It does this by selecting portions of systems listed in
Ansible&#8217;s inventory file, which defaults to /etc/ansible/hosts.</p>
<div class="section" id="hosts-and-groups">
<span id="inventoryformat"></span><h2>Hosts and Groups<a class="headerlink" href="#hosts-and-groups" title="Permalink to this headline"></a></h2>
<p>The format for /etc/ansible/hosts is an INI format and looks like this:</p>
2012-03-08 19:36:47 +01:00
<div class="highlight-python"><div class="highlight"><pre><span class="n">mail</span><span class="o">.</span><span class="n">example</span><span class="o">.</span><span class="n">com</span>
<span class="p">[</span><span class="n">webservers</span><span class="p">]</span>
<span class="n">foo</span><span class="o">.</span><span class="n">example</span><span class="o">.</span><span class="n">com</span>
<span class="n">bar</span><span class="o">.</span><span class="n">example</span><span class="o">.</span><span class="n">com</span>
<span class="p">[</span><span class="n">dbservers</span><span class="p">]</span>
<span class="n">one</span><span class="o">.</span><span class="n">example</span><span class="o">.</span><span class="n">com</span>
<span class="n">two</span><span class="o">.</span><span class="n">example</span><span class="o">.</span><span class="n">com</span>
<span class="n">three</span><span class="o">.</span><span class="n">example</span><span class="o">.</span><span class="n">com</span>
</pre></div>
</div>
2012-05-02 06:55:54 +02:00
<p>The things in brackets are group names. You don&#8217;t have to have them,
but they are useful.</p>
2012-04-18 01:59:15 +02:00
<p>If you have hosts that run on non-standard SSH ports you can put the port number
after the hostname with a colon.</p>
<blockquote>
<div>four.example.com:5309</div></blockquote>
2012-08-15 04:14:47 +02:00
<p>In 0.6 and later, if you have a lot of hosts following similar patterns you can do this:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="p">[</span><span class="n">webservers</span><span class="p">]</span>
<span class="n">www</span><span class="p">[</span><span class="mo">01</span><span class="o">-</span><span class="mi">50</span><span class="p">]</span><span class="o">.</span><span class="n">example</span><span class="o">.</span><span class="n">com</span>
</pre></div>
</div>
<p>Leading zeros can be included or removed, as desired, and the ranges are inclusive.</p>
2012-03-08 19:36:47 +01:00
</div>
2012-03-08 19:53:48 +01:00
<div class="section" id="selecting-targets">
<h2>Selecting Targets<a class="headerlink" href="#selecting-targets" title="Permalink to this headline"></a></h2>
2012-08-17 03:12:36 +02:00
<p>We&#8217;ll go over how to use the command line in <a class="reference internal" href="examples.html"><em>Command Line Examples And Next Steps</em></a> section, however, basically it looks like this:</p>
<div class="highlight-python"><pre>ansible &lt;pattern_goes_here&gt; -m &lt;module_name&gt; -a &lt;arguments&gt;</pre>
</div>
<p>Such as:</p>
2012-03-31 16:40:01 +02:00
<div class="highlight-python"><pre>ansible webservers -m service -a "name=httpd state=restarted"</pre>
</div>
<p>Within <a class="reference internal" href="playbooks.html"><em>Playbooks</em></a>, these patterns can be used for even greater purposes.</p>
<p>Anyway, to use Ansible, you&#8217;ll first need to know how to tell Ansible which hosts in your inventory file to talk to.
This is done by designating particular host names or groups of hosts.</p>
<p>The following patterns target all hosts in the inventory file:</p>
2012-03-08 19:53:48 +01:00
<div class="highlight-python"><pre>all
*</pre>
2012-03-08 19:36:47 +01:00
</div>
<p>Basically &#8216;all&#8217; is an alias for &#8216;*&#8217;. It is also possible to address a specific host or hosts:</p>
2012-03-08 19:53:48 +01:00
<div class="highlight-python"><pre>one.example.com
one.example.com:two.example.com
192.168.1.50
192.168.1.*</pre>
2012-03-08 19:53:48 +01:00
</div>
<p>The following patterns address one or more groups, which are denoted
with the aforementioned bracket headers in the inventory file:</p>
2012-03-08 19:53:48 +01:00
<div class="highlight-python"><pre>webservers
webservers:dbservers</pre>
2012-03-08 19:36:47 +01:00
</div>
2012-07-20 17:51:03 +02:00
<p>You can exclude groups as well, for instance, all webservers not in Phoenix:</p>
<div class="highlight-python"><pre>webservers:!phoenix</pre>
</div>
<p>Individual host names (or IPs), but not groups, can also be referenced using
wildcards:</p>
<div class="highlight-python"><pre>*.example.com
*.com</pre>
</div>
2012-03-08 19:53:48 +01:00
<p>It&#8217;s also ok to mix wildcard patterns and groups at the same time:</p>
<div class="highlight-python"><pre>one*.com:dbservers</pre>
2012-03-08 19:36:47 +01:00
</div>
2012-08-17 03:12:36 +02:00
<p>Easy enough. See <a class="reference internal" href="examples.html"><em>Command Line Examples And Next Steps</em></a> and then <a class="reference internal" href="playbooks.html"><em>Playbooks</em></a> for how to do things to selected hosts.</p>
2012-04-18 01:54:23 +02:00
</div>
<div class="section" id="host-variables">
<h2>Host Variables<a class="headerlink" href="#host-variables" title="Permalink to this headline"></a></h2>
<p>It is easy to assign variables to hosts that will be used later in playbooks:</p>
<div class="highlight-python"><pre>[atlanta]
host1 http_port=80 maxRequestsPerChild=808
host2 http_port=303 maxRequestsPerChild=909</pre>
</div>
</div>
<div class="section" id="group-variables">
<h2>Group Variables<a class="headerlink" href="#group-variables" title="Permalink to this headline"></a></h2>
<p>Variables can also be applied to an entire group at once:</p>
<div class="highlight-python"><pre>[atlanta]
host1
host2
[atlanta:vars]
ntp_server=ntp.atlanta.example.com
proxy=proxy.atlanta.example.com</pre>
</div>
</div>
2012-05-10 07:41:31 +02:00
<div class="section" id="groups-of-groups-and-group-variables">
<h2>Groups of Groups, and Group Variables<a class="headerlink" href="#groups-of-groups-and-group-variables" title="Permalink to this headline"></a></h2>
<p>It is also possible to make groups of groups and assign
2012-05-10 07:41:31 +02:00
variables to groups. These variables can be used by /usr/bin/ansible-playbook, but not
2012-06-20 03:22:27 +02:00
/usr/bin/ansible:</p>
<div class="highlight-python"><pre>[atlanta]
host1
host2
2012-06-20 03:22:27 +02:00
[raleigh]
host2
host3
[southeast:children]
2012-05-06 23:16:42 +02:00
atlanta
2012-06-20 03:22:27 +02:00
raleigh
[southeast:vars]
some_server=foo.southeast.example.com
2012-05-10 07:41:31 +02:00
halon_system_timeout=30
self_destruct_countdown=60
2012-06-20 03:22:27 +02:00
escape_pods=2
[usa:children]
southeast
northeast
southwest
2012-06-20 03:22:27 +02:00
southeast</pre>
</div>
2012-07-20 17:51:03 +02:00
<p>If you need to store lists or hash data, or prefer to keep host and group specific variables
seperate from the inventory file, see the next section.</p>
</div>
2012-07-20 17:51:03 +02:00
<div class="section" id="splitting-out-host-and-group-specific-data">
<h2>Splitting Out Host and Group Specific Data<a class="headerlink" href="#splitting-out-host-and-group-specific-data" title="Permalink to this headline"></a></h2>
2012-08-10 04:09:05 +02:00
<p class="versionadded">
<span class="versionmodified">New in version 0.6.</span></p>
<p>In addition to the storing variables directly in the INI file, host
and group variables can be stored in individual files relative to the
inventory file. These variable files are in YAML format.</p>
2012-07-20 17:51:03 +02:00
<p>Assuming the inventory file path is:</p>
<div class="highlight-python"><pre>/etc/ansible/hosts</pre>
</div>
<p>If the host is named &#8216;foosball&#8217;, and in groups &#8216;raleigh&#8217; and &#8216;webservers&#8217;, variables
in YAML files at the following locations will be made available to the host:</p>
<div class="highlight-python"><pre>/etc/ansible/group_vars/raleigh
/etc/ansible/group_vars/webservers
/etc/ansible/host_vars/foosball</pre>
</div>
<p>For instance, suppose you have hosts grouped by datacenter, and each datacenter
uses some different servers. The data in the groupfile &#8216;/etc/ansible/group_vars/raleigh&#8217; for
the &#8216;raleigh&#8217; group might look like:</p>
2012-04-18 01:54:23 +02:00
<div class="highlight-python"><pre>---
2012-07-20 17:51:03 +02:00
ntp_server: acme.example.org
database_server: storage.example.org</pre>
2012-04-18 01:54:23 +02:00
</div>
2012-07-20 17:51:03 +02:00
<p>It is ok if these files do not exist, this is an optional feature.</p>
<p>Tip: Keeping your inventory file and variables in a git repo (or other version control)
is an excellent way to track changes to your inventory and host variables.</p>
2012-08-10 04:09:05 +02:00
<p class="versionadded">
<span class="versionmodified">New in version 0.5: </span>If you ever have two python interpreters on a system, set a
variable called &#8216;ansible_python_interpreter&#8217; to the Python
interpreter path you would like to use.</p>
2012-07-20 17:51:03 +02:00
</div>
<div class="section" id="yaml-inventory">
<h2>YAML Inventory<a class="headerlink" href="#yaml-inventory" title="Permalink to this headline"></a></h2>
2012-08-10 04:09:05 +02:00
<p class="deprecated">
<span class="versionmodified">Deprecated since version 0.7.</span></p>
<p>Ansible&#8217;s YAML inventory format is deprecated and will be removed in
Ansible 0.7. Ansible 0.6 includes a <a class="reference external" href="https://github.com/ansible/ansible/blob/devel/examples/scripts/yaml_to_ini.py">conversion script</a>.</p>
2012-07-20 17:51:03 +02:00
<p>Usage:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">yaml_to_ini</span><span class="o">.</span><span class="n">py</span> <span class="o">/</span><span class="n">etc</span><span class="o">/</span><span class="n">ansible</span><span class="o">/</span><span class="n">hosts</span>
</pre></div>
</div>
<div class="admonition-see-also admonition seealso">
<p class="first admonition-title">See also</p>
<dl class="last docutils">
2012-08-17 03:12:36 +02:00
<dt><a class="reference internal" href="examples.html"><em>Command Line Examples And Next Steps</em></a></dt>
<dd>Examples of basic commands</dd>
<dt><a class="reference internal" href="playbooks.html"><em>Playbooks</em></a></dt>
<dd>Learning ansible&#8217;s configuration management language</dd>
<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>
</dl>
</div>
2012-03-08 19:36:47 +01:00
</div>
2012-03-07 17:35:18 +01:00
</div>
2012-04-28 16:42:32 +02:00
<br/>
2012-03-07 17:35:18 +01:00
</div>
2012-04-28 16:53:15 +02:00
<footer class="footer">
<div class="container">
<div id="fb-root"></div>
2012-04-28 16:50:20 +02:00
<p>
2012-07-31 02:08:13 +02:00
<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_donations">
<input type="hidden" name="business" value="michael.dehaan@gmail.com">
<input type="hidden" name="lc" value="US">
<input type="hidden" name="item_name" value="Ansible">
<input type="hidden" name="no_note" value="0">
<input type="hidden" name="currency_code" value="USD">
<input type="hidden" name="bn" value="PP-DonationsBF:btn_donate_LG.gif:NonHostedGuest">
<input type="image" src="https://www.paypalobjects.com/en_US/i/btn/btn_donate_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">
<img alt="" border="0" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" width="1" height="1">
</form>
</p>
<p>
2012-04-28 16:42:32 +02:00
<a href="https://twitter.com/share" class="twitter-share-button" data-text="ansible.github.com">Share On Twitter</a>
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script>
<g:plusone annotation="inline"></g:plusone>
<div class="fb-like" data-href="http://ansible.github.com" data-send="true" data-width="450" data-show-faces="false"></div>
2012-04-28 16:50:20 +02:00
</p>
<p>
&copy; Copyright 2012 Michael DeHaan.<br/>
2012-08-25 01:33:14 +02:00
Last updated on Aug 24, 2012.<br/>
</p>
</div>
</footer>
2012-03-07 17:35:18 +01:00
</body>
</html>