1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00
community.general/rst/playbooks.rst

155 lines
5.7 KiB
ReStructuredText
Raw Normal View History

2012-03-08 19:53:48 +01:00
Playbooks: Ansible for Deployment, Configuration Management, and Orchestration
==============================================================================
2012-03-07 17:35:18 +01:00
2012-03-08 19:36:47 +01:00
.. seealso::
:doc:`YAMLScripts`
Learn about YAML syntax
:doc:`modules`
Learn about available modules and writing your own
:doc:`patterns`
Learn about how to select hosts
2012-03-09 04:50:00 +01: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.
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
Playbook Example
````````````````
Playbooks are expressed in YAML format and have a minimum of syntax. Each playbook is composed
2012-03-08 19:53:48 +01:00
of one or more 'plays' in a list. By composing a playbook of multiple 'plays', it is possible
2012-03-08 19:36:47 +01:00
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-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:
- include: base.yml somevar=3 othervar=4
- 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:
- include: handlers.yml
Hosts line
``````````
2012-03-09 04:50:00 +01:00
The hosts line is a list of one or more groups or host patterns, seperated by colons, as
described in the 'patterns' documentation. This is just like the first parameter to /usr/bin/ansible.
2012-03-08 19:36:47 +01:00
Vars section
````````````
2012-03-09 04:50:00 +01:00
A list of variables and values that can be used in the plays. These can be used in templates
or 'action' lines and are dereferenced using ```jinja2``` syntax like this:
2012-03-08 19:36:47 +01:00
{{ varname }}
Further, if there are discovered variables about the system (say, if facter or ohai were
installed) these variables bubble up back into the playbook, and can be used on each
2012-03-08 19:53:48 +01:00
system just like explicitly set variables. Facter variables are prefixed with 'facter_'
2012-03-09 04:50:00 +01:00
and Ohai variables are prefixed with 'ohai_'. So for instance, if I wanted to write the
hostname into the /etc/motd file, I could say:
- name: write the motd
- action: template src=/srv/templates/motd.j2 dest=/etc/motd
And in /srv/templates/motd.j2:::
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-08 19:53:48 +01:00
Each play contains a list of tasks. Tasks are executed in order, one at a time, against
all machines matched by the play's host pattern, before moving on to the next task.
2012-03-09 04:50:00 +01:00
2012-03-08 19:53:48 +01:00
Hosts with failed tasks are taken out of the rotation for the entire playbook. If things fail,
2012-03-09 04:50:00 +01:00
simply correct the playbook file and rerun.
Modules other than command 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.
2012-03-08 19:36:47 +01:00
2012-03-08 19:53:48 +01:00
Task name and action
`````````````````````
2012-03-08 19:36:47 +01:00
2012-03-08 19:53:48 +01:00
Every task must have a name, which is included in the output from running the playbook.
2012-03-08 19:36:47 +01:00
The action line is the name of an ansible module followed by parameters. Usually these
are expressed in key=value form, except for the command module, which looks just like a Linux/Unix
command line. See the module documentation for more info.
2012-03-09 04:50:00 +01:00
Variables, as mentioned above, can be used in action lines. So if, hypothetically, you wanted
to make a directory on each system named after the hostname ... yeah, that's I know silly ... you could
do it like so:
- name: make a directory
- action: mkdir /tmp/{{ facter_hostname }}
2012-03-08 19:36:47 +01:00
Notify statements
`````````````````
Nearly all modules are written to be 'idempotent' and can signal when they have affected a change
on the remote system. If a notify statement is used, the named handler will be run against
2012-03-09 04:50:00 +01:00
each system where a change was effected, but NOT on systems where no change occurred. This happens
after all of the tasks are run. For example, if notifying Apache and potentially replacing lots of
configuration files, you could have Apache restart just once, at the end of a run. If you need
Apache restarted in the middle of a run, you could just make a task for it, no harm done. Notifiers
are optional.
2012-03-08 19:36:47 +01:00
Handlers
````````
Handlers are lists of tasks, not really any different from regular tasks, that are referenced
2012-03-09 04:50:00 +01:00
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 19:36:47 +01:00
Includes
````````
Not all tasks have to be listed directly in the main file. An include file can contain
a list of tasks (in YAML) as well, optionally passing extra variables into the file.
2012-03-09 04:50:00 +01:00
Variables passed in can be deferenced like this (assume a variable named 'user')
{{ user }}
For instance, if deploying multiple wordpress instances, I could contain all of my tasks
in a wordpress.yml file, and use it like so:
- tasks:
- include: wordpress.yml user=timmy
- include: wordpress.yml user=alice
- include: wordpress.yml user=bob
2012-03-08 19:36:47 +01:00
2012-03-09 04:50:00 +01:00
In addition to the explicitly passed in parameters, all variables from the vars section
are also available.
2012-03-08 19:36:47 +01:00
Asynchronous Actions and Polling
````````````````````````````````
(Information on this feature is pending)
Executing A Playbook
````````````````````
To run a playbook::
ansible-playbook playbook.yml