2012-03-09 17:44:14 +01:00
|
|
|
Playbooks
|
|
|
|
=========
|
2012-03-07 17:35:18 +01:00
|
|
|
|
2012-03-09 17:44:14 +01:00
|
|
|
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.
|
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
|
|
|
|
between sets of machines in particular orders. They can launch tasks
|
|
|
|
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-03-31 16:17:35 +02: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 19:36:47 +01:00
|
|
|
|
|
|
|
Playbook Example
|
|
|
|
````````````````
|
|
|
|
|
2012-03-09 17:44:14 +01:00
|
|
|
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.
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
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
|
|
|
|
action: yum pkg=httpd state=latest
|
2012-03-08 19:36:47 +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
|
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-03-31 04:10:32 +02:00
|
|
|
---
|
|
|
|
- hosts: webservers
|
|
|
|
user: yourname
|
|
|
|
sudo: True
|
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
|
|
|
|
|
|
|
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
|
|
|
|
users: root
|
|
|
|
vars:
|
|
|
|
http_port: 80
|
|
|
|
van_halen_port: 5150
|
|
|
|
other: 'magic'
|
|
|
|
|
|
|
|
These variables can be used later in the playbook, or on the managed system (in templates), just like this::
|
2012-03-08 19:36:47 +01:00
|
|
|
|
2012-03-13 04:12:21 +01:00
|
|
|
{{ varname }}
|
2012-03-08 19:36:47 +01:00
|
|
|
|
2012-03-21 01:15:53 +01:00
|
|
|
Within playbooks themselves, but not within templates on the remote machines, it's also legal
|
|
|
|
to use nicer shorthand like this::
|
|
|
|
|
|
|
|
$varname
|
|
|
|
|
2012-04-19 05:02:28 +02:00
|
|
|
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.
|
|
|
|
|
|
|
|
Facter variables are prefixed with ``facter_`` and Ohai
|
2012-04-19 05:02:28 +02:00
|
|
|
variables are prefixed with ``ohai_``. Ansible variables (0.3 and later)
|
|
|
|
are not surprisingly prefixed with ``ansible_``. 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::
|
2012-03-09 04:50:00 +01:00
|
|
|
|
|
|
|
- name: write the motd
|
2012-03-13 04:12:21 +01:00
|
|
|
action: 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 }}
|
|
|
|
|
|
|
|
But we're getting ahead of ourselves. 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-03-09 17:44:14 +01:00
|
|
|
before moving on to the next task.
|
2012-03-09 04:50:00 +01:00
|
|
|
|
2012-03-09 17:44:14 +01: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 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-05-02 07:35:02 +02:00
|
|
|
Modules other than `command` and `shell` 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.
|
2012-03-08 19:36:47 +01:00
|
|
|
|
2012-05-02 07:35:02 +02:00
|
|
|
The `command` and `shell` modules will actually rerun the same command again,
|
2012-03-21 01:15:53 +01:00
|
|
|
which is totally ok if the command is something like
|
|
|
|
'chmod' or 'setsebool', etc.
|
2012-03-08 19:36:47 +01:00
|
|
|
|
2012-03-09 17:44:14 +01:00
|
|
|
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.
|
2012-03-08 19:36:47 +01:00
|
|
|
|
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
|
|
|
|
action: 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:
|
|
|
|
- name: disable selinux
|
|
|
|
action: command /sbin/setenforce 0
|
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:
|
2012-05-04 01:54:41 +02:00
|
|
|
- name: create a virtual host file for $vhost
|
2012-03-21 01:15:53 +01:00
|
|
|
action: template src=somefile.j2 dest=/etc/httpd/conf.d/$vhost
|
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-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-03-21 01:15:53 +01:00
|
|
|
As we've mentioned, nearly all 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.
|
|
|
|
|
|
|
|
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
|
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
|
|
|
|
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-03-13 04:12:21 +01: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
|
|
|
|
action: service name=memcached state=restarted
|
2012-03-13 04:17:59 +01:00
|
|
|
- name: restart apache
|
|
|
|
action: 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.
|
|
|
|
|
2012-03-13 04:12:21 +01:00
|
|
|
|
|
|
|
Power Tricks
|
|
|
|
````````````
|
|
|
|
|
|
|
|
Now that you have the basics down, let's learn some more advanced
|
|
|
|
things you can do with playbooks.
|
|
|
|
|
2012-04-13 04:44:04 +02:00
|
|
|
Local Playbooks
|
|
|
|
+++++++++++++++
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
To run an entire playbook locally, just set the "hosts:" line to "hosts:127.0.0.1" and then run the playbook like so::
|
|
|
|
|
2012-05-10 06:58:27 +02:00
|
|
|
ansible-playbook playbook.yml --connection=local
|
2012-04-13 04:44:04 +02:00
|
|
|
|
|
|
|
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::
|
|
|
|
|
|
|
|
hosts: 127.0.0.1
|
|
|
|
connection: local
|
|
|
|
|
2012-04-26 04:32:24 +02:00
|
|
|
Variables From Other Hosts
|
|
|
|
++++++++++++++++++++++++++
|
|
|
|
|
|
|
|
If your database server wants to check the value of a 'fact' from another node, it's easy to do so
|
|
|
|
within a template or even an action line::
|
|
|
|
|
|
|
|
{{ hostvars.get('name_of_host').get('name_of_fact') }}
|
|
|
|
|
|
|
|
NOTE: No database or other complex system is required to exchange data between hosts. The hosts that you
|
|
|
|
want to reference data from must be included in either the current play or any previous play.
|
2012-03-13 04:12:21 +01:00
|
|
|
|
2012-05-02 07:35:02 +02:00
|
|
|
External Variables and Prompted or Sensitive Data
|
2012-04-03 03:07:33 +02:00
|
|
|
+++++++++++++++++++++++++++++++++++++++++++++++++
|
2012-03-13 04:12:21 +01:00
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
You can do this by using an external variables file, or files, just like this::
|
2012-03-08 19:36:47 +01:00
|
|
|
|
2012-03-13 04:12:21 +01:00
|
|
|
---
|
|
|
|
- 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
|
|
|
|
|
|
|
|
This removes the risk of sharing sensitive data with others when
|
|
|
|
sharing your playbook source with them.
|
|
|
|
|
|
|
|
The contents of each variables file is a simple YAML dictionary, like this::
|
|
|
|
|
|
|
|
---
|
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
|
|
|
|
|
2012-04-03 03:03:46 +02:00
|
|
|
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::
|
2012-04-03 03:03:46 +02:00
|
|
|
|
|
|
|
---
|
|
|
|
- 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?"
|
|
|
|
|
|
|
|
There are full examples of both of these items in the github examples/playbooks directory.
|
|
|
|
|
2012-03-21 01:15:53 +01:00
|
|
|
Conditional Execution
|
|
|
|
+++++++++++++++++++++
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
This is easy to do in Ansible, with the `only_if` clause. This clause can be applied to any task,
|
|
|
|
and allows usage of variables from anywhere in ansible, either denoted with `$dollar_sign_syntax` or
|
|
|
|
`{{ braces_syntax }}` and then evaluates them with a Python expression. Don't panic -- it's actually
|
2012-04-13 00:20:52 +02:00
|
|
|
pretty simple::
|
2012-03-21 01:15:53 +01:00
|
|
|
|
|
|
|
vars:
|
2012-03-22 02:02:17 +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'
|
2012-03-21 01:15:53 +01:00
|
|
|
|
2012-04-19 05:02:28 +02:00
|
|
|
Variables from tools like `facter` and `ohai` 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 `$facter_operatingsystem`, not `$operatingsystem`. Ansible's
|
|
|
|
built in variables are prefixed with `ansible_`. 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 `True` or `False`. 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.
|
2012-03-21 01:15:53 +01:00
|
|
|
|
|
|
|
|
2012-03-20 04:43:03 +01:00
|
|
|
Conditional Imports
|
|
|
|
+++++++++++++++++++
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
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::
|
|
|
|
|
|
|
|
---
|
|
|
|
- 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
|
|
|
|
|
2012-05-02 07:35:02 +02:00
|
|
|
Note that a variable (`$facter_operatingsystem`) is being interpolated into the list of
|
|
|
|
filenames being defined for vars_files.
|
2012-03-20 04:43:03 +01:00
|
|
|
|
|
|
|
As a reminder, the various YAML files contain just keys and values::
|
|
|
|
|
|
|
|
---
|
|
|
|
# for vars/CentOS.yml
|
|
|
|
apache: httpd
|
|
|
|
somethingelse: 42
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
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::
|
|
|
|
|
|
|
|
# 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"
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
2012-03-13 04:12:21 +01:00
|
|
|
|
|
|
|
Include Files And Reuse
|
|
|
|
+++++++++++++++++++++++
|
|
|
|
|
|
|
|
Suppose you want to reuse lists of tasks between plays or playbooks. You can use
|
|
|
|
include files to do this.
|
|
|
|
|
2012-03-14 01:32:55 +01:00
|
|
|
An 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
|
|
|
|
action: command /bin/foo
|
|
|
|
- name: placeholder bar
|
|
|
|
action: command /bin/bar
|
|
|
|
|
2012-03-14 02:55:55 +01:00
|
|
|
Include directives look like this::
|
2012-03-14 01:32:55 +01:00
|
|
|
|
|
|
|
- tasks:
|
|
|
|
- include: tasks/foo.yml
|
|
|
|
|
2012-05-02 06:34:37 +02:00
|
|
|
You can also pass variables into includes directly. We might 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
|
|
|
|
|
|
|
- tasks:
|
2012-03-13 04:12:21 +01:00
|
|
|
- include: wordpress.yml user=timmy
|
|
|
|
- include: wordpress.yml user=alice
|
|
|
|
- include: wordpress.yml user=bob
|
2012-03-08 19:36:47 +01:00
|
|
|
|
2012-05-02 06:34:37 +02:00
|
|
|
Variables passed in can be used in the included files. Using
|
|
|
|
`jinja2` syntax, in the included file, you can reference them like this::
|
|
|
|
|
|
|
|
{{ user }}
|
|
|
|
|
|
|
|
or, more simply, using Ansible's simplified variable syntax::
|
|
|
|
|
|
|
|
$user
|
|
|
|
|
2012-03-09 17:44:14 +01:00
|
|
|
In addition to the explicitly passed in parameters, all variables from
|
2012-05-02 06:34:37 +02:00
|
|
|
the vars section are also available for use here as well.
|
2012-03-08 19:36:47 +01:00
|
|
|
|
2012-03-13 04:12:21 +01:00
|
|
|
.. note::
|
|
|
|
Include statements are only usable from the top level
|
|
|
|
playbook file. This means includes can not include other
|
2012-05-02 06:34:37 +02:00
|
|
|
includes. This may be implemented in a later release.
|
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-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
|
|
|
|
|
|
|
|
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-03-20 04:43:03 +01: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 13:42:53 +01:00
|
|
|
|
|
|
|
Using Includes To Assign Classes of Systems
|
2012-03-13 04:12:21 +01:00
|
|
|
+++++++++++++++++++++++++++++++++++++++++++
|
2012-03-09 13:42:53 +01:00
|
|
|
|
2012-03-13 04:12:21 +01:00
|
|
|
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::
|
2012-03-09 13:42:53 +01:00
|
|
|
|
|
|
|
---
|
|
|
|
- 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
|
2012-03-09 13:42:53 +01:00
|
|
|
|
2012-03-09 17:44:14 +01:00
|
|
|
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
|
|
|
|
'class definitions', telling the systems what they are supposed to do or be.
|
2012-03-14 01:32:55 +01:00
|
|
|
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.
|
2012-03-09 13:42:53 +01:00
|
|
|
|
2012-03-13 04:12:21 +01:00
|
|
|
.. note::
|
|
|
|
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.
|
2012-03-09 13:42:53 +01:00
|
|
|
|
2012-04-18 02:04:24 +02:00
|
|
|
Loop Shorthand
|
|
|
|
++++++++++++++
|
|
|
|
|
|
|
|
To save some typing, repeated tasks can be written in short-hand like so::
|
|
|
|
|
|
|
|
- name: add user $item
|
|
|
|
action: user name=$item state=present groups=wheel
|
|
|
|
with_items:
|
|
|
|
- testuser1
|
|
|
|
- testuser2
|
|
|
|
|
2012-05-02 15:53:29 +02:00
|
|
|
The above would be the equivalent of::
|
|
|
|
|
|
|
|
- name: add user testuser1
|
|
|
|
action: user name=testuser1 state=present groups=wheel
|
|
|
|
- name: add user testuser2
|
|
|
|
action: user name=testuser2 state=present groups=wheel
|
2012-03-09 13:42:53 +01:00
|
|
|
|
2012-03-08 19:36:47 +01:00
|
|
|
Asynchronous Actions and Polling
|
2012-03-13 04:12:21 +01:00
|
|
|
++++++++++++++++++++++++++++++++
|
|
|
|
|
|
|
|
By default tasks in playbooks block, meaning the connections stay open
|
|
|
|
until the task is done on each node. If executing playbooks with
|
2012-05-02 07:35:02 +02:00
|
|
|
a small parallelism value (aka ``--forks``), you may wish that long
|
2012-03-13 04:12:21 +01:00
|
|
|
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.
|
|
|
|
|
|
|
|
You will also want to use asynchronous mode on very long running
|
|
|
|
operations that might be subject to timeout.
|
|
|
|
|
2012-04-13 00:20:52 +02:00
|
|
|
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 `poll`::
|
|
|
|
|
|
|
|
---
|
|
|
|
- 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
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
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.
|
2012-03-08 19:36:47 +01:00
|
|
|
|
2012-03-13 04:12:21 +01:00
|
|
|
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::
|
2012-03-08 19:36:47 +01:00
|
|
|
|
2012-03-13 04:12:21 +01:00
|
|
|
---
|
|
|
|
- hosts: all
|
|
|
|
user: root
|
|
|
|
tasks:
|
2012-03-13 04:17:59 +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
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
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.
|
|
|
|
|
|
|
|
.. note::
|
2012-05-02 07:35:02 +02:00
|
|
|
Using a higher value for ``--forks`` will result in kicking off asynchronous
|
2012-03-13 04:12:21 +01:00
|
|
|
tasks even faster. This also increases the efficiency of polling.
|
2012-03-09 17:44:14 +01: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-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
|
|
|
|
: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 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
|
|
|
|