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-03-31 04:28:30 +02:00
< link rel = "next" title = "API & Integrations" href = "api.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 >
< 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-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-03-10 03:50:00 +01:00
< li class = "toctree-l1" > < a class = "reference internal" href = "gettingstarted.html" > Downloads & Getting Started< / a > < / li >
2012-03-09 17:44:14 +01:00
< li class = "toctree-l1" > < a class = "reference internal" href = "patterns.html" > The Inventory File, Patterns, and Groups< / a > < / li >
< li class = "toctree-l1" > < a class = "reference internal" href = "examples.html" > Command Line Examples< / a > < / li >
< 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-03-31 04:28:30 +02:00
< li class = "toctree-l1" > < a class = "reference internal" href = "api.html" > API & Integrations< / a > < / li >
2012-03-14 17:50:40 +01:00
< li class = "toctree-l1" > < a class = "reference internal" href = "moduledev.html" > Module Development Guide< / a > < / li >
2012-03-10 03:50:00 +01:00
< li class = "toctree-l1" > < a class = "reference internal" href = "faq.html" > Frequently Asked Questions< / 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 >
< li > < a class = "reference internal" href = "#playbook-example" > Playbook Example< / a > < / li >
< 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-03-31 15:36:37 +02:00
< li > < a class = "reference internal" href = "#power-tricks" > Power Tricks< / a > < ul >
2012-04-13 04:47:15 +02:00
< li > < a class = "reference internal" href = "#local-playbooks" > Local Playbooks< / a > < / li >
2012-04-03 03:07:33 +02:00
< li > < a class = "reference internal" href = "#external-variables-and-prompted-or-sensitive-data" > External Variables And Prompted or Sensitive Data< / a > < / li >
2012-03-21 01:15:53 +01:00
< li > < a class = "reference internal" href = "#conditional-execution" > Conditional Execution< / a > < / li >
2012-03-20 04:43:03 +01:00
< li > < a class = "reference internal" href = "#conditional-imports" > Conditional Imports< / a > < / li >
2012-03-31 15:36:37 +02:00
< li > < a class = "reference internal" href = "#include-files-and-reuse" > Include Files And Reuse< / a > < / li >
< li > < a class = "reference internal" href = "#using-includes-to-assign-classes-of-systems" > Using Includes To Assign Classes of Systems< / a > < / li >
2012-04-18 02:04:24 +02:00
< li > < a class = "reference internal" href = "#loop-shorthand" > Loop Shorthand< / a > < / li >
2012-03-31 15:36:37 +02:00
< li > < a class = "reference internal" href = "#asynchronous-actions-and-polling" > Asynchronous Actions and Polling< / a > < / li >
< / ul >
< / li >
< li > < a class = "reference internal" href = "#executing-a-playbook" > Executing A Playbook< / a > < / li >
< / 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-19 15:39:50 +02:00
< a href = "http://github.com/ansible/ansible" > < img style = "position: absolute; right: 0; border: 0;" src = "https://a248.e.akamai.net/assets.github.com/img/e6bef7a091f5f3138b8cd40bc3e114258dd68ddf/687474703a2f2f73332e616d617a6f6e6177732e636f6d2f6769746875622f726962626f6e732f666f726b6d655f72696768745f7265645f6161303030302e706e67" 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-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 >
< p > Playbooks are a completely different way to use ansible and are
2012-03-31 16:17:35 +02:00
particularly awesome. They are the basis for a really simple
configuration management and multi-machine deployment system,
unlike any that already exist, and
2012-03-09 17:44:14 +01:00
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
the < a class = "reference external" href = "https://github.com/ansible/ansible/tree/master/examples/playbooks" > github examples directory< / a > in
another tab, so you can apply the theory to what things look like in practice.< / p >
2012-03-08 19:36:47 +01:00
< div class = "section" id = "playbook-example" >
< h2 > Playbook Example< a class = "headerlink" href = "#playbook-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 >
< 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-03-10 00:29:01 +01:00
separated by colons, as described in the < a class = "reference internal" href = "patterns.html#patterns" > < em > The Inventory File, Patterns, and Groups< / 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 > ---
- hosts: webservers
user: yourname
sudo: True< / pre >
< / div >
2012-04-14 14:45:27 +02:00
< p > If you need to specify a password to sudo, run < cite > ansible-playbook< / cite > with < cite > – ask-sudo-pass< / cite > (< cite > -K< / cite > ).
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-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 >
< p > These variables can be used later in the playbook, or on the managed system (in templates), just like this:< / p >
2012-03-27 01:48:32 +02:00
< div class = "highlight-python" > < pre > {{ varname }}< / pre >
2012-03-09 05:06:07 +01:00
< / div >
2012-03-21 01:15:53 +01:00
< p > Within playbooks themselves, but not within templates on the remote machines, it’ s also legal
to use nicer shorthand like this:< / p >
< div class = "highlight-python" > < pre > $varname< / pre >
< / div >
2012-04-19 05:02:28 +02:00
< p > Further, if there are discovered variables about the system (ansible provides some of these,
plus we include ones taken from facter or ohai if installed) these variables bubble up back into the
2012-03-09 17:44:14 +01:00
playbook, and can be used on each system just like explicitly set
2012-03-21 01:15:53 +01:00
variables.< / p >
< p > Facter variables are prefixed with < tt class = "docutils literal" > < span class = "pre" > facter_< / span > < / tt > and Ohai
2012-04-19 05:02:28 +02:00
variables are prefixed with < tt class = "docutils literal" > < span class = "pre" > ohai_< / span > < / tt > . Ansible variables (0.3 and later)
are not surprisingly prefixed with < tt class = "docutils literal" > < span class = "pre" > ansible_< / span > < / tt > . 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-03-09 17:44:14 +01:00
before moving on to the next task.< / p >
< p > 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-03-13 04:12:21 +01:00
< p > Modules other than < cite > command< / cite > 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 >
< p > Command will actually rerun the same command again,
which is totally ok if the command is something like
‘ chmod’ or ‘ setsebool’ , etc.< / p >
2012-03-09 17:44:14 +01:00
< p > Every task must have a name, 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
nice to have reasonably good descriptions of each task step.< / p >
< 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 >
< p > The command module is the one module that just takes a list
2012-03-21 01:15:53 +01:00
of arguments, and doesn’ t use the key=value form. This makes
it work just like you would expect. Simple:< / p >
< div class = "highlight-python" > < pre > tasks:
- name: disable selinux
action: command /sbin/setenforce 0< / pre >
2012-03-13 04:12:21 +01:00
< / div >
< 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:
- name: make a directory
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-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 >
< p > As we’ ve mentioned, nearly all modules are written to be ‘ idempotent’ and can relay when
2012-03-13 04:12:21 +01:00
they have affected a change on the remote system. Playbooks recognize this and
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-03-13 04:12:21 +01:00
< div class = "section" id = "power-tricks" >
< h2 > Power Tricks< a class = "headerlink" href = "#power-tricks" title = "Permalink to this headline" > ¶< / a > < / h2 >
< p > Now that you have the basics down, let’ s learn some more advanced
things you can do with playbooks.< / p >
2012-04-13 04:47:15 +02:00
< div class = "section" id = "local-playbooks" >
< h3 > Local Playbooks< a class = "headerlink" href = "#local-playbooks" title = "Permalink to this headline" > ¶< / a > < / h3 >
< p > It may be useful to use a playbook locally, rather than by connecting over SSH. This can be useful
for assuring the configuration of a system by putting a playbook on a crontab. This may also be used
to run a playbook inside a OS installer, such as an Anaconda kickstart.< / p >
< p > To run an entire playbook locally, just set the “ hosts:” line to “ hosts:127.0.0.1” and then run the playbook like so:< / p >
< div class = "highlight-python" > < pre > playbook playbook.yml --connection=local< / pre >
< / div >
< p > Alternatively, a local connection can be used in a single playbook play, even if other plays in the playbook
use the default remote connection type:< / p >
< div class = "highlight-python" > < pre > hosts: 127.0.0.1
connection: local< / pre >
< / div >
< / div >
2012-04-03 03:07:33 +02:00
< div class = "section" id = "external-variables-and-prompted-or-sensitive-data" >
< h3 > External Variables And Prompted or Sensitive Data< a class = "headerlink" href = "#external-variables-and-prompted-or-sensitive-data" title = "Permalink to this headline" > ¶< / a > < / h3 >
2012-03-13 04:12:21 +01:00
< 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
2012-04-03 03:07:33 +02:00
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 >
2012-03-13 04:12:21 +01:00
< div class = "highlight-python" > < pre > ---
- hosts: all
user: root
vars:
favcolor: blue
vars_files:
2012-03-14 01:32:55 +01:00
- /vars/external_vars.yml
2012-03-13 04:12:21 +01:00
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 >
< div class = "highlight-python" > < pre > ---
2012-03-14 01:32:55 +01:00
# in the above example, this would be vars/external_vars.yml
2012-03-13 04:12:21 +01:00
somevar: somevalue
password: magic< / pre >
< / div >
2012-04-03 03:03:46 +02:00
< p > Alternatively, you may wish to prompt the user for certain input, and can
2012-04-03 03:07:33 +02:00
do so with the similarly named ‘ vars_prompt’ section. This has uses
beyond security, for instance, you may use the same playbook for all
software releases and would prompt for a particular release version
in a push-script:< / p >
2012-04-03 03:03:46 +02:00
< div class = "highlight-python" > < pre > ---
- hosts: all
user: root
vars:
from: "camelot"
vars_prompt:
name: "what is your name?"
quest: "what is your quest?"
favcolor: "what is your favorite color?"< / pre >
< / div >
< p > There are full examples of both of these items in the github examples/playbooks directory.< / p >
2012-03-13 04:12:21 +01:00
< / div >
2012-03-21 01:15:53 +01:00
< div class = "section" id = "conditional-execution" >
< h3 > Conditional Execution< a class = "headerlink" href = "#conditional-execution" title = "Permalink to this headline" > ¶< / a > < / h3 >
< p > Sometimes you will want to skip a particular step on a particular host. This could be something
as simple as not installing a certain package if the operating system is a particular version,
or it could be something like performing some cleanup steps if a filesystem is getting full.< / p >
< p > This is easy to do in Ansible, with the < cite > only_if< / cite > clause. This clause can be applied to any task,
and allows usage of variables from anywhere in ansible, either denoted with < cite > $dollar_sign_syntax< / cite > or
< cite > {{ braces_syntax }}< / cite > and then evaluates them with a Python expression. Don’ t panic – it’ s actually
2012-04-13 04:47:15 +02:00
pretty simple:< / p >
2012-03-21 01:15:53 +01:00
< div class = "highlight-python" > < pre > vars:
2012-03-22 06:01:02 +01:00
favcolor: blue
2012-03-22 01:35:06 +01:00
is_favcolor_blue: "'$favcolor' == 'blue'"
is_centos: "'$facter_operatingsystem' == 'CentOS'"
2012-03-21 01:15:53 +01:00
tasks:
- name: "shutdown if my favorite color is blue"
action: command /sbin/shutdown -t now
2012-03-22 01:35:06 +01:00
only_if: '$is_favcolor_blue'< / pre >
2012-03-21 01:15:53 +01:00
< / div >
2012-04-19 05:02:28 +02:00
< p > Variables from tools like < cite > facter< / cite > and < cite > ohai< / cite > can be used here, if installed, or you can
use variables that bubble up from ansible (0.3 and later). As a reminder,
these variables are prefixed, so it’ s < cite > $facter_operatingsystem< / cite > , not < cite > $operatingsystem< / cite > . Ansible’ s
built in variables are prefixed with < cite > ansible_< / cite > . The only_if
2012-03-21 01:15:53 +01:00
expression is actually a tiny small bit of Python, so be sure to quote variables and make something
2012-03-22 01:35:06 +01:00
that evaluates to < cite > True< / cite > or < cite > False< / cite > . It is a good idea to use ‘ vars_files’ instead of ‘ vars’ to define
all of your conditional expressions in a way that makes them very easy to reuse between plays
and playbooks.< / p >
2012-03-21 01:15:53 +01:00
< / div >
2012-03-20 04:43:03 +01:00
< div class = "section" id = "conditional-imports" >
< h3 > Conditional Imports< a class = "headerlink" href = "#conditional-imports" title = "Permalink to this headline" > ¶< / a > < / h3 >
< p > Sometimes you will want to do certain things differently in a playbook based on certain criteria.
Having one playbook that works on multiple platforms and OS versions is a good example.< / p >
< p > As an example, the name of the Apache package may be different between CentOS and Debian,
but it is easily handled with a minimum of syntax in an Ansible Playbook:< / p >
< div class = "highlight-python" > < pre > ---
- hosts: all
user: root
vars_files:
2012-03-20 04:56:35 +01:00
- "vars/common.yml"
- [ "vars/$facter_operatingsystem.yml", "vars/os_defaults.yml" ]
2012-03-20 04:43:03 +01:00
tasks:
- name: make sure apache is running
action: service name=$apache state=running< / pre >
< / div >
< p > As a reminder, the various YAML files contain just keys and values:< / p >
< div class = "highlight-python" > < pre > ---
# for vars/CentOS.yml
apache: httpd
somethingelse: 42< / pre >
< / div >
< p > How does this work? If the operating system was ‘ CentOS’ , the first file Ansible would try to import
would be ‘ vars/CentOS.yml’ , followed up by ‘ /vars/os_defaults.yml’ if that file
did not exist. If no files in the list were found, an error would be raised.
On Debian, it would instead first look towards ‘ vars/Debian.yml’ instead of ‘ vars/CentOS.yml’ , before
falling back on ‘ vars/os_defaults.yml’ . Pretty simple.< / p >
< p > To use this conditional import feature, you’ ll need facter or ohai installed prior to running the playbook, but
you can of course push this out with Ansible if you like:< / p >
< div class = "highlight-python" > < pre > # for facter
ansible -m yum -a "pkg=facter ensure=installed"
ansible -m yum -a "pkg=ruby-json ensure=installed"
# for ohai
ansible -m yum -a "pkg=ohai ensure=installed"< / pre >
< / div >
< p > Ansible’ s approach to configuration – seperating variables from tasks, keeps your playbooks
from turning into arbitrary code with ugly nested ifs, conditionals, and so on - and results
in more streamlined & auditable configuration rules – especially because there are a
minimum of decision points to track.< / p >
< / div >
2012-03-13 04:12:21 +01:00
< div class = "section" id = "include-files-and-reuse" >
< h3 > Include Files And Reuse< a class = "headerlink" href = "#include-files-and-reuse" title = "Permalink to this headline" > ¶< / a > < / h3 >
< p > Suppose you want to reuse lists of tasks between plays or playbooks. You can use
include files to do this.< / p >
2012-03-14 01:32:55 +01:00
< p > An 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-03-14 01:32:55 +01:00
< p > Include directives look like this:< / p >
2012-03-14 02:55:55 +01:00
< div class = "highlight-python" > < pre > - tasks:
- include: tasks/foo.yml< / pre >
< / div >
2012-03-14 01:32:55 +01:00
< p > Variables passed in can be used in the include files too. Assume a variable named ‘ user’ . Using
2012-03-13 04:12:21 +01:00
< cite > jinja2< / cite > syntax, anywhere in the included file, you can say:< / p >
2012-03-27 01:48:32 +02:00
< div class = "highlight-python" > < pre > {{ user }}< / pre >
2012-03-09 05:06:07 +01:00
< / div >
2012-03-14 01:32:55 +01:00
< p > I can also pass variables into includes directly. We might 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-03-09 05:06:07 +01: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-03-09 17:44:14 +01:00
< p > In addition to the explicitly passed in parameters, all variables from
2012-03-13 04:12:21 +01:00
the vars section are also available for use here as well. Variables that bubble
2012-03-14 01:32:55 +01:00
up from tools like facter and ohai are not usable here though – but they ARE available for use
inside ‘ action’ lines and in templates.< / p >
2012-03-10 00:29:01 +01:00
< div class = "admonition note" >
< p class = "first admonition-title" > Note< / p >
2012-03-13 04:12:21 +01:00
< p class = "last" > Include statements are only usable from the top level
playbook file. This means includes can not include other
2012-03-10 00:29:01 +01:00
includes.< / p >
< / 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
of your playbooks. You might make a notifiers.yaml that looked like:< / p >
< 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-03-20 04:43:03 +01:00
< p > Note that 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.< / p >
2012-03-09 13:42:53 +01:00
< / div >
< div class = "section" id = "using-includes-to-assign-classes-of-systems" >
2012-03-13 04:12:21 +01:00
< h3 > Using Includes To Assign Classes of Systems< a class = "headerlink" href = "#using-includes-to-assign-classes-of-systems" title = "Permalink to this headline" > ¶< / a > < / h3 >
< p > Include files are really powerful when used to reuse logic between playbooks. You
2012-03-09 17:44:14 +01:00
could imagine a playbook describing your entire infrastructure like
2012-03-13 04:12:21 +01:00
this, in a list of just a few plays:< / p >
2012-03-09 13:42:53 +01:00
< div class = "highlight-python" > < pre > ---
- hosts: atlanta-webservers
vars:
datacenter: atlanta
tasks:
2012-03-14 01:32:55 +01:00
- include: tasks/base.yml
- include: tasks/webservers.yml database=db.atlanta.com
2012-03-09 13:42:53 +01:00
handlers:
2012-03-14 01:32:55 +01:00
- include: handlers/common.yml
2012-03-09 13:42:53 +01:00
- hosts: atlanta-dbservers
vars:
datacenter: atlanta
tasks:
2012-03-14 01:32:55 +01:00
- include: tasks/base.yml
- include: tasks/dbservers.yml
2012-03-09 13:42:53 +01:00
handlers:
2012-03-14 01:32:55 +01:00
- include: handlers/common.yml< / pre >
2012-03-09 13:42:53 +01:00
< / div >
2012-03-09 17:44:14 +01:00
< p > There is one (or more) play defined for each group of systems, and
2012-03-13 04:12:21 +01:00
each play maps each group to several includes. These includes represent
2012-03-14 01:32:55 +01:00
‘ class definitions’ , telling the systems what they are supposed to do or be.
In the above example, all hosts get the base configuration first and further
customize it depending on what class or nature of machines they are.< / p >
2012-03-13 04:12:21 +01:00
< div class = "admonition note" >
< p class = "first admonition-title" > Note< / p >
< p class = "last" > Playbooks do not always have to be declarative; you can do something
similar to model a push process for a multi-tier web application. This is
actually one of the things playbooks were invented to do.< / p >
< / div >
2012-03-08 19:36:47 +01:00
< / div >
2012-04-18 02:04:24 +02:00
< div class = "section" id = "loop-shorthand" >
< h3 > Loop Shorthand< a class = "headerlink" href = "#loop-shorthand" title = "Permalink to this headline" > ¶< / a > < / h3 >
< p > To save some typing, repeated tasks can be written in short-hand like so:< / p >
< div class = "highlight-python" > < pre > - name: add user $item
action: user name=$item state=present groups=wheel
with_items:
- testuser1
- testuser2< / pre >
< / div >
< / div >
2012-03-08 19:36:47 +01:00
< div class = "section" id = "asynchronous-actions-and-polling" >
2012-03-13 04:12:21 +01:00
< h3 > Asynchronous Actions and Polling< a class = "headerlink" href = "#asynchronous-actions-and-polling" title = "Permalink to this headline" > ¶< / a > < / h3 >
< p > By default tasks in playbooks block, meaning the connections stay open
until the task is done on each node. If executing playbooks with
a small parallelism value (aka < cite > – forks< / cite > ), you may wish that long
running operations can go faster. The easiest way to do this is
to kick them off all at once and then poll until they are done.< / p >
< p > You will also want to use asynchronous mode on very long running
operations that might be subject to timeout.< / p >
2012-04-13 04:47:15 +02:00
< p > To launch a task asynchronously, specify its maximum runtime
2012-03-13 04:12:21 +01:00
and how frequently you would like to poll for status. The default
poll value is 10 seconds if you do not specify a value for < cite > poll< / cite > :< / p >
< div class = "highlight-python" > < pre > ---
- hosts: all
user: root
tasks:
- name: simulate long running op (15 sec), wait for up to 45, poll every 5
action: command /bin/sleep 15
async: 45
poll: 5< / pre >
< / div >
< div class = "admonition note" >
< p class = "first admonition-title" > Note< / p >
< p class = "last" > There is no default for the async time limit. If you leave off the
‘ async’ keyword, the task runs synchronously, which is Ansible’ s
default.< / p >
< / div >
< p > Alternatively, if you do not need to wait on the task to complete, you may
“ fire and forget” by specifying a poll value of 0:< / p >
< div class = "highlight-python" > < pre > ---
- hosts: all
user: root
tasks:
2012-03-14 01:32:55 +01:00
- name: simulate long running op, allow to run for 45, fire and forget
2012-03-13 04:12:21 +01:00
action: command /bin/sleep 15
async: 45
poll: 0< / pre >
< / div >
< div class = "admonition note" >
< p class = "first admonition-title" > Note< / p >
< p class = "last" > You shouldn’ t “ fire and forget” with operations that require
exclusive locks, such as yum transactions, if you expect to run other
commands later in the playbook against those same resources.< / p >
< / div >
< div class = "admonition note" >
< p class = "first admonition-title" > Note< / p >
< p class = "last" > Using a higher value for < cite > – forks< / cite > will result in kicking off asynchronous
tasks even faster. This also increases the efficiency of polling.< / p >
< / div >
< / div >
2012-03-08 19:36:47 +01:00
< / div >
< 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-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 >
< dt > < a class = "reference internal" href = "modules.html" > < em > Ansible Modules< / em > < / a > < / dt >
< dd > Learn about available modules< / dd >
< dt > < a class = "reference internal" href = "moduledev.html" > < em > Module Development Guide< / em > < / a > < / dt >
< dd > Learn how to extend Ansible by writing your own modules< / dd >
< dt > < a class = "reference internal" href = "patterns.html" > < em > The Inventory File, Patterns, and Groups< / em > < / a > < / dt >
< dd > Learn about how to select hosts< / dd >
< dt > < a class = "reference external" href = "https://github.com/ansible/ansible/tree/master/examples/playbooks" > Github examples directory< / a > < / dt >
< 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 >
< / div >
2012-03-09 17:44:14 +01:00
< footer class = "footer" >
< div class = "container" >
< p class = "pull-right" > < a href = "#" > Back to top< / a > < / p >
< p >
© Copyright 2012 Michael DeHaan.< br / >
2012-04-22 17:43:03 +02:00
Last updated on Apr 22, 2012.< br / >
2012-03-27 01:48:32 +02:00
Created using < a href = "http://sphinx.pocoo.org/" > Sphinx< / a > 1.0.8.< br / >
2012-03-09 17:44:14 +01:00
< / p >
< / div >
< / footer >
2012-03-07 17:35:18 +01:00
< / body >
< / html >