2012-03-09 17:44:14 +01:00
Playbooks
=========
2012-03-07 17:35:18 +01:00
2012-08-28 21:41:10 +02:00
.. contents ::
:depth: 2
Introduction
`` ` ` ` ` ` ` ` ` ``
2012-08-01 04:19:04 +02:00
Playbooks are a completely different way to use ansible than in task execution mode, and are
2012-09-07 15:16:08 +02:00
particularly powerful. Simply put, playbooks are the basis for a really simple
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.
2012-03-09 04:50:00 +01:00
2012-03-31 16:17:35 +02:00
Playbooks can declare configurations, but they can also orchestrate steps of
any manual ordered process, even as different steps must bounce back and forth
2012-09-07 15:16:08 +02:00
between sets of machines in particular orders. They can launch tasks
2012-03-31 16:17:35 +02:00
synchronously or asynchronously.
2012-03-13 04:12:21 +01:00
2012-03-09 17:44:14 +01:00
While you might run the main /usr/bin/ansible program for ad-hoc
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.
2012-03-08 19:36:47 +01:00
2012-09-07 15:16:08 +02:00
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 `github examples directory <https://github.com/ansible/ansible/tree/devel/examples/playbooks> `_ in
2012-03-31 16:17:35 +02:00
another tab, so you can apply the theory to what things look like in practice.
2012-03-08 19:36:47 +01:00
2013-03-20 17:45:41 +01:00
There are also some full sets of playbooks illustrating a lot of these techniques in the
`ansible-examples repository <https://github.com/ansible/ansible-examples> `_ .
2012-08-01 04:19:04 +02:00
Playbook Language Example
`` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ``
2012-03-08 19:36:47 +01:00
2012-03-09 17:44:14 +01:00
Playbooks are expressed in YAML format and have a minimum of syntax.
2012-09-07 15:16:08 +02:00
Each playbook is composed of one or more 'plays' in a list.
2012-03-13 04:12:21 +01:00
2013-06-11 20:54:42 +02:00
The goal of a play is to map a group of hosts to some well defined roles, represented by
2013-06-11 20:58:28 +02:00
things ansible calls tasks. At a basic level, a task is nothing more than a call
2012-08-01 04:19:04 +02:00
to an ansible module, which you should have learned about in earlier chapters.
2012-03-13 04:12:21 +01:00
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-09-07 15:16:08 +02:00
server group, then more commands back on the webservers group, etc.
2012-03-13 04:12:21 +01:00
2012-04-13 00:20:52 +02:00
For starters, here's a playbook that contains just one play::
2012-03-08 19:36:47 +01:00
---
2012-03-08 19:53:48 +01:00
- hosts: webservers
2012-03-08 19:36:47 +01:00
vars:
http_port: 80
max_clients: 200
user: root
tasks:
2012-03-13 00:23:58 +01:00
- name: ensure apache is at the latest version
2013-07-15 19:50:48 +02:00
yum: pkg=httpd state=latest
2012-03-08 19:36:47 +01:00
- name: write the apache config file
2013-07-15 19:50:48 +02:00
template: src=/srv/httpd.j2 dest=/etc/httpd.conf
2012-03-08 19:36:47 +01:00
notify:
- restart apache
- name: ensure apache is running
2013-07-15 19:50:48 +02:00
service: name=httpd state=started
2012-03-08 19:36:47 +01:00
handlers:
2012-03-14 01:32:55 +01:00
- name: restart apache
2013-07-15 19:50:48 +02:00
service: name=httpd state=restarted
2012-03-13 04:12:21 +01:00
Below, we'll break down what the various features of the playbook language are.
2012-03-31 15:36:37 +02:00
Basics
`` ` ` ``
2012-03-13 04:12:21 +01:00
2012-03-21 01:15:53 +01:00
Hosts and Users
+++++++++++++++
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.
2012-03-08 19:36:47 +01:00
2012-03-13 04:12:21 +01:00
The `hosts` line is a list of one or more groups or host patterns,
2012-03-09 20:39:29 +01:00
separated by colons, as described in the :ref: `patterns`
2012-03-21 01:15:53 +01:00
documentation. The `user` is just the name of the user account::
2012-03-13 04:12:21 +01:00
2012-03-21 01:15:53 +01:00
---
- hosts: webservers
user: root
2012-03-13 04:12:21 +01:00
2012-03-31 18:08:28 +02:00
Support for running things from sudo is also available::
2012-09-07 15:16:08 +02:00
2012-03-31 04:10:32 +02:00
---
- hosts: webservers
user: yourname
2012-12-14 11:56:53 +01:00
sudo: yes
2012-07-28 14:42:20 +02:00
2013-05-02 21:14:53 +02:00
You can also use sudo on a particular task instead of the whole play::
---
- hosts: webservers
user: yourname
tasks:
- service: name=nginx state=started
sudo: yes
2012-07-28 14:42:20 +02:00
You can also login as you, and then sudo to different users than root::
---
- hosts: webservers
user: yourname
2012-12-14 11:56:53 +01:00
sudo: yes
2012-07-28 14:42:20 +02:00
sudo_user: postgres
2012-03-13 04:12:21 +01:00
2012-05-02 07:35:02 +02:00
If you need to specify a password to sudo, run `ansible-playbook` with `` --ask-sudo-pass `` (`-K` ).
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 `Control-C` to kill it and run it again with `-K` .
2012-03-08 19:36:47 +01:00
2012-08-07 04:43:35 +02:00
.. important ::
When using `sudo_user` 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
`sudo_user` set. In other cases, '/tmp' is not used and this does
not come into play. Ansible also takes care to not log password
parameters.
2012-07-28 14:42:20 +02:00
2012-03-08 19:36:47 +01:00
Vars section
2012-03-31 15:36:37 +02:00
++++++++++++
2012-03-08 19:36:47 +01:00
2012-03-21 02:04:47 +01:00
The `vars` section contains a list of variables and values that can be used in the plays, like this::
2012-03-21 01:15:53 +01:00
---
- hosts: webservers
2012-08-31 16:44:26 +02:00
user: root
2012-03-21 01:15:53 +01:00
vars:
http_port: 80
van_halen_port: 5150
2012-09-07 15:16:08 +02:00
other: 'magic'
2012-03-21 01:15:53 +01:00
2013-04-21 18:45:45 +02:00
.. note ::
You can also keep variables in separate files and include them alongside inline `vars` with a `vars_files` declaration
2013-06-20 03:34:02 +02:00
in the play. See the `Advanced Playbooks chapter <http://www.ansibleworks.com/docs/playbooks2.html#variable-file-separation> `_
2013-04-21 18:45:45 +02:00
for more info.
2012-05-13 17:00:02 +02:00
These variables can be used later in the playbook like this::
2012-03-08 19:36:47 +01:00
2013-04-13 00:21:09 +02:00
$varname or ${varname} or {{ varname }}
2012-06-13 02:32:42 +02:00
2013-06-14 17:13:38 +02:00
If you ever want to do anything complex like uppercasing a string, {{ varname }} is best, as it uses the Jinja2 templating engine. It is a good idea to get in the habit of using this form most of the time when the output is to be a string.
2012-03-08 19:36:47 +01:00
2013-05-05 19:21:47 +02:00
If just referencing the value of another simple variable though, it's fine to say $x or ${x}. This is common for when a datastructure has a member that is the value of another datastructure.
2013-06-14 17:13:38 +02:00
To learn more about Jinja2, you can optionally see the `Jinja2 docs <http://jinja.pocoo.org/docs/> `_ - though remember that Jinja2 loops and conditionals are only for 'templates' in Ansible, in playbooks, ansible has the 'when' and 'with' keywords for conditionals and loops.
2012-03-21 01:15:53 +01:00
2013-08-12 15:49:34 +02:00
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
2013-08-11 06:54:05 +02:00
of these, prefixed with 'ansible', which are documented under 'setup' in the module documentation. Additionally,
2013-05-05 19:21:47 +02:00
facts can be gathered by ohai and facter if they are installed. Facter variables are prefixed with `` facter_ `` and Ohai variables are prefixed with `` ohai_ `` . These add extra dependencies and are only there for ease of users
2013-08-12 15:49:34 +02:00
porting over from those other configuration systems. Finally, it's possible to drop files
on to the remote systems that provide additional sources of fact data, see "Facts.d" as documented
in the Advanced Playbooks section.
2012-07-25 21:21:42 +02:00
2013-05-05 19:21:47 +02:00
How about an example. If I wanted to write the hostname into the /etc/motd file, I could say::
2012-03-09 04:50:00 +01:00
- name: write the motd
2013-07-15 19:50:48 +02:00
template: src=/srv/templates/motd.j2 dest=/etc/motd
2012-03-09 04:50:00 +01:00
2012-03-09 05:05:52 +01:00
And in /srv/templates/motd.j2::
2012-03-09 04:50:00 +01:00
You are logged into {{ facter_hostname }}
2013-05-29 15:42:13 +02:00
But we're getting ahead of ourselves, as that just showed a task in a playbook ahead of schedule. Let's talk about tasks.
2012-03-08 19:36:47 +01:00
Tasks list
2012-03-31 15:36:37 +02:00
++++++++++
2012-03-08 19:36:47 +01:00
2012-03-09 17:44:14 +01:00
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.
2012-03-09 04:50:00 +01:00
2012-09-07 15:16:08 +02:00
When running the playbook, which runs top to bottom, hosts with failed tasks are
2012-08-01 04:19:04 +02:00
taken out of the rotation for the entire playbook. If things fail, simply correct the playbook file and rerun.
2012-03-09 04:50:00 +01:00
2012-03-21 01:15:53 +01:00
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.
2012-08-01 04:19:04 +02:00
Modules are 'idempotent', meaning if you run them
2013-08-11 06:54:05 +02:00
again, they will make only the changes they must in order 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-09-07 15:16:08 +02:00
unless they have to change things.
2012-03-08 19:36:47 +01:00
2012-09-07 15:16:08 +02:00
The `command` and `shell` modules will typically rerun the same command again,
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.
2012-03-08 19:36:47 +01:00
2012-07-28 16:24:47 +02:00
Every task should 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
2012-07-28 16:24:47 +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.
2012-03-08 19:36:47 +01:00
2013-07-15 19:50:48 +02:00
Tasks can be declared using the legacy "action: module options" format, but
it is recommeded that you use the more conventional "module: options" format.
This recommended format is used throughout the documentation, but you may
encounter the older format in some playbooks.
2012-03-21 01:15:53 +01:00
Here is what a basic task looks like, as with most modules,
the service module takes key=value arguments::
2012-03-13 04:12:21 +01:00
2012-03-21 01:15:53 +01:00
tasks:
- name: make sure apache is running
2013-07-15 19:50:48 +02:00
service: name=httpd state=running
2012-03-13 04:12:21 +01:00
2012-05-02 07:35:02 +02:00
The `command` and `shell` 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::
2012-03-08 19:36:47 +01:00
2012-03-21 01:15:53 +01:00
tasks:
2012-09-07 15:16:08 +02:00
- name: disable selinux
2013-07-15 19:50:48 +02:00
command: /sbin/setenforce 0
2012-03-13 04:12:21 +01:00
2012-08-01 04:19:04 +02:00
The command and shell module care about return codes, so if you have a command
2013-08-11 06:54:05 +02:00
whose successful exit code is not zero, you may wish to do this::
2012-08-01 04:19:04 +02:00
tasks:
- name: run this command and ignore the result
2013-07-15 19:50:48 +02:00
shell: /usr/bin/somecommand || /bin/true
2012-08-01 04:19:04 +02:00
2013-03-30 02:38:33 +01:00
Or this::
tasks:
- name: run this command and ignore the result
2013-07-15 19:50:48 +02:00
shell: /usr/bin/somecommand
2013-03-30 02:38:33 +01:00
ignore_errors: True
2012-10-17 16:31:33 +02:00
If the action line is getting too long for comfort you can break it on
a space and indent any continuation lines::
tasks:
- name: Copy ansible inventory file to client
2013-07-15 19:50:48 +02:00
copy: src=/etc/ansible/hosts dest=/etc/ansible/hosts
2012-10-17 16:31:33 +02:00
owner=root group=root mode=0644
2012-03-13 04:12:21 +01:00
Variables can be used in action lines. Suppose you defined
a variable called 'vhost' in the 'vars' section, you could do this::
2012-03-09 04:50:00 +01:00
2012-03-21 01:15:53 +01:00
tasks:
2013-04-13 00:21:09 +02:00
- name: create a virtual host file for {{ vhost }}
2013-07-15 19:50:48 +02:00
template: src=somefile.j2 dest=/etc/httpd/conf.d/{{ vhost }}
2012-03-13 04:12:21 +01:00
Those same variables are usable in templates, which we'll get to later.
2012-03-09 04:50:00 +01:00
2012-08-01 04:19:04 +02:00
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.
2012-03-21 01:15:53 +01:00
2012-10-17 01:09:15 +02:00
Action Shorthand
`` ` ` ` ` ` ` ` ` ` ` ` ` ``
.. versionadded: 0.8
2013-08-02 02:28:37 +02:00
Ansible prefers listing modules like this in 0.8 and later::
2012-10-17 01:09:15 +02:00
2013-07-15 19:50:48 +02:00
template: src=templates/foo.j2 dest=/etc/foo.conf
2012-10-17 01:09:15 +02:00
2013-08-02 02:28:37 +02:00
You will notice in earlier versions, this was only available as::
2012-10-17 01:09:15 +02:00
2013-08-02 02:28:37 +02:00
action: template src=templates/foo.j2 dest=/etc/foo.conf
2012-10-17 01:09:15 +02:00
2013-08-02 02:28:37 +02:00
The old form continues to work in newer versions without any plan of deprecation.
2012-10-17 01:09:15 +02:00
2012-03-21 01:15:53 +01:00
Running Operations On Change
2012-03-31 15:36:37 +02:00
`` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ``
2012-03-08 19:36:47 +01:00
2012-08-01 04:19:04 +02:00
As we've mentioned, modules are written to be 'idempotent' and can relay when
2012-05-02 07:35:02 +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.
2013-04-21 00:10:01 +02:00
These 'notify' actions are triggered at the end of each block of tasks in a playbook, and will only be
2013-05-02 21:14:53 +02:00
triggered once even if notified by multiple different tasks.
2013-04-21 00:10:01 +02:00
For instance, multiple resources may indicate
2013-05-02 21:14:53 +02:00
that apache needs to be restarted because they have changed a config file,
2013-04-21 00:10:01 +02:00
but apache will only be bounced once to avoid unneccessary restarts.
2012-03-13 04:12:21 +01:00
Here's an example of restarting two services when the contents of a file
change, but only if the file changes::
- name: template configuration file
2013-07-15 19:50:48 +02:00
template: src=template.j2 dest=/etc/foo.conf
2012-03-13 04:12:21 +01:00
notify:
- restart memcached
2012-03-13 04:17:59 +01:00
- restart apache
2012-03-13 04:12:21 +01:00
2012-03-21 01:15:53 +01:00
The things listed in the 'notify' section of a task are called
2012-09-07 15:16:08 +02:00
handlers.
2012-03-08 19:36:47 +01:00
2012-03-09 17:44:14 +01:00
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
2012-09-07 15:16:08 +02:00
of the tasks complete in a particular play.
2012-03-08 19:36:47 +01:00
2012-03-13 04:12:21 +01:00
Here's an example handlers section::
handlers:
- name: restart memcached
2013-07-15 19:50:48 +02:00
service: name=memcached state=restarted
2012-03-13 04:17:59 +01:00
- name: restart apache
2013-07-15 19:50:48 +02:00
service: name=apache state=restarted
2012-03-13 04:12:21 +01:00
Handlers are best used to restart services and trigger reboots. You probably
won't need them for much else.
2012-03-21 01:15:53 +01:00
.. note ::
Notify handlers are always run in the order written.
2013-08-11 06:54:05 +02:00
Roles are described later on. It's worthwhile to point out that handlers are
2013-04-21 00:10:01 +02:00
automatically processed between 'pre_tasks', 'roles', 'tasks', and 'post_tasks'
sections. If you ever want to flush all the handler commands immediately though,
in 1.2 and later, you can::
tasks:
- shell: some tasks go here
- meta: flush_handlers
- shell: some other tasks
In the above example any queued up handlers would be processed early when the 'meta'
statement was reached. This is a bit of a niche case but can come in handy from
time to time.
2012-03-13 04:12:21 +01:00
2013-02-23 20:56:25 +01:00
Task Include Files And Encouraging Reuse
2013-02-23 22:05:05 +01:00
`` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ``
2012-03-13 04:12:21 +01:00
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...
2012-03-13 04:12:21 +01:00
2012-08-01 04:19:04 +02:00
A task include file simply contains a flat list of tasks, like so::
2012-03-13 04:12:21 +01:00
---
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
2013-07-15 19:50:48 +02:00
command: /bin/foo
2012-03-13 04:12:21 +01:00
- name: placeholder bar
2013-07-15 19:50:48 +02:00
command: /bin/bar
2012-03-13 04:12:21 +01:00
2012-08-01 04:19:04 +02:00
Include directives look like this, and can be mixed in with regular tasks in a playbook::
2012-03-14 01:32:55 +01:00
2012-08-02 04:50:03 +02:00
tasks:
- include: tasks/foo.yml
2012-03-14 01:32:55 +01:00
2012-08-01 04:19:04 +02:00
You can also pass variables into includes. We call this a 'parameterized include'.
2012-03-14 01:32:55 +01:00
2012-03-09 17:44:14 +01:00
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::
2012-03-09 04:50:00 +01:00
2012-08-02 04:50:03 +02:00
tasks:
2012-09-07 15:16:08 +02:00
- include: wordpress.yml user=timmy
2012-03-13 04:12:21 +01:00
- include: wordpress.yml user=alice
- include: wordpress.yml user=bob
2012-03-08 19:36:47 +01:00
2012-08-01 04:19:04 +02:00
Variables passed in can then be used in the included files. You can reference them like this::
2012-09-07 15:16:08 +02:00
2013-04-13 00:21:09 +02:00
{{ user }}
2012-05-02 06:34:37 +02:00
2013-08-11 06:54:05 +02:00
(In addition to the explicitly passed-in parameters, all variables from
2012-08-01 04:19:04 +02:00
the vars section are also available for use here as well.)
2012-03-08 19:36:47 +01:00
2013-02-23 20:34:29 +01:00
Starting in 1.0, variables can also be passed to include files using an alternative syntax,
which also supports structured variables::
tasks:
- include: wordpress.yml
vars:
user: timmy
2013-05-02 21:14:53 +02:00
some_list_variable:
2013-02-23 20:34:29 +01:00
- alpha
- beta
- gamma
2013-02-23 20:56:25 +01:00
Playbooks can include other playbooks too, but that's mentioned in a later section.
2012-03-13 04:12:21 +01:00
.. note ::
2013-01-09 13:41:14 +01:00
As of 1.0, task include statements can be used at arbitrary depth.
They were previously limited to a single level, so task includes
could not include other files containing task includes.
2012-03-09 13:42:53 +01:00
2012-03-13 04:12:21 +01:00
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 07:35:02 +02:00
of your playbooks. You might make a handlers.yml that looks like::
2012-03-13 04:12:21 +01:00
2012-10-18 14:35:35 +02:00
---
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
2013-07-15 19:50:48 +02:00
service: name=apache state=restarted
2012-03-13 04:12:21 +01:00
And in your main playbook file, just include it like so, at the bottom
of a play::
handlers:
2012-03-14 01:32:55 +01:00
- include: handlers/handlers.yml
2012-03-13 04:12:21 +01:00
You can mix in includes along with your regular non-included tasks and handlers.
2012-03-09 13:42:53 +01:00
2012-09-07 15:16:08 +02:00
Includes can also be used to import one playbook file into another. This allows
you to define a top-level playbook that is composed of other playbooks.
For example::
- name: this is a play at the top level of a file
hosts: all
user: root
tasks:
- name: say hi
tags: foo
2013-07-15 19:50:48 +02:00
shell: echo "hi..."
2012-09-07 15:16:08 +02:00
- include: load_balancers.yml
- include: webservers.yml
- include: dbservers.yml
Note that you cannot do variable substitution when including one playbook
inside another.
2012-08-07 04:43:35 +02:00
.. note ::
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
2013-05-02 15:22:08 +02:00
play are going to get the same tasks. ('*when* ' provides some
2012-08-07 04:43:35 +02:00
ability for hosts to conditionally skip tasks).
2012-03-09 13:42:53 +01:00
2013-06-14 17:13:38 +02:00
.. _roles:
2013-04-12 23:41:16 +02:00
Roles
`` ` ``
2013-05-11 22:40:09 +02:00
.. versionadded: 1.2
2013-04-12 23:41:16 +02:00
Now that you have learned about vars_files, tasks, and handlers, what is the best way to organize your playbooks?
2013-05-30 00:14:20 +02:00
The short answer is to use roles! Roles are ways of automatically loading certain vars_files, tasks, and
2013-04-12 23:41:16 +02:00
handlers based on a known file structure. Grouping content by roles also allows easy sharing of roles with other users.
Roles are just automation around 'include' directives as redescribed above, and really don't contain much
additional magic beyond some improvements to search path handling for referenced files. However, that can be a big thing!
Example project structure::
site.yml
webservers.yml
fooservers.yml
roles/
common/
files/
templates/
tasks/
handlers/
vars/
webservers/
files/
templates/
tasks/
handlers/
vars/
In a playbook, it would look like this::
---
- hosts: webservers
roles:
- common
- webservers
This designates the following behaviors, for each role 'x':
- If roles/x/tasks/main.yml exists, tasks listed therein will be added to the play
- If roles/x/handlers/main.yml exists, handlers listed therein will be added to the play
- If roles/x/vars/main.yml exists, variables listed therein will be added to the play
- Any copy tasks can reference files in roles/x/files/ without having to path them relatively or absolutely
2013-05-25 16:51:59 +02:00
- Any script tasks can reference scripts in roles/x/files/ without having to path them relatively or absolutely
2013-04-12 23:41:16 +02:00
- Any template tasks can reference files in roles/x/templates/ without having to path them relatively or absolutely
If any files are not present, they are just ignored. So it's ok to not have a 'vars/' subdirectory for the role,
for instance.
Note, you are still allowed to list tasks, vars_files, and handlers "loose" in playbooks without using roles,
but roles are a good organizational feature and are highly recommended. if there are loose things in the playbook,
the roles are evaluated first.
Also, should you wish to parameterize roles, by adding variables, you can do so, like this::
---
- hosts: webservers
roles:
- common
- { role: foo_app_instance, dir: '/opt/a', port: 5000 }
- { role: foo_app_instance, dir: '/opt/b', port: 5001 }
2013-04-13 02:46:12 +02:00
While it's probably not something you should do often, you can also conditionally apply roles like so::
---
- hosts: webservers
roles:
- { role: some_role, when: "ansible_os_family == 'RedHat'" }
2013-05-02 21:14:53 +02:00
2013-04-13 02:46:12 +02:00
This works by applying the conditional to every task in the role. Conditionals are covered later on in
2013-05-02 21:14:53 +02:00
the documentation.
2013-04-20 22:21:52 +02:00
2013-06-28 15:28:41 +02:00
Finally, you may wish to assign tags to the roles you specify. You can do so inline:::
---
- hosts: webservers
roles:
- { role: foo, tags: ["bar", "baz"] }
2013-04-20 22:21:52 +02:00
If the play still has a 'tasks' section, those tasks are executed after roles are applied.
2013-05-02 21:14:53 +02:00
If you want to define certain tasks to happen before AND after roles are applied, you can do this::
2013-04-20 22:21:52 +02:00
---
- hosts: webservers
pre_tasks:
- shell: echo 'hello'
roles:
- { role: some_role }
2013-05-24 22:42:24 +02:00
tasks:
- shell: echo 'still busy'
2013-04-20 22:21:52 +02:00
post_tasks:
- shell: echo 'goodbye'
2013-05-02 21:14:53 +02:00
2013-07-19 16:27:37 +02:00
.. note ::
2013-07-21 17:18:07 +02:00
If using tags with tasks (described later as a means of only running part of a playbook),
be sure to also tag your pre_tasks and post_tasks and pass those along as well, especially if the pre
and post tasks are used for monitoring outage window control or load balancing.
2013-07-19 16:27:37 +02:00
2012-03-08 19:36:47 +01:00
Executing A Playbook
`` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ``
2012-03-13 04:12:21 +01:00
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::
ansible-playbook playbook.yml -f 10
2012-03-08 19:36:47 +01:00
2012-07-04 23:44:39 +02:00
Tips and Tricks
`` ` ` ` ` ` ` ` ` ` ` ` ``
2013-08-11 06:54:05 +02:00
Look at the bottom of the playbook execution for a summary of the nodes that were targeted
2012-09-07 15:16:08 +02:00
and how they performed. General failures and fatal "unreachable" communication attempts are
2013-03-06 19:41:19 +01:00
kept separate in the counts.
2012-07-04 23:44:39 +02:00
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.
2012-09-07 15:16:08 +02:00
Also, in version 0.5 and later, Ansible playbook output is vastly upgraded if the cowsay
2012-07-04 23:44:39 +02:00
package is installed. Try it!
2012-08-17 04:08:17 +02:00
In version 0.7 and later, to see what hosts would be affected by a playbook before you run it, you
can do this::
ansible-playbook playbook.yml --list-hosts.
2012-03-31 15:29:31 +02:00
.. seealso ::
2012-03-14 01:32:55 +01:00
2012-03-31 15:29:31 +02:00
:doc: `YAMLSyntax`
Learn about YAML syntax
2012-05-13 17:56:09 +02:00
:doc: `playbooks`
Review the basic Playbook language features
2012-05-13 17:00:02 +02:00
:doc: `playbooks2`
Learn about Advanced Playbook Features
2012-05-13 17:56:09 +02:00
:doc: `bestpractices`
Various tips about managing playbooks in the real world
2012-03-31 15:29:31 +02:00
:doc: `modules`
Learn about available modules
:doc: `moduledev`
Learn how to extend Ansible by writing your own modules
:doc: `patterns`
Learn about how to select hosts
2012-08-07 04:00:50 +02:00
`Github examples directory <https://github.com/ansible/ansible/tree/devel/examples/playbooks> `_
2012-03-31 15:29:31 +02:00
Complete playbook files from the github project source
2012-03-20 04:43:03 +01:00
`Mailing List <http://groups.google.com/group/ansible-project> `_
Questions? Help? Ideas? Stop by the list on Google Groups
2012-03-08 19:36:47 +01:00