ansible/rst/playbooks.rst

336 lines
12 KiB
ReStructuredText
Raw Normal View History

Playbooks
=========
2012-03-07 16:35:18 +00:00
Playbooks are a completely different way to use ansible and are
particularly awesome. They are the basis for a really simple
configuration management and multi-machine deployment system,
unlike any that already exist, and
one that is very well suited to deploying complex applications.
2012-03-09 03:50:00 +00: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.
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 18:36:47 +00:00
Let's dive in and see how they work. As you go, you may wish to open
the `github examples directory <https://github.com/ansible/ansible/tree/master/examples/playbooks>`_ in
another tab, so you can apply the theory to what things look like in practice.
2012-03-08 18:36:47 +00:00
Playbook Example
````````````````
Playbooks are expressed in YAML format and have a minimum of syntax.
Each playbook is composed of one or more 'plays' in a list.
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-04-12 22:20:52 +00:00
For starters, here's a playbook that contains just one play::
2012-03-08 18:36:47 +00:00
---
2012-03-08 18:53:48 +00:00
- hosts: webservers
2012-03-08 18:36:47 +00:00
vars:
http_port: 80
max_clients: 200
user: root
tasks:
2012-03-12 23:23:58 +00:00
- name: ensure apache is at the latest version
action: yum pkg=httpd state=latest
2012-03-08 18:36:47 +00: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:
- name: restart apache
action: service name=apache state=restarted
Below, we'll break down what the various features of the playbook language are.
Basics
``````
2012-03-21 00:15:53 +00: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 18:36:47 +00:00
The `hosts` line is a list of one or more groups or host patterns,
2012-03-09 19:39:29 +00:00
separated by colons, as described in the :ref:`patterns`
2012-03-21 00:15:53 +00:00
documentation. The `user` is just the name of the user account::
2012-03-21 00:15:53 +00:00
---
- hosts: webservers
user: root
Support for running things from sudo is also available::
---
- hosts: webservers
user: yourname
sudo: True
If you need to specify a password to sudo, run `ansible-playbook` with ``--ask-sudo-pass`` (`-K`).
2012-04-14 12:45:27 +00: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 18:36:47 +00:00
Vars section
++++++++++++
2012-03-08 18:36:47 +00:00
2012-03-21 01:04:47 +00:00
The `vars` section contains a list of variables and values that can be used in the plays, like this::
2012-03-21 00:15:53 +00:00
---
- hosts: webservers
users: root
vars:
http_port: 80
van_halen_port: 5150
other: 'magic'
2012-05-13 15:00:02 +00:00
These variables can be used later in the playbook like this::
2012-03-08 18:36:47 +00:00
$varname or ${varname}
The later is useful in the event you need to do something like ${other}_concatenated_value.
2012-03-08 18:36:47 +00:00
2012-07-04 21:44:39 +00:00
The full power of the Jinja2 templating language is also available (note: in 0.4, this is only true inside of templates), which looks like this::
2012-03-21 00:15:53 +00:00
2012-05-13 15:00:02 +00:00
{{ varname }}
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.
2012-03-21 00:15:53 +00:00
2012-05-13 15:00:02 +00:00
If there are discovered variables about the system (ansible provides some of these,
2012-04-19 03:02:28 +00:00
plus we include ones taken from facter or ohai if installed) these variables bubble up back into the
playbook, and can be used on each system just like explicitly set
2012-03-21 00:15:53 +00:00
variables.
Facter variables are prefixed with ``facter_`` and Ohai
2012-04-19 03:02:28 +00:00
variables are prefixed with ``ohai_``. Ansible variables (0.3 and later)
are not surprisingly prefixed with ``ansible_``. So for instance, if I wanted
to write the hostname into the /etc/motd file, I could say::
2012-03-09 03:50:00 +00:00
- name: write the motd
action: template src=/srv/templates/motd.j2 dest=/etc/motd
2012-03-09 03:50:00 +00:00
2012-03-09 04:05:52 +00:00
And in /srv/templates/motd.j2::
2012-03-09 03:50:00 +00:00
You are logged into {{ facter_hostname }}
But we're getting ahead of ourselves. Let's talk about tasks.
2012-03-08 18:36:47 +00:00
Tasks list
++++++++++
2012-03-08 18:36:47 +00: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.
2012-03-09 03:50:00 +00:00
Hosts with failed tasks are taken out of the rotation for the entire
playbook. If things fail, simply correct the playbook file and rerun.
2012-03-09 03:50:00 +00:00
2012-03-21 00:15:53 +00: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.
Modules other than `command` and `shell` are 'idempotent', meaning if you run them
again, they will make the changes they are told to make 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
2012-03-21 00:15:53 +00:00
unless they have to change things.
2012-03-08 18:36:47 +00:00
The `command` and `shell` modules will actually rerun the same command again,
2012-03-21 00:15:53 +00:00
which is totally ok if the command is something like
'chmod' or 'setsebool', etc.
2012-03-08 18:36:47 +00:00
Every task must have a name, which is included in the output from
2012-03-21 00:15:53 +00:00
running the playbook. This is output for humans, so it is
nice to have reasonably good descriptions of each task step.
2012-03-08 18:36:47 +00:00
2012-03-21 00:15:53 +00:00
Here is what a basic task looks like, as with most modules,
the service module takes key=value arguments::
2012-03-21 00:15:53 +00:00
tasks:
- name: make sure apache is running
action: service name=httpd state=running
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 18:36:47 +00:00
2012-03-21 00:15:53 +00:00
tasks:
- name: disable selinux
action: command /sbin/setenforce 0
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 03:50:00 +00:00
2012-03-21 00:15:53 +00:00
tasks:
- name: create a virtual host file for $vhost
2012-03-21 00:15:53 +00:00
action: template src=somefile.j2 dest=/etc/httpd/conf.d/$vhost
Those same variables are usable in templates, which we'll get to later.
2012-03-09 03:50:00 +00:00
2012-03-21 00:15:53 +00:00
Running Operations On Change
````````````````````````````
2012-03-08 18:36:47 +00:00
2012-03-21 00:15:53 +00:00
As we've mentioned, nearly all 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 '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.
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
action: template src=template.j2 dest=/etc/foo.conf
notify:
- restart memcached
- restart apache
2012-03-21 00:15:53 +00:00
The things listed in the 'notify' section of a task are called
handlers.
2012-03-08 18:36:47 +00: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
of the tasks complete in a particular play.
2012-03-08 18:36:47 +00:00
Here's an example handlers section::
handlers:
- name: restart memcached
action: service name=memcached state=restarted
- name: restart apache
action: service name=apache state=restarted
Handlers are best used to restart services and trigger reboots. You probably
won't need them for much else.
2012-03-21 00:15:53 +00:00
.. note::
Notify handlers are always run in the order written.
Include Files And Reuse
```````````````````````
Suppose you want to reuse lists of tasks between plays or playbooks. You can use
include files to do this.
An include file simply contains a flat list of tasks, like so::
---
# possibly saved as tasks/foo.yml
- name: placeholder foo
action: command /bin/foo
- name: placeholder bar
action: command /bin/bar
Include directives look like this::
- tasks:
- include: tasks/foo.yml
2012-05-02 04:34:37 +00:00
You can also pass variables into includes directly. We might call this a 'parameterized include'.
For instance, if deploying multiple wordpress instances, I could
contain all of my wordpress tasks in a single wordpress.yml file, and use it like so::
2012-03-09 03:50:00 +00:00
- tasks:
- include: wordpress.yml user=timmy
- include: wordpress.yml user=alice
- include: wordpress.yml user=bob
2012-03-08 18:36:47 +00:00
2012-05-13 15:00:02 +00:00
Variables passed in can be used in the included files. You can reference them like this::
2012-05-02 04:34:37 +00:00
$user
In addition to the explicitly passed in parameters, all variables from
2012-05-02 04:34:37 +00:00
the vars section are also available for use here as well.
2012-03-08 18:36:47 +00:00
.. note::
Include statements are only usable from the top level
playbook file. This means includes can not include other
2012-05-02 04:34:37 +00:00
includes. This may be implemented in a later release.
2012-03-09 12:42:53 +00: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
of your playbooks. You might make a handlers.yml that looks like::
----
# this might be in a file like handlers/handlers.yml
- name: restart apache
action: service name=apache state=restarted
And in your main playbook file, just include it like so, at the bottom
of a play::
handlers:
- include: handlers/handlers.yml
You can mix in includes along with your regular non-included tasks and handlers.
2012-03-09 12:42:53 +00:00
2012-03-20 03:43:03 +00:00
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.
2012-03-09 12:42:53 +00:00
2012-03-08 18:36:47 +00:00
Executing A Playbook
````````````````````
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 18:36:47 +00:00
2012-07-04 21:44:39 +00:00
Tips and Tricks
```````````````
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.
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.
Also, in version 0.5 and later, Ansible playbook output is vastly upgraded if the cowsay
package is installed. Try it!
.. seealso::
:doc:`YAMLSyntax`
Learn about YAML syntax
:doc:`playbooks`
Review the basic Playbook language features
2012-05-13 15:00:02 +00:00
:doc:`playbooks2`
Learn about Advanced Playbook Features
:doc:`bestpractices`
Various tips about managing playbooks in the real world
: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
`Github examples directory <https://github.com/ansible/ansible/tree/master/examples/playbooks>`_
Complete playbook files from the github project source
2012-03-20 03:43:03 +00:00
`Mailing List <http://groups.google.com/group/ansible-project>`_
Questions? Help? Ideas? Stop by the list on Google Groups
2012-03-08 18:36:47 +00:00