<ahref="http://github.com/ansible/ansible"><imgstyle="position: absolute; right: 0; border: 0;"src="http://ansible.github.com/github.png"alt="Fork me on GitHub"></a>
<h1>Best Practices<aclass="headerlink"href="#best-practices"title="Permalink to this headline">¶</a></h1>
<p>Here are some tips for making the most of Ansible.</p>
<divclass="section"id="group-by-roles">
<h2>Group By Roles<aclass="headerlink"href="#group-by-roles"title="Permalink to this headline">¶</a></h2>
<p>A system can be in multiple groups. See ref:<cite>patterns</cite>. Having groups named after things like
‘webservers’ and ‘dbservers’ is repeated in the examples because it’s a very powerful concept.</p>
<p>This allows playbooks to target machines based on role, as well as to assign role specific variables
using the group variable system.</p>
</div>
<divclass="section"id="directory-organization">
<h2>Directory Organization<aclass="headerlink"href="#directory-organization"title="Permalink to this headline">¶</a></h2>
<p>Playbooks should be organized like this:</p>
<divclass="highlight-python"><pre>(root of source control repository)
acme/ # each playbook has a directory
setup.yml # playbook to manage the service
stop.yml # playbook to halt the service (optional)
files/
some_file_path_foo.conf
templates/
etc_acme_conf_acme.conf
etc_other_conf_other.conf
vars/
main.yml
handlers/
main.yml
tasks/
setup.yml
stop.yml</pre>
</div>
<p>Any directories or files not needed can be omitted. Not all modules may require <cite>vars</cite> or <cite>files</cite> sections, though most
will require <cite>handlers</cite>, <cite>tasks</cite>, and <cite>templates</cite>. To review what each of these sections do, see ref:<cite>playbooks</cite> and ref:<cite>playbooks2</cite>.</p>
<p>The acme/setup.yml playbook would be as simple as:</p>
<divclass="highlight-python"><pre>----
- hosts: webservers
user: root
vars_files
- include: vars/main.yml
tasks:
- include: tasks/setup.yml
handlers:
- include: handlers/main.yml</pre>
</div>
<p>The tasks are individually broken out in ‘acme/tasks/setup.yml’, and handlers, which are common to all task files,
are contained in ‘acme/handlers/main.yml’. As a reminder, handlers are mostly just used to notify services to restart
when things change, and these are described in ref:<cite>playbooks</cite>.</p>
<p>Including more than one setup file or more than one handlers file is of course legal.</p>
<p>Having playbooks be able to include other playbooks is coming in release 0.5.</p>
<p>Until then, to manage your entire site, simply execute all of your playbooks together, in the order desired.
You don’t have to do this though, it’s fine to select sections of your infrastructure to manage at a single time.
You may wish to construct simple shell scripts to wrap calls to ansible-playbook.</p>
</div>
<divclass="section"id="miscellaneous-tips">
<h2>Miscellaneous Tips<aclass="headerlink"href="#miscellaneous-tips"title="Permalink to this headline">¶</a></h2>
<p>When you can do something simply, do something simply. Do not reach to use every feature of Ansible together, all
at once. Use what works for you. For example, you should probably not need ‘vars’, ‘vars_files’, ‘vars_prompt’ and ‘–extra-vars’ all at once, while also using an external inventory file.</p>
<p>Optimize for readability. Whitespace between sections of YAML documents and in between tasks is strongly encouraged,
as is usage of YAML comments, which start with “#”. It is also useful to comment at the top of each file the purpose of the individual file and the author, including email address.</p>
<p>It is possible to leave off the “name” for a given task, though it is recommended to provide
a descriptive comment about why something is being done instead.</p>
<p>Use version control. Keep your playbooks and inventory file in git (or another version control system), and commit when you make changes to them.
This way you have an audit trail describing when and why you changed the rules automating your infrastructure.</p>
<p>Resist the urge to write the same playbooks and configuration files for heterogeneous distributions. While lots of software packages claim to make this easy on you, the configuration files are often quite different, to the point where it would be easier to treat them as different playbooks. This is why, for example, Ansible has a seperate ‘yum’ and ‘apt’ module. Yum and apt have different capabilities, and we don’t want to code for the least common denominator.</p>
<p>Use variables for user tunable settings versus having constants in the tasks file or templates, so that it is easy to reconfigure a playbook. Think about this as exposing the knobs to things you would like to tweak.</p>
<p>Since a system can be in more than one group, if you have multiple datacenters or sites, consider putting systems into groups by role, but also different groups by geography. This allows you to assign different variables to different geographies.</p>
<ahref="http://github.com/ansible/ansible"><imgstyle="position: absolute; right: 0; border: 0;"src="http://ansible.github.com/github.png"alt="Fork me on GitHub"></a>
<h2>Accessing Information About Other Hosts<aclass="headerlink"href="#accessing-information-about-other-hosts"title="Permalink to this headline">¶</a></h2>
<p>If your database server wants to check the value of a ‘fact’ from another node, or an inventory variable
assigned to another node, it’s easy to do so within a template or even an action line (note: this uses syntax available in 0.4 and later):</p>
<p>NOTE: No database or other complex system is required to exchange data between hosts. The hosts that you
want to reference data from must be included in either the current play or any previous play.</p>
</div>
<divclass="section"id="magic-variables">
<h2>Magic Variables<aclass="headerlink"href="#magic-variables"title="Permalink to this headline">¶</a></h2>
<p>Some variables made available to hosts don’t come from definitions in a playbook, the inventory file, or discovery from the system. There are only two of these, and are used in special cases that many users won’t need.</p>
<p><cite>groups</cite> is a list (array) of all the groups the current host is in. This can be used in templates using Jinja2
syntax to make template source files that vary based on the group membership (or role) of the host:</p>
<divclass="highlight-python"><pre>{% if 'webserver' in groups %}
# some part of a configuration file that only applies to webservers
{% endif %}</pre>
</div>
<p><cite>inventory_hostname</cite> is the name of the hostname as configured in Ansible’s inventory host file. This can
be useful for when you don’t want to rely on the discovered hostname <cite>ansible_hostname</cite> or for other mysterious
reasons. Don’t worry about it unless you think you need it.</p>
</div>
<divclass="section"id="variable-file-seperation">
<h2>Variable File Seperation<aclass="headerlink"href="#variable-file-seperation"title="Permalink to this headline">¶</a></h2>
<p>It’s a great idea to keep your playbooks under source control, but
you may wish to make the playbook source public while keeping certain
important variables private. Similarly, sometimes you may just
want to keep certain information in different files, away from
the main playbook.</p>
<p>You can do this by using an external variables file, or files, just like this:</p>
<divclass="highlight-python"><pre>---
- hosts: all
user: root
vars:
favcolor: blue
vars_files:
- /vars/external_vars.yml
tasks:
- name: this is just a placeholder
action: command /bin/echo foo</pre>
</div>
<p>This removes the risk of sharing sensitive data with others when
sharing your playbook source with them.</p>
<p>The contents of each variables file is a simple YAML dictionary, like this:</p>
<divclass="highlight-python"><pre>---
# in the above example, this would be vars/external_vars.yml
<h2>Passing Variables On The Command Line<aclass="headerlink"href="#passing-variables-on-the-command-line"title="Permalink to this headline">¶</a></h2>
<p>In addition to <cite>vars_prompt</cite> and <cite>vars_files</cite>, it is possible to send variables over
the ansible command line. This is particularly useful when writing a generic release playbook
where you may want to pass in the version of the application to deploy:</p>
<h2>Selecting Files And Templates Based On Variables<aclass="headerlink"href="#selecting-files-and-templates-based-on-variables"title="Permalink to this headline">¶</a></h2>
<p>Sometimes a configuration file you want to copy, or a template you will use may depend on a variable.
The following construct (new in 0.4) selects the first available file appropriate for the variables of a given host,
which is often much cleaner than putting a lot of if conditionals in a template.</p>
<p>The following example shows how to template out a configuration file that was very different between, say,
CentOS and Debian:</p>
<divclass="highlight-python"><pre>- name: template a file