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-13 00:13:19 +01:00
< title > Playbooks — Ansible - SSH-Based Configuration Management & 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" / >
2012-03-09 17:44:14 +01:00
< 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: '',
2012-03-13 00:13:19 +01:00
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 >
2012-03-09 17:44:14 +01:00
< 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" / >
2012-03-13 00:13:19 +01:00
< link rel = "top" title = "Ansible - SSH-Based Configuration Management & Deployment" href = "index.html" / >
2012-05-13 17:00:02 +02:00
< link rel = "next" title = "Advanced Playbooks" href = "playbooks2.html" / >
2012-03-11 20:34:21 +01:00
< link rel = "prev" title = "YAML Syntax" href = "YAMLSyntax.html" / >
2012-03-09 17:44:14 +01:00
< 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();
});
}());
2012-03-10 04:31:54 +01:00
< / script >
2012-04-28 16:46:09 +02:00
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);
})();
2012-03-09 17:44:14 +01:00
< / script >
2012-04-28 16:46:09 +02:00
< 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 > ( f u n c t i o n ( d , s , i d ) {
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-09 17:44:14 +01:00
2012-03-07 17:35:18 +01:00
< / head >
< body >
2012-03-09 17:44:14 +01:00
< 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> -->
2012-03-09 17:44:14 +01:00
< ul class = "nav" >
< li class = "dropdown" data-dropdown = "dropdown" >
< a href = "index.html"
2012-03-31 15:29:31 +02:00
class="dropdown-toggle">Chapter< / a >
2012-03-09 17:44:14 +01:00
< 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" > < a class = "reference internal" href = "patterns.html" > Inventory & 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 >
2012-03-09 17:44:14 +01:00
< li class = "toctree-l1" > < a class = "reference internal" href = "modules.html" > Ansible Modules< / a > < / li >
2012-03-11 20:34:21 +01:00
< li class = "toctree-l1" > < a class = "reference internal" href = "YAMLSyntax.html" > YAML Syntax< / a > < / li >
2012-03-09 17:44:14 +01:00
< li class = "toctree-l1 current" > < a class = "current reference internal" href = "" > 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 >
2012-05-13 17:56:09 +02:00
< li class = "toctree-l1" > < a class = "reference internal" href = "bestpractices.html" > Best Practices< / a > < / li >
2012-03-31 04:28:30 +02:00
< li class = "toctree-l1" > < a class = "reference internal" href = "api.html" > API & 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 >
2012-05-13 17:56:09 +02:00
< 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 >
2012-03-09 17:44:14 +01:00
< / ul >
< / span >
2012-03-31 15:36:37 +02:00
< / li >
< li class = "dropdown" data-dropdown = "dropdown" >
< a href = "#"
class="dropdown-toggle">Page< / a >
< span class = "localtoc" > < ul >
< li > < a class = "reference internal" href = "#" > Playbooks< / a > < ul >
2012-08-01 04:19:04 +02:00
< li > < a class = "reference internal" href = "#playbook-language-example" > Playbook Language Example< / a > < / li >
2012-03-31 15:36:37 +02:00
< li > < a class = "reference internal" href = "#basics" > Basics< / a > < ul >
2012-03-21 01:15:53 +01:00
< li > < a class = "reference internal" href = "#hosts-and-users" > Hosts and Users< / a > < / li >
2012-03-31 15:36:37 +02:00
< li > < a class = "reference internal" href = "#vars-section" > Vars section< / a > < / li >
< li > < a class = "reference internal" href = "#tasks-list" > Tasks list< / a > < / li >
< / ul >
< / li >
2012-03-21 01:15:53 +01:00
< li > < a class = "reference internal" href = "#running-operations-on-change" > Running Operations On Change< / a > < / li >
2012-08-01 04:19:04 +02:00
< li > < a class = "reference internal" href = "#include-files-and-encouraging-reuse" > Include Files And Encouraging Reuse< / a > < / li >
2012-03-31 15:36:37 +02:00
< li > < a class = "reference internal" href = "#executing-a-playbook" > Executing A Playbook< / a > < / li >
2012-07-04 23:47:04 +02:00
< li > < a class = "reference internal" href = "#tips-and-tricks" > Tips and Tricks< / a > < / li >
2012-03-31 15:36:37 +02:00
< / ul >
< / li >
< / ul >
< / span >
2012-03-09 17:44:14 +01:00
< / 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 >
2012-04-28 17:20:38 +02:00
< 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-09 17:44:14 +01:00
2012-03-10 16:39:34 +01:00
2012-03-09 17:44:14 +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-03-09 17:44:14 +01:00
< div class = "section" id = "playbooks" >
< h1 > Playbooks< a class = "headerlink" href = "#playbooks" title = "Permalink to this headline" > ¶< / a > < / h1 >
2012-08-01 04:19:04 +02:00
< p > Playbooks are a completely different way to use ansible than in task execution mode, and are
2012-08-17 03:12:36 +02:00
particularly powerful. Simply put, playbooks are the basis for a really simple
2012-03-31 16:17:35 +02:00
configuration management and multi-machine deployment system,
2012-08-01 04:19:04 +02:00
unlike any that already exist, and one that is very well suited to deploying complex applications.< / p >
2012-03-31 16:17:35 +02:00
< p > Playbooks can declare configurations, but they can also orchestrate steps of
any manual ordered process, even as different steps must bounce back and forth
between sets of machines in particular orders. They can launch tasks
synchronously or asynchronously.< / p >
2012-03-09 17:50:07 +01:00
< p > While you might run the main /usr/bin/ansible program for ad-hoc
2012-03-09 17:44:14 +01:00
tasks, playbooks are more likely to be kept in source control and used
to push out your configuration or assure the configurations of your
remote systems are in spec.< / p >
2012-03-31 16:17:35 +02:00
< p > Let’ s dive in and see how they work. As you go, you may wish to open
2012-08-07 04:00:50 +02:00
the < a class = "reference external" href = "https://github.com/ansible/ansible/tree/devel/examples/playbooks" > github examples directory< / a > in
2012-03-31 16:17:35 +02:00
another tab, so you can apply the theory to what things look like in practice.< / p >
2012-08-01 04:19:04 +02:00
< div class = "section" id = "playbook-language-example" >
< h2 > Playbook Language Example< a class = "headerlink" href = "#playbook-language-example" title = "Permalink to this headline" > ¶< / a > < / h2 >
2012-03-09 17:44:14 +01:00
< p > Playbooks are expressed in YAML format and have a minimum of syntax.
2012-03-13 04:12:21 +01:00
Each playbook is composed of one or more ‘ plays’ in a list.< / p >
2012-08-01 04:19:04 +02:00
< p > The goal of a play is map a group of hosts to some well defined roles, represented by
things ansible called tasks. At the basic level, a task is nothing more than a call
to an ansible module, which you should have learned about in earlier chapters.< / p >
2012-03-13 04:12:21 +01:00
< p > By composing a playbook of multiple ‘ plays’ , it is possible to
2012-03-09 17:44:14 +01:00
orchestrate multi-machine deployments, running certain steps on all
machines in the webservers group, then certain steps on the database
2012-03-13 04:12:21 +01:00
server group, then more commands back on the webservers group, etc.< / p >
2012-04-13 04:47:15 +02:00
< p > For starters, here’ s a playbook that contains just one play:< / p >
2012-03-08 19:36:47 +01:00
< div class = "highlight-python" > < pre > ---
2012-03-08 19:53:48 +01:00
- hosts: webservers
vars:
http_port: 80
max_clients: 200
user: root
tasks:
2012-03-13 04:12:21 +01:00
- name: ensure apache is at the latest version
action: yum pkg=httpd state=latest
2012-03-08 19:53:48 +01:00
- name: write the apache config file
action: template src=/srv/httpd.j2 dest=/etc/httpd.conf
notify:
- restart apache
- name: ensure apache is running
action: service name=httpd state=started
handlers:
2012-03-14 01:32:55 +01:00
- name: restart apache
action: service name=apache state=restarted< / pre >
2012-03-08 19:36:47 +01:00
< / div >
2012-03-13 04:12:21 +01:00
< p > Below, we’ ll break down what the various features of the playbook language are.< / p >
2012-03-08 19:36:47 +01:00
< / div >
2012-03-31 15:36:37 +02:00
< div class = "section" id = "basics" >
< h2 > Basics< a class = "headerlink" href = "#basics" title = "Permalink to this headline" > ¶< / a > < / h2 >
2012-03-21 01:15:53 +01:00
< div class = "section" id = "hosts-and-users" >
< h3 > Hosts and Users< a class = "headerlink" href = "#hosts-and-users" title = "Permalink to this headline" > ¶< / a > < / h3 >
< p > For each play in a playbook, you get to choose which machines in your infrastructure
to target and what remote user to complete the steps (called tasks) as.< / p >
2012-03-13 04:12:21 +01:00
< p > The < cite > hosts< / cite > line is a list of one or more groups or host patterns,
2012-05-13 17:00:02 +02:00
separated by colons, as described in the < a class = "reference internal" href = "patterns.html#patterns" > < em > Inventory & Patterns< / em > < / a >
2012-03-21 01:15:53 +01:00
documentation. The < cite > user< / cite > is just the name of the user account:< / p >
< div class = "highlight-python" > < pre > ---
- hosts: webservers
user: root< / pre >
2012-03-13 04:12:21 +01:00
< / div >
2012-03-31 18:08:28 +02:00
< p > Support for running things from sudo is also available:< / p >
< div class = "highlight-python" > < pre > ---
2012-07-28 14:42:20 +02:00
- hosts: webservers
user: yourname
sudo: True< / pre >
< / div >
< p > You can also login as you, and then sudo to different users than root:< / p >
< div class = "highlight-python" > < pre > ---
2012-03-31 18:08:28 +02:00
- hosts: webservers
user: yourname
2012-07-26 05:34:02 +02:00
sudo: True
2012-07-28 14:42:20 +02:00
sudo_user: postgres< / pre >
2012-03-31 18:08:28 +02:00
< / div >
2012-05-02 06:55:54 +02:00
< p > If you need to specify a password to sudo, run < cite > ansible-playbook< / cite > with < tt class = "docutils literal" > < span class = "pre" > --ask-sudo-pass< / span > < / tt > (< cite > -K< / cite > ).
2012-04-14 14:45:27 +02:00
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 >
2012-08-10 04:12:10 +02:00
< div class = "admonition important" >
< p class = "first admonition-title" > Important< / p >
< p class = "last" > When using < cite > sudo_user< / cite > to a user other than root, the module
arguments are briefly written into a random tempfile in /tmp.
These are deleted immediately after the command is executed. This
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 >
2012-03-08 19:36:47 +01:00
< / div >
< div class = "section" id = "vars-section" >
2012-03-31 15:36:37 +02:00
< h3 > Vars section< a class = "headerlink" href = "#vars-section" title = "Permalink to this headline" > ¶< / a > < / h3 >
2012-03-21 02:04:47 +01:00
< p > The < cite > vars< / cite > section contains a list of variables and values that can be used in the plays, like this:< / p >
2012-03-21 01:15:53 +01:00
< div class = "highlight-python" > < pre > ---
- hosts: webservers
users: root
vars:
http_port: 80
van_halen_port: 5150
other: 'magic'< / pre >
< / div >
2012-05-13 17:00:02 +02:00
< p > These variables can be used later in the playbook like this:< / p >
2012-06-20 03:22:27 +02:00
< div class = "highlight-python" > < pre > $varname or ${varname}< / pre >
2012-03-21 01:15:53 +01:00
< / div >
2012-08-01 04:19:04 +02:00
< p > The later is useful in the event you need to do something like ${other}_some_string.< / p >
2012-08-17 03:12:36 +02:00
< p > Inside templates, the full power of the < a class = "reference external" href = "http://jinja.pocoo.org/docs/" > Jinja2< / a > templating language is also available, which looks like this:< / p >
2012-05-19 23:30:30 +02:00
< div class = "highlight-python" > < pre > {{ varname }}< / pre >
< / div >
2012-05-13 17:00:02 +02:00
< p > The Jinja2 documentation provides information about how to construct loops and conditionals for those
who which to use more advanced templating. This is optional and the $varname format still works in template
files.< / p >
2012-08-01 04:19:04 +02:00
< p > If there are discovered variables about the system, called ‘ facts’ , these variables bubble up back into the
playbook, and can be used on each system just like explicitly set variables. Ansible provides several
of these, prefixed with ‘ ansible’ , and are documented under < a class = "reference internal" href = "modules.html#setup" > < em > setup< / em > < / a > in the module documentation. Additionally,
facts can be gathered by ohai and facter if they are installed. Facter variables are prefixed with < tt class = "docutils literal" > < span class = "pre" > facter_< / span > < / tt > and Ohai
variables are prefixed with < tt class = "docutils literal" > < span class = "pre" > ohai_< / span > < / tt > .< / p >
2012-07-26 05:34:02 +02:00
< p > So for instance, if I wanted
2012-03-09 17:44:14 +01:00
to write the hostname into the /etc/motd file, I could say:< / p >
2012-03-09 05:06:07 +01:00
< div class = "highlight-python" > < pre > - name: write the motd
2012-03-13 04:12:21 +01:00
action: template src=/srv/templates/motd.j2 dest=/etc/motd< / pre >
2012-03-09 05:06:07 +01:00
< / div >
< p > And in /srv/templates/motd.j2:< / p >
2012-03-09 04:50:12 +01:00
< div class = "highlight-python" > < pre > You are logged into {{ facter_hostname }}< / pre >
< / div >
< p > But we’ re getting ahead of ourselves. Let’ s talk about tasks.< / p >
2012-03-08 19:36:47 +01:00
< / div >
< div class = "section" id = "tasks-list" >
2012-03-31 15:36:37 +02:00
< h3 > Tasks list< a class = "headerlink" href = "#tasks-list" title = "Permalink to this headline" > ¶< / a > < / h3 >
2012-03-09 17:44:14 +01:00
< p > Each play contains a list of tasks. Tasks are executed in order, one
2012-03-13 04:12:21 +01:00
at a time, against all machines matched by the host pattern,
2012-08-01 04:19:04 +02:00
before moving on to the next task. It is important to understand that, within a play,
all hosts are going to get the same task directives. It is the purpose of a play to map
a selection of hosts to tasks.< / p >
< p > When running the playbook, which runs top to bottom, hosts with failed tasks are
taken out of the rotation for the entire playbook. If things fail, simply correct the playbook file and rerun.< / p >
2012-03-21 01:15:53 +01:00
< p > The goal of each task is to execute a module, with very specific arguments.
Variables, as mentioned above, can be used in arguments to modules.< / p >
2012-08-01 04:19:04 +02:00
< p > Modules are ‘ idempotent’ , meaning if you run them
2012-03-09 17:44:14 +01:00
again, they will make the changes they are told to make to bring the
2012-03-13 04:12:21 +01:00
system to the desired state. This makes it very safe to rerun
the same playbook multiple times. They won’ t change things
2012-03-21 01:15:53 +01:00
unless they have to change things.< / p >
2012-08-01 04:19:04 +02:00
< p > The < cite > command< / cite > and < cite > shell< / cite > modules will typically rerun the same command again,
2012-03-21 01:15:53 +01:00
which is totally ok if the command is something like
2012-08-01 04:19:04 +02:00
‘ chmod’ or ‘ setsebool’ , etc. Though there is a ‘ creates’ flag available which can
be used to make these modules also idempotent.< / p >
2012-07-28 16:24:56 +02:00
< p > Every task should have a < cite > name< / cite > , which is included in the output from
2012-03-21 01:15:53 +01:00
running the playbook. This is output for humans, so it is
2012-07-28 16:24:56 +02:00
nice to have reasonably good descriptions of each task step. If the name
is not provided though, the string fed to ‘ action’ will be used for
output.< / p >
2012-03-21 01:15:53 +01:00
< p > Here is what a basic task looks like, as with most modules,
the service module takes key=value arguments:< / p >
< div class = "highlight-python" > < pre > tasks:
- name: make sure apache is running
action: service name=httpd state=running< / pre >
2012-03-13 04:12:21 +01:00
< / div >
2012-05-02 06:55:54 +02:00
< p > The < cite > command< / cite > and < cite > shell< / cite > modules are the one modules that just takes a list
of arguments, and don’ t use the key=value form. This makes
them work just like you would expect. Simple:< / p >
2012-03-21 01:15:53 +01:00
< div class = "highlight-python" > < pre > tasks:
- name: disable selinux
action: command /sbin/setenforce 0< / pre >
2012-03-13 04:12:21 +01:00
< / div >
2012-08-01 04:19:04 +02:00
< p > The command and shell module care about return codes, so if you have a command
who’ s successful exit code is not zero, you may wish to do this:< / p >
< blockquote >
< div > < dl class = "docutils" >
< dt > tasks:< / dt >
< dd > < ul class = "first last simple" >
< li > name: run this command and ignore the result
action: shell /usr/bin/somecommand & /bin/true< / li >
< / ul >
< / dd >
< / dl >
< / div > < / blockquote >
2012-03-13 04:12:21 +01:00
< p > Variables can be used in action lines. Suppose you defined
a variable called ‘ vhost’ in the ‘ vars’ section, you could do this:< / p >
2012-03-21 01:15:53 +01:00
< div class = "highlight-python" > < pre > tasks:
2012-05-02 06:55:54 +02:00
- name: create a virtual host file for $vhost
2012-03-21 01:15:53 +01:00
action: template src=somefile.j2 dest=/etc/httpd/conf.d/$vhost< / pre >
2012-03-09 05:06:07 +01:00
< / div >
2012-03-13 04:12:21 +01:00
< p > Those same variables are usable in templates, which we’ ll get to later.< / p >
2012-08-01 04:19:04 +02:00
< p > Now in a very basic playbook all the tasks will be listed directly in that play, though it will usually
make more sense to break up tasks using the ‘ include:’ directive. We’ ll show that a bit later.< / p >
2012-03-08 19:36:47 +01:00
< / div >
2012-03-31 15:36:37 +02:00
< / div >
2012-03-21 01:15:53 +01:00
< div class = "section" id = "running-operations-on-change" >
< h2 > Running Operations On Change< a class = "headerlink" href = "#running-operations-on-change" title = "Permalink to this headline" > ¶< / a > < / h2 >
2012-08-01 04:19:04 +02:00
< p > As we’ ve mentioned, modules are written to be ‘ idempotent’ and can relay when
2012-05-02 06:55:54 +02:00
they have made a change on the remote system. Playbooks recognize this and
2012-03-13 04:12:21 +01:00
have a basic event system that can be used to respond to change.< / p >
< p > These ‘ notify’ actions are triggered at the end of each ‘ play’ in a playbook, and
trigger only once each. For instance, multiple resources may indicate
that apache needs to be restarted, but apache will only be bounced once.< / p >
< p > Here’ s an example of restarting two services when the contents of a file
change, but only if the file changes:< / p >
< div class = "highlight-python" > < pre > - name: template configuration file
action: template src=template.j2 dest=/etc/foo.conf
notify:
- restart memcached
2012-03-14 01:32:55 +01:00
- restart apache< / pre >
2012-03-13 04:12:21 +01:00
< / div >
2012-03-21 01:15:53 +01:00
< p > The things listed in the ‘ notify’ section of a task are called
handlers.< / p >
2012-03-09 17:44:14 +01:00
< p > Handlers are lists of tasks, not really any different from regular
tasks, that are referenced by name. Handlers are what notifiers
notify. If nothing notifies a handler, it will not run. Regardless
of how many things notify a handler, it will run only once, after all
of the tasks complete in a particular play.< / p >
2012-03-13 04:12:21 +01:00
< p > Here’ s an example handlers section:< / p >
< div class = "highlight-python" > < pre > handlers:
- name: restart memcached
2012-03-14 01:32:55 +01:00
action: service name=memcached state=restarted
- name: restart apache
action: service name=apache state=restarted< / pre >
2012-03-13 04:12:21 +01:00
< / div >
< p > Handlers are best used to restart services and trigger reboots. You probably
won’ t need them for much else.< / p >
2012-03-21 01:15:53 +01:00
< div class = "admonition note" >
< p class = "first admonition-title" > Note< / p >
< p class = "last" > Notify handlers are always run in the order written.< / p >
< / div >
2012-03-08 19:36:47 +01:00
< / div >
2012-08-01 04:19:04 +02:00
< div class = "section" id = "include-files-and-encouraging-reuse" >
< h2 > Include Files And Encouraging Reuse< a class = "headerlink" href = "#include-files-and-encouraging-reuse" title = "Permalink to this headline" > ¶< / a > < / h2 >
2012-03-13 04:12:21 +01:00
< p > Suppose you want to reuse lists of tasks between plays or playbooks. You can use
2012-08-01 04:19:04 +02:00
include files to do this. Use of included task lists is a great way to define a role
that system is going to fulfill. Remember, the goal of a play in a playbook is to map
a group of systems into multiple roles. Let’ s see what this looks like...< / p >
< p > A task include file simply contains a flat list of tasks, like so:< / p >
2012-03-13 04:12:21 +01:00
< div class = "highlight-python" > < pre > ---
2012-03-14 01:32:55 +01:00
# possibly saved as tasks/foo.yml
2012-03-13 04:12:21 +01:00
- name: placeholder foo
action: command /bin/foo
- name: placeholder bar
action: command /bin/bar< / pre >
< / div >
2012-08-01 04:19:04 +02:00
< p > Include directives look like this, and can be mixed in with regular tasks in a playbook:< / p >
2012-08-03 04:08:00 +02:00
< div class = "highlight-python" > < pre > tasks:
- include: tasks/foo.yml< / pre >
2012-03-14 02:55:55 +01:00
< / div >
2012-08-01 04:19:04 +02:00
< p > You can also pass variables into includes. We call this a ‘ parameterized include’ .< / p >
2012-03-09 17:44:14 +01:00
< p > For instance, if deploying multiple wordpress instances, I could
2012-03-13 04:12:21 +01:00
contain all of my wordpress tasks in a single wordpress.yml file, and use it like so:< / p >
2012-08-03 04:08:00 +02:00
< div class = "highlight-python" > < pre > tasks:
2012-03-13 04:12:21 +01:00
- include: wordpress.yml user=timmy
- include: wordpress.yml user=alice
- include: wordpress.yml user=bob< / pre >
2012-03-09 05:06:07 +01:00
< / div >
2012-08-01 04:19:04 +02:00
< p > Variables passed in can then be used in the included files. You can reference them like this:< / p >
2012-05-02 06:34:56 +02:00
< div class = "highlight-python" > < pre > $user< / pre >
< / div >
2012-08-01 04:19:04 +02:00
< p > (In addition to the explicitly passed in parameters, all variables from
the vars section are also available for use here as well.)< / p >
2012-03-10 00:29:01 +01:00
< div class = "admonition note" >
< p class = "first admonition-title" > Note< / p >
2012-08-01 04:19:04 +02:00
< p class = "last" > Task include statements are only usable one-level deep.
This means task includes can not include other
task includes. This may change in a later release.< / p >
2012-03-10 00:29:01 +01:00
< / div >
2012-03-13 04:12:21 +01:00
< p > Includes can also be used in the ‘ handlers’ section, for instance, if you
want to define how to restart apache, you only have to do that once for all
2012-05-02 06:55:54 +02:00
of your playbooks. You might make a handlers.yml that looks like:< / p >
2012-03-13 04:12:21 +01:00
< div class = "highlight-python" > < pre > ----
2012-03-14 01:32:55 +01:00
# this might be in a file like handlers/handlers.yml
2012-03-13 04:12:21 +01:00
- name: restart apache
action: service name=apache state=restarted< / pre >
< / div >
< p > And in your main playbook file, just include it like so, at the bottom
of a play:< / p >
< div class = "highlight-python" > < pre > handlers:
2012-03-14 01:32:55 +01:00
- include: handlers/handlers.yml< / pre >
2012-03-13 04:12:21 +01:00
< / div >
< p > You can mix in includes along with your regular non-included tasks and handlers.< / p >
2012-08-10 04:12:10 +02:00
< div class = "admonition note" >
< p class = "first admonition-title" > Note< / p >
< p class = "last" > You can not conditionally path the location to an include file,
like you can with ‘ vars_files’ . If you find yourself needing to do
this, consider how you can restructure your playbook to be more
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 >
2012-03-09 13:42:53 +01:00
< / div >
2012-03-08 19:36:47 +01:00
< div class = "section" id = "executing-a-playbook" >
< h2 > Executing A Playbook< a class = "headerlink" href = "#executing-a-playbook" title = "Permalink to this headline" > ¶< / a > < / h2 >
2012-03-13 04:12:21 +01:00
< p > Now that you’ ve learned playbook syntax, how do you run a playbook? It’ s simple.
Let’ s run a playbook using a parallelism level of 10:< / p >
< div class = "highlight-python" > < pre > ansible-playbook playbook.yml -f 10< / pre >
2012-03-08 19:36:47 +01:00
< / div >
2012-07-04 23:47:04 +02:00
< / div >
< div class = "section" id = "tips-and-tricks" >
< h2 > Tips and Tricks< a class = "headerlink" href = "#tips-and-tricks" title = "Permalink to this headline" > ¶< / a > < / h2 >
< p > Look at the bottom of the playbook execution for a summary of the nodes that were executed
and how they performed. General failures and fatal “ unreachable” communication attempts are
kept seperate in the counts.< / p >
< p > If you ever want to see detailed output from successful modules as well as unsuccessful ones,
use the ‘ – verbose’ flag. This is available in Ansible 0.5 and later.< / p >
< p > Also, in version 0.5 and later, Ansible playbook output is vastly upgraded if the cowsay
package is installed. Try it!< / p >
2012-08-17 04:08:17 +02:00
< p > In version 0.7 and later, to see what hosts would be affected by a playbook before you run it, you
can do this:< / p >
2012-08-17 04:09:45 +02:00
< div class = "highlight-python" > < pre > ansible-playbook playbook.yml --list-hosts.< / pre >
2012-08-17 04:08:17 +02:00
< / div >
2012-03-31 15:29:31 +02:00
< 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 = "YAMLSyntax.html" > < em > YAML Syntax< / em > < / a > < / dt >
< dd > Learn about YAML syntax< / dd >
2012-05-13 17:56:09 +02:00
< dt > < a class = "reference internal" href = "" > < em > Playbooks< / em > < / a > < / dt >
< dd > Review the basic Playbook language features< / dd >
2012-05-13 17:00:02 +02:00
< dt > < a class = "reference internal" href = "playbooks2.html" > < em > Advanced Playbooks< / em > < / a > < / dt >
< dd > Learn about Advanced Playbook Features< / dd >
2012-05-13 17:56:09 +02:00
< dt > < a class = "reference internal" href = "bestpractices.html" > < em > Best Practices< / em > < / a > < / dt >
< dd > Various tips about managing playbooks in the real world< / dd >
2012-03-31 15:29:31 +02:00
< dt > < a class = "reference internal" href = "modules.html" > < em > Ansible Modules< / em > < / a > < / dt >
< dd > Learn about available modules< / dd >
2012-05-13 17:00:02 +02:00
< dt > < a class = "reference internal" href = "moduledev.html" > < em > Module Development< / em > < / a > < / dt >
2012-03-31 15:29:31 +02:00
< dd > Learn how to extend Ansible by writing your own modules< / dd >
2012-05-13 17:00:02 +02:00
< dt > < a class = "reference internal" href = "patterns.html" > < em > Inventory & Patterns< / em > < / a > < / dt >
2012-03-31 15:29:31 +02:00
< dd > Learn about how to select hosts< / dd >
2012-08-07 04:00:50 +02:00
< dt > < a class = "reference external" href = "https://github.com/ansible/ansible/tree/devel/examples/playbooks" > Github examples directory< / a > < / dt >
2012-03-31 15:29:31 +02:00
< dd > Complete playbook files from the github project source< / dd >
2012-03-20 04:43:03 +01:00
< 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 >
2012-03-31 15:29:31 +02:00
< / dl >
2012-03-14 01:32:55 +01:00
< / 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 >
2012-03-09 17:44:14 +01:00
© Copyright 2012 Michael DeHaan.< br / >
2012-08-25 01:33:14 +02:00
Last updated on Aug 24, 2012.< br / >
2012-03-09 17:44:14 +01:00
< / p >
< / div >
< / footer >
2012-03-07 17:35:18 +01:00
< / body >
< / html >