<p>Here are some tips for making the most of Ansible.</p>
<p>Here are some tips for making the most of Ansible.</p>
<divclass="section"id="always-mention-state">
<divclass="section"id="always-mention-state">
<h2>Always Mention State<aclass="headerlink"href="#always-mention-state"title="Permalink to this headline">¶</a></h2>
<h2>Always Mention State<aclass="headerlink"href="#always-mention-state"title="Permalink to this headline">¶</a></h2>
<p>The ‘state’ parameter is optional to a lot of modules. Whether state=present or state=absent, it’s always
<p>The ‘state’ parameter is optional to a lot of modules. Whether
best to leave that parameter in your playbooks to make it clear, especially as some modules support additional
‘state=present’ or ‘state=absent’, it’s always best to leave that
states.</p>
parameter in your playbooks to make it clear, especially as some
modules support additional states.</p>
</div>
</div>
<divclass="section"id="group-by-roles">
<divclass="section"id="group-by-roles">
<h2>Group By Roles<aclass="headerlink"href="#group-by-roles"title="Permalink to this headline">¶</a></h2>
<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 <aclass="reference internal"href="patterns.html"><em>Inventory & Patterns</em></a>. Having groups named after things like
<p>A system can be in multiple groups. See <aclass="reference internal"href="patterns.html"><em>Inventory & Patterns</em></a>. Having groups named after things like
‘webservers’ and ‘dbservers’ is repeated in the examples because it’s a very powerful concept.</p>
<em>webservers</em> and <em>dbservers</em> 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
<p>This allows playbooks to target machines based on role, as well as to assign role specific variables
using the group variable system.</p>
using the group variable system.</p>
</div>
</div>
<divclass="section"id="directory-organization">
<divclass="section"id="directory-organization">
<h2>Directory Organization<aclass="headerlink"href="#directory-organization"title="Permalink to this headline">¶</a></h2>
<h2>Directory Organization<aclass="headerlink"href="#directory-organization"title="Permalink to this headline">¶</a></h2>
<p>Playbooks should be organized like this:</p>
<p>Playbooks should be organized like this:</p>
<divclass="highlight-python"><pre>(root of source control repository)
<divclass="highlight-python"><pre># root of source control repository
├── acme/
global_vars.yml # variables for all playbooks
│ ├── setup.yml
acme/ # each playbook has a directory
│ └── stop.yml
├── files/
setup.yml # playbook to manage the service
│ └── some_file_path_foo.conf
stop.yml # playbook to halt the service (optional)
├── handlers/
│ └── main.yml
files/
├── tasks/
some_file_path_foo.conf
│ ├── setup.yml
│ └── stop.yml
templates/
├── templates/
etc_acme_conf_acme.conf
│ ├── etc_acme_conf_acme.conf
etc_other_conf_other.conf
│ └── etc_other_conf_other.conf
├── vars/
vars/
│ └── main.yml
main.yml # variables specific to this playbook
└── global_vars.yml</pre>
handlers/
main.yml
tasks/
setup.yml
stop.yml</pre>
</div>
</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
<p>Any directories or files not needed can be omitted. Not all modules
will require <cite>handlers</cite>, <cite>tasks</cite>, and <cite>templates</cite>. To review what each of these sections do, see <aclass="reference internal"href="playbooks.html"><em>Playbooks</em></a> and <aclass="reference internal"href="playbooks2.html"><em>Advanced Playbooks</em></a>.</p>
may require ‘vars’ or ‘files’ sections, though most will require
‘handlers’, ‘tasks’, and ‘templates’. To review what each of
these sections do, see <aclass="reference internal"href="playbooks.html"><em>Playbooks</em></a> and <aclass="reference internal"href="playbooks2.html"><em>Advanced Playbooks</em></a>.</p>
<p>The acme/setup.yml playbook would be as simple as:</p>
<p>The acme/setup.yml playbook would be as simple as:</p>
<divclass="highlight-python"><pre>----
<divclass="highlight-python"><pre>---
- hosts: webservers
- hosts: webservers
user: root
user: root
...
@@ -246,7 +241,8 @@ will require <cite>handlers</cite>, <cite>tasks</cite>, and <cite>templates</cit
...
@@ -246,7 +241,8 @@ will require <cite>handlers</cite>, <cite>tasks</cite>, and <cite>templates</cit
are contained in ‘acme/handlers/main.yml’. As a reminder, handlers are mostly just used to notify services to restart
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 <aclass="reference internal"href="playbooks.html"><em>Playbooks</em></a>.</p>
when things change, and these are described in <aclass="reference internal"href="playbooks.html"><em>Playbooks</em></a>.</p>
<p>Including more than one setup file or more than one handlers file is of course legal.</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 a future release. See Issue 538.</p>
<p>Having playbooks be able to include other playbooks is coming in a
future release. See <aclass="reference external"href="https://github.com/ansible/ansible/issues/538">Issue 538</a>.</p>
<p>Until then, to manage your entire site, simply execute all of your playbooks together, in the order desired.
<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 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>
You may wish to construct simple shell scripts to wrap calls to ansible-playbook.</p>
...
@@ -262,17 +258,39 @@ keep modules that go with a playbook together.</p>
...
@@ -262,17 +258,39 @@ keep modules that go with a playbook together.</p>
</div>
</div>
<divclass="section"id="miscellaneous-tips">
<divclass="section"id="miscellaneous-tips">
<h2>Miscellaneous Tips<aclass="headerlink"href="#miscellaneous-tips"title="Permalink to this headline">¶</a></h2>
<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
<p>When you can do something simply, do something simply. Do not reach
at once. Use what works for you. For example, you should probably not need <ttclass="docutils literal"><spanclass="pre">vars</span></tt>, <ttclass="docutils literal"><spanclass="pre">vars_files</span></tt>, <ttclass="docutils literal"><spanclass="pre">vars_prompt</span></tt> and <ttclass="docutils literal"><spanclass="pre">--extra-vars</span></tt> all at once, while also using an external inventory file.</p>
to use every feature of Ansible together, all at once. Use what works
<p>Optimize for readability. Whitespace between sections of YAML documents and in between tasks is strongly encouraged,
for you. For example, you should probably not need ‘vars’,
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>
‘vars_files’, ‘vars_prompt’ and ‘–extra-vars’ all at once,
<p>It is possible to leave off the “name” for a given task, though it is recommended to provide
while also using an external inventory file.</p>
a descriptive comment about why something is being done instead.</p>
<p>Optimize for readability. Whitespace between sections of YAML
<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.
documents and in between tasks is strongly encouraged, as is usage of
This way you have an audit trail describing when and why you changed the rules automating your infrastructure.</p>
YAML comments, which start with ‘#’. It is also useful to comment
<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>
at the top of each file the purpose of the individual file and the
<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>
author, including email address.</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>
<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
separate <aclass="reference internal"href="modules.html#yum"><em>yum</em></a> and <aclass="reference internal"href="modules.html#apt"><em>apt</em></a> 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
@@ -267,12 +267,19 @@ documentation. The <cite>user</cite> is just the name of the user account:</p>
...
@@ -267,12 +267,19 @@ documentation. The <cite>user</cite> is just the name of the user account:</p>
<p>If you need to specify a password to sudo, run <cite>ansible-playbook</cite> with <ttclass="docutils literal"><spanclass="pre">--ask-sudo-pass</span></tt> (<cite>-K</cite>).
<p>If you need to specify a password to sudo, run <cite>ansible-playbook</cite> with <ttclass="docutils literal"><spanclass="pre">--ask-sudo-pass</span></tt> (<cite>-K</cite>).
If you run a sudo playbook and the playbook seems to hang, it’s probably stuck at the sudo prompt.
If you run a sudo playbook and the playbook seems to hang, it’s probably stuck at the sudo prompt.
Just <cite>Control-C</cite> to kill it and run it again with <cite>-K</cite>.</p>
Just <cite>Control-C</cite> to kill it and run it again with <cite>-K</cite>.</p>
<p>NOTE: When using <cite>sudo_user</cite> to a user other than root, the module arguments are briefly written into
<divclass="admonition important">
a random tempfile in /tmp. These are deleted immediately after the command is executed. This only
<pclass="first admonition-title">Important</p>
occurs when sudoing from a user like ‘bob’ to ‘timmy’, not when going from ‘bob’ to ‘root’, or
<pclass="last">When using <cite>sudo_user</cite> to a user other than root, the module
logging in directly as ‘bob’ or ‘root’. If this concerns you that this data is briefly readable
arguments are briefly written into a random tempfile in /tmp.
(not writeable), avoid transferring uncrypted passwords with <cite>sudo_user</cite> set. In other cases, ‘/tmp’ is not used and
These are deleted immediately after the command is executed. This
this does not come into play. Ansible also takes care to not log password parameters.</p>
only occurs when sudoing from a user like ‘bob’ to ‘timmy’, not
when going from ‘bob’ to ‘root’, or logging in directly as ‘bob’ or
‘root’. If this concerns you that this data is briefly readable
(not writeable), avoid transferring uncrypted passwords with
<cite>sudo_user</cite> set. In other cases, ‘/tmp’ is not used and this does
not come into play. Ansible also takes care to not log password
parameters.</p>
</div>
</div>
</div>
<divclass="section"id="vars-section">
<divclass="section"id="vars-section">
<h3>Vars section<aclass="headerlink"href="#vars-section"title="Permalink to this headline">¶</a></h3>
<h3>Vars section<aclass="headerlink"href="#vars-section"title="Permalink to this headline">¶</a></h3>
...
@@ -459,12 +466,16 @@ of a play:</p>
...
@@ -459,12 +466,16 @@ of a play:</p>
- include: handlers/handlers.yml</pre>
- include: handlers/handlers.yml</pre>
</div>
</div>
<p>You can mix in includes along with your regular non-included tasks and handlers.</p>
<p>You can mix in includes along with your regular non-included tasks and handlers.</p>
<p>NOTE:: you can not conditionally path the location to an include file, like you can
<divclass="admonition note">
with ‘vars_files’. If you find yourself needing to do this, consider how you can
<pclass="first admonition-title">Note</p>
restructure your playbook to be more class/role oriented. This is to say you cannot
<pclass="last">You can not conditionally path the location to an include file,
use a ‘fact’ to decide what include file to use. All hosts contained within the play
like you can with ‘vars_files’. If you find yourself needing to do
are going to get the same tasks. (‘only_if’ provides some ability for hosts to conditionally
this, consider how you can restructure your playbook to be more
skip tasks).</p>
class/role oriented. This is to say you cannot use a ‘fact’ to
decide what include file to use. All hosts contained within the
play are going to get the same tasks. (‘only_if’ provides some
ability for hosts to conditionally skip tasks).</p>
</div>
</div>
</div>
<divclass="section"id="executing-a-playbook">
<divclass="section"id="executing-a-playbook">
<h2>Executing A Playbook<aclass="headerlink"href="#executing-a-playbook"title="Permalink to this headline">¶</a></h2>
<h2>Executing A Playbook<aclass="headerlink"href="#executing-a-playbook"title="Permalink to this headline">¶</a></h2>
<p>NOTE: No database or other complex system is required to exchange data between hosts. The hosts that you
<divclass="admonition note">
want to reference data from must be included in either the current play or any previous play.</p>
<pclass="first admonition-title">Note</p>
<pclass="last">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>
<p>Additionally, <em>group_names</em> 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>
<p>Additionally, <em>group_names</em> 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 group_names %}
<divclass="highlight-python"><pre>{% if 'webserver' in group_names %}
# some part of a configuration file that only applies to webservers
# some part of a configuration file that only applies to webservers
...
@@ -321,7 +325,11 @@ sharing your playbook source with them.</p>
...
@@ -321,7 +325,11 @@ sharing your playbook source with them.</p>
somevar: somevalue
somevar: somevalue
password: magic</pre>
password: magic</pre>
</div>
</div>
<p>NOTE: It’s also possible to keep per-host and per-group variables in very similar files, this is covered in <aclass="reference internal"href="patterns.html#patterns"><em>Inventory & Patterns</em></a>.</p>
<divclass="admonition note">
<pclass="first admonition-title">Note</p>
<pclass="last">It’s also possible to keep per-host and per-group variables in very
similar files, this is covered in <aclass="reference internal"href="patterns.html#patterns"><em>Inventory & Patterns</em></a>.</p>