2013-09-30 01:10:28 +02:00
Intro to Playbooks
==================
2013-02-23 20:34:29 +01:00
2013-09-29 22:11:36 +02:00
.. contents ::
:depth: 2
2013-02-23 20:56:25 +01:00
2013-10-05 00:41:14 +02:00
.. _about_playbooks:
2013-09-30 01:10:28 +02:00
About Playbooks
`` ` ` ` ` ` ` ` ` ` ` ` ``
2012-03-09 13:42:53 +01:00
2013-10-03 03:56:55 +02:00
Playbooks are a completely different way to use ansible than in adhoc task execution mode, and are
particularly powerful.
Simply put, playbooks are the basis for a really simple configuration management and multi-machine deployment system,
2013-09-29 22:11:36 +02:00
unlike any that already exist, and one that is very well suited to deploying complex applications.
2012-03-13 04:12:21 +01:00
2013-09-29 22:11:36 +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
between sets of machines in particular orders. They can launch tasks
synchronously or asynchronously.
2012-03-13 04:12:21 +01:00
2013-09-29 22:11:36 +02: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-13 04:12:21 +01:00
2013-09-29 22:11:36 +02:00
There are also some full sets of playbooks illustrating a lot of these techniques in the
2013-10-05 18:40:53 +02:00
`ansible-examples repository <https://github.com/ansible/ansible-examples> `_ . We'd recommend
looking at these in another tab as you go along.
2012-03-09 13:42:53 +01:00
2013-09-29 22:11:36 +02:00
There are also many jumping off points after you learn playbooks, so hop back to the documentation
index after you're done with this section.
2012-09-07 15:16:08 +02:00
2013-10-05 00:41:14 +02:00
.. _playbook_language_example:
2013-09-29 22:11:36 +02:00
Playbook Language Example
`` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ``
2012-09-07 15:16:08 +02:00
2013-10-03 03:56:55 +02:00
Playbooks are expressed in YAML format (see :doc: `YAMLSyntax` ) and have a minimum of syntax, which intentionally
tries to not be a programming language or script, but rather a model of a configuration or a process.
2013-09-29 22:11:36 +02:00
Each playbook is composed of one or more 'plays' in a list.
2012-09-07 15:16:08 +02:00
2013-09-29 22:11:36 +02:00
The goal of a play is to map a group of hosts to some well defined roles, represented by
things ansible calls tasks. At a basic level, a task is nothing more than a call
to an ansible module, which you should have learned about in earlier chapters.
2012-09-07 15:16:08 +02:00
2013-09-29 22:11:36 +02:00
By composing a playbook of multiple 'plays', it is possible to
orchestrate multi-machine deployments, running certain steps on all
machines in the webservers group, then certain steps on the database
server group, then more commands back on the webservers group, etc.
2012-09-07 15:16:08 +02:00
2013-10-03 03:56:55 +02:00
"plays" are more or less a sports analogy. You can have quite a lot of plays that affect your systems
to do different things. It's not as if you were just defining one particular state or model, and you
can run different plays at different times.
2013-09-29 22:11:36 +02:00
For starters, here's a playbook that contains just one play::
2013-04-12 23:41:16 +02:00
---
- hosts: webservers
2013-09-29 22:11:36 +02:00
vars:
http_port: 80
max_clients: 200
remote_user: root
tasks:
- name: ensure apache is at the latest version
yum: pkg=httpd state=latest
- name: write the apache config file
template: src=/srv/httpd.j2 dest=/etc/httpd.conf
notify:
- restart apache
- name: ensure apache is running
service: name=httpd state=started
handlers:
- name: restart apache
service: name=httpd state=restarted
2013-04-12 23:41:16 +02:00
2013-09-29 22:11:36 +02:00
Below, we'll break down what the various features of the playbook language are.
2013-04-12 23:41:16 +02:00
2013-10-05 00:41:14 +02:00
.. _playbook_basics:
2013-09-29 22:11:36 +02:00
Basics
`` ` ` ``
2013-04-12 23:41:16 +02:00
2013-10-05 00:41:14 +02:00
.. _playbook_hosts_and_users:
2013-09-29 22:11:36 +02:00
Hosts and Users
+++++++++++++++
2013-08-16 17:27:14 +02:00
2013-09-29 22:11:36 +02:00
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.
2013-04-12 23:41:16 +02:00
2013-09-29 22:11:36 +02:00
The `hosts` line is a list of one or more groups or host patterns,
2013-10-05 19:58:03 +02:00
separated by colons, as described in the :doc: `intro_patterns`
2013-09-29 22:11:36 +02:00
documentation. The `remote_user` is just the name of the user account::
2013-04-12 23:41:16 +02:00
2013-09-29 22:11:36 +02:00
---
- hosts: webservers
remote_user: root
.. Note ::
The `remote_user` parameter was formerly called just `user` . It was renamed in Ansible 1.4 to make it more distinguishable from the `user` module (used to create users on remote systems).
Support for running things from sudo is also available::
2013-04-12 23:41:16 +02:00
---
- hosts: webservers
2013-09-29 22:11:36 +02:00
remote_user: yourname
sudo: yes
2013-04-12 23:41:16 +02:00
2013-09-29 22:11:36 +02:00
You can also use sudo on a particular task instead of the whole play::
2013-04-13 02:46:12 +02:00
---
- hosts: webservers
2013-09-29 22:11:36 +02:00
remote_user: yourname
tasks:
- service: name=nginx state=started
sudo: yes
2013-05-02 21:14:53 +02:00
2013-04-20 22:21:52 +02:00
2013-09-29 22:11:36 +02:00
You can also login as you, and then sudo to different users than root::
2013-06-28 15:28:41 +02:00
---
- hosts: webservers
2013-09-29 22:11:36 +02:00
remote_user: yourname
sudo: yes
sudo_user: postgres
2013-06-28 15:28:41 +02:00
2013-09-29 22:11:36 +02:00
If you need to specify a password to sudo, run `ansible-playbook` with `` --ask-sudo-pass `` (`-K` ).
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` .
2013-06-28 15:28:41 +02:00
2013-09-29 22:11:36 +02:00
.. important ::
2013-04-20 22:21:52 +02:00
2013-09-29 22:11:36 +02:00
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.
2013-04-20 22:21:52 +02:00
2013-10-05 00:41:14 +02:00
.. _tasks_list:
2013-09-29 22:11:36 +02:00
Tasks list
++++++++++
2013-05-02 21:14:53 +02:00
2013-09-29 22:11:36 +02:00
Each play contains a list of tasks. Tasks are executed in order, one
at a time, against all machines matched by the host pattern,
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.
2013-07-19 16:27:37 +02:00
2013-09-29 22:11:36 +02:00
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.
2013-08-30 16:20:46 +02:00
2013-09-29 22:11:36 +02: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.
2013-08-30 16:20:46 +02:00
2013-09-29 22:11:36 +02:00
Modules are 'idempotent', meaning if you run them
again, they will make only the changes they must in order to bring the
system to the desired state. This makes it very safe to rerun
the same playbook multiple times. They won't change things
unless they have to change things.
2013-08-30 16:20:46 +02:00
2013-09-29 22:11:36 +02:00
The `command` and `shell` modules will typically rerun the same command again,
which is totally ok if the command is something like
'chmod' or 'setsebool', etc. Though there is a 'creates' flag available which can
be used to make these modules also idempotent.
2013-08-16 17:27:14 +02:00
2013-09-29 22:11:36 +02:00
Every task should have a `name` , which is included in the output from
running the playbook. This is output for humans, so it is
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.
2013-08-16 17:27:14 +02:00
2013-09-29 22:11:36 +02:00
Tasks can be declared using the legacy "action: module options" format, but
2013-10-06 03:19:19 +02:00
it is recommended that you use the more conventional "module: options" format.
2013-09-29 22:11:36 +02:00
This recommended format is used throughout the documentation, but you may
encounter the older format in some playbooks.
2013-08-16 17:27:14 +02:00
2013-09-29 22:11:36 +02:00
Here is what a basic task looks like, as with most modules,
the service module takes key=value arguments::
2013-08-16 17:27:14 +02:00
2013-09-29 22:11:36 +02:00
tasks:
- name: make sure apache is running
service: name=httpd state=running
2013-08-16 17:27:14 +02:00
2013-09-29 22:11:36 +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::
2013-08-16 17:27:14 +02:00
2013-09-29 22:11:36 +02:00
tasks:
- name: disable selinux
command: /sbin/setenforce 0
2013-08-16 17:27:14 +02:00
2013-09-29 22:11:36 +02:00
The command and shell module care about return codes, so if you have a command
whose successful exit code is not zero, you may wish to do this::
2013-08-16 17:27:14 +02:00
2013-09-29 22:11:36 +02:00
tasks:
- name: run this command and ignore the result
shell: /usr/bin/somecommand || /bin/true
2013-08-30 18:35:35 +02:00
2013-09-29 22:11:36 +02:00
Or this::
tasks:
- name: run this command and ignore the result
shell: /usr/bin/somecommand
ignore_errors: True
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
copy: src=/etc/ansible/hosts dest=/etc/ansible/hosts
owner=root group=root mode=0644
Variables can be used in action lines. Suppose you defined
a variable called 'vhost' in the 'vars' section, you could do this::
tasks:
- name: create a virtual host file for {{ vhost }}
template: src=somefile.j2 dest=/etc/httpd/conf.d/{{ vhost }}
Those same variables are usable in templates, which we'll get to later.
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.
2013-10-05 00:41:14 +02:00
.. _action_shorthand:
2013-09-29 22:11:36 +02:00
Action Shorthand
`` ` ` ` ` ` ` ` ` ` ` ` ` ``
.. versionadded :: 0.8
Ansible prefers listing modules like this in 0.8 and later::
template: src=templates/foo.j2 dest=/etc/foo.conf
You will notice in earlier versions, this was only available as::
action: template src=templates/foo.j2 dest=/etc/foo.conf
The old form continues to work in newer versions without any plan of deprecation.
2013-10-05 00:41:14 +02:00
.. _handlers:
2013-09-29 22:11:36 +02:00
Handlers: Running Operations On Change
`` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ``
As we've mentioned, modules are written to be 'idempotent' and can relay when
they have made a change on the remote system. Playbooks recognize this and
have a basic event system that can be used to respond to change.
These 'notify' actions are triggered at the end of each block of tasks in a playbook, and will only be
triggered once even if notified by multiple different tasks.
For instance, multiple resources may indicate
that apache needs to be restarted because they have changed a config file,
2013-10-06 03:19:19 +02:00
but apache will only be bounced once to avoid unnecessary restarts.
2013-09-29 22:11:36 +02: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
template: src=template.j2 dest=/etc/foo.conf
notify:
- restart memcached
- restart apache
The things listed in the 'notify' section of a task are called
handlers.
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.
Here's an example handlers section::
handlers:
- name: restart memcached
service: name=memcached state=restarted
- name: restart apache
service: name=apache state=restarted
Handlers are best used to restart services and trigger reboots. You probably
won't need them for much else.
2013-08-16 17:27:14 +02:00
.. note ::
2013-09-29 22:11:36 +02:00
Notify handlers are always run in the order written.
Roles are described later on. It's worthwhile to point out that handlers are
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.
2013-08-16 17:27:14 +02:00
2013-10-05 00:41:14 +02:00
.. _executing_a_playbook:
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
2013-10-05 00:41:14 +02:00
.. _tips_and_tricks:
2013-10-05 20:17:07 +02:00
Ansible-Pull
`` ` ` ` ` ` ` ` ` ``
Should you want to invert the architecture of Ansible, so that nodes check in to a central location, instead
2013-10-06 03:19:19 +02:00
of pushing configuration out to them, you can.
2013-10-05 20:17:07 +02:00
Ansible-pull is a small script that will checkout a repo of configuration instructions from git, and then
run ansible-playbook against that content.
Assuming you load balance your checkout location, ansible-pull scales essentially infinitely.
Run 'ansible-pull --help' for details.
2013-10-05 20:57:45 +02:00
There's also a `clever playbook <https://github.com/ansible/ansible-examples/blob/master/language_features/ansible_pull.yml> `_ available to using ansible in push mode to configure ansible-pull via a crontab!
2013-10-05 20:17:07 +02: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.
2013-09-29 22:11:36 +02:00
Ansible playbook output is vastly upgraded if the cowsay
2012-07-04 23:44:39 +02:00
package is installed. Try it!
2013-09-29 22:11:36 +02:00
To see what hosts would be affected by a playbook before you run it, you
2012-08-17 04:08:17 +02:00
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
2013-09-30 01:30:10 +02:00
:doc: `playbooks_best_practices`
2012-05-13 17:56:09 +02:00
Various tips about managing playbooks in the real world
2013-10-05 18:31:16 +02:00
:doc: `index`
Hop back to the documentation index for a lot of special topics about playbooks
2012-03-31 15:29:31 +02:00
:doc: `modules`
Learn about available modules
2013-09-30 01:16:59 +02:00
:doc: `developing_modules`
2012-03-31 15:29:31 +02:00
Learn how to extend Ansible by writing your own modules
2013-09-30 01:16:59 +02:00
:doc: `intro_patterns`
2012-03-31 15:29:31 +02:00
Learn about how to select hosts
2013-10-05 18:31:16 +02:00
`Github examples directory <https://github.com/ansible/ansible-examples> `_
Complete end-to-end playbook examples
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