2013-12-25 00:51:46 +01:00
Intro to Playbooks
==================
2019-02-06 21:56:52 +01:00
.. contents ::
:local:
2013-12-25 00:51:46 +01:00
.. _about_playbooks:
2019-06-26 23:07:27 +02:00
.. _playbooks_intro:
2013-12-25 00:51:46 +01:00
About Playbooks
`` ` ` ` ` ` ` ` ` ` ` ` ``
2018-10-24 18:01:24 +02:00
Playbooks are a completely different way to use ansible than in ad-hoc task execution mode, and are
2015-10-21 10:00:47 +02:00
particularly powerful.
2013-12-25 00:51:46 +01:00
Simply put, playbooks 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.
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.
2015-09-14 08:55:39 +02:00
While you might run the main `` /usr/bin/ansible `` program for ad-hoc
2013-12-25 00:51:46 +01:00
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.
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> `_ . We'd recommend
looking at these in another tab as you go along.
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.
.. _playbook_language_example:
Playbook Language Example
`` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ``
2018-03-14 20:44:21 +01:00
Playbooks are expressed in YAML format (see :ref: `yaml_syntax` ) and have a minimum of syntax, which intentionally
2013-12-25 00:51:46 +01:00
tries to not be a programming language or script, but rather a model of a configuration or a process.
2019-02-06 21:56:52 +01:00
.. note ::
Some editors have add-ons that can help you write clean YAML syntax in your playbooks. See :ref: `other_tools_and_programs` for details.
2013-12-25 00:51:46 +01:00
Each playbook is composed of one or more 'plays' in a list.
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
2019-06-26 23:07:27 +02:00
to an ansible module.
2013-12-25 00:51:46 +01: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.
"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
2015-10-21 10:00:47 +02:00
can run different plays at different times.
2013-12-25 00:51:46 +01:00
2019-02-06 21:56:52 +01:00
.. _apache-playbook:
For starters, here's a playbook, `` verify-apache.yml `` that contains just one play::
2013-12-25 00:51:46 +01:00
2015-02-04 16:49:51 +01:00
---
- hosts: webservers
vars:
http_port: 80
max_clients: 200
remote_user: root
tasks:
- name: ensure apache is at the latest version
yum:
2015-10-20 13:32:35 +02:00
name: httpd
2015-02-04 16:49:51 +01:00
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
2015-06-09 01:32:44 +02:00
Playbooks can contain multiple plays. You may have a playbook that targets first
the web servers, and then the database servers. For example::
---
- hosts: webservers
remote_user: root
tasks:
- name: ensure apache is at the latest version
2017-09-14 05:06:33 +02:00
yum:
name: httpd
state: latest
2015-06-09 01:32:44 +02:00
- name: write the apache config file
2017-09-14 05:06:33 +02:00
template:
src: /srv/httpd.j2
dest: /etc/httpd.conf
2015-06-09 01:32:44 +02:00
- hosts: databases
remote_user: root
tasks:
- name: ensure postgresql is at the latest version
2017-09-14 05:06:33 +02:00
yum:
name: postgresql
state: latest
2015-06-09 01:32:44 +02:00
- name: ensure that postgresql is started
2017-09-14 05:06:33 +02:00
service:
name: postgresql
state: started
2015-06-09 01:32:44 +02:00
You can use this method to switch between the host group you're targeting,
the username logging into the remote servers, whether to sudo or not, and so
forth. Plays, like tasks, run in the order specified in the playbook: top to
bottom.
2013-12-25 00:51:46 +01:00
Below, we'll break down what the various features of the playbook language are.
.. _playbook_basics:
Basics
`` ` ` ``
.. _playbook_hosts_and_users:
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.
2015-09-14 08:55:39 +02:00
The `` hosts `` line is a list of one or more groups or host patterns,
2019-06-26 23:07:27 +02:00
separated by colons, as described in the :ref: `intro_patterns`
2015-09-14 08:55:39 +02:00
documentation. The `` remote_user `` is just the name of the user account::
2013-12-25 00:51:46 +01:00
---
- hosts: webservers
remote_user: root
2014-01-10 04:59:47 +01:00
.. note ::
2013-12-25 00:51:46 +01:00
2015-09-14 09:01:07 +02:00
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).
2013-12-25 00:51:46 +01:00
Remote users can also be defined per task::
---
- hosts: webservers
remote_user: root
tasks:
- name: test connection
ping:
remote_user: yourname
2019-06-26 23:07:27 +02:00
Support for running things as another user is also available (see :ref: `become` )::
2013-12-25 00:51:46 +01:00
---
- hosts: webservers
remote_user: yourname
2016-02-16 22:19:26 +01:00
become: yes
2013-12-25 00:51:46 +01:00
2018-07-17 17:46:03 +02:00
You can also use keyword `` become `` on a particular task instead of the whole play::
2013-12-25 00:51:46 +01:00
---
- hosts: webservers
remote_user: yourname
tasks:
2018-12-03 22:21:59 +01:00
- service:
2017-09-14 05:06:33 +02:00
name: nginx
state: started
2015-04-20 19:31:36 +02:00
become: yes
become_method: sudo
2013-12-25 00:51:46 +01:00
2015-04-20 19:31:36 +02:00
You can also login as you, and then become a user different than root::
2013-12-25 00:51:46 +01:00
---
- hosts: webservers
remote_user: yourname
2015-04-20 19:31:36 +02:00
become: yes
become_user: postgres
You can also use other privilege escalation methods, like su::
---
- hosts: webservers
remote_user: yourname
become: yes
become_method: su
2013-12-25 00:51:46 +01:00
Become plugins (#50991)
* [WIP] become plugins
Move from hardcoded method to plugins for ease of use, expansion and overrides
- load into connection as it is going to be the main consumer
- play_context will also use to keep backwards compat API
- ensure shell is used to construct commands when needed
- migrate settings remove from base config in favor of plugin specific configs
- cleanup ansible-doc
- add become plugin docs
- remove deprecated sudo/su code and keywords
- adjust become options for cli
- set plugin options from context
- ensure config defs are avaialbe before instance
- refactored getting the shell plugin, fixed tests
- changed into regex as they were string matching, which does not work with random string generation
- explicitly set flags for play context tests
- moved plugin loading up front
- now loads for basedir also
- allow pyc/o for non m modules
- fixes to tests and some plugins
- migrate to play objects fro play_context
- simiplify gathering
- added utf8 headers
- moved option setting
- add fail msg to dzdo
- use tuple for multiple options on fail/missing
- fix relative plugin paths
- shift from play context to play
- all tasks already inherit this from play directly
- remove obsolete 'set play'
- correct environment handling
- add wrap_exe option to pfexec
- fix runas to noop
- fixed setting play context
- added password configs
- removed required false
- remove from doc building till they are ready
future development:
- deal with 'enable' and 'runas' which are not 'command wrappers' but 'state flags' and currently hardcoded in diff subsystems
* cleanup
remove callers to removed func
removed --sudo cli doc refs
remove runas become_exe
ensure keyerorr on plugin
also fix backwards compat, missing method is attributeerror, not ansible error
get remote_user consistently
ignore missing system_tmpdirs on plugin load
correct config precedence
add deprecation
fix networking imports
backwards compat for plugins using BECOME_METHODS
* Port become_plugins to context.CLIARGS
This is a work in progress:
* Stop passing options around everywhere as we can use context.CLIARGS
instead
* Refactor make_become_commands as asked for by alikins
* Typo in comment fix
* Stop loading values from the cli in more than one place
Both play and play_context were saving default values from the cli
arguments directly. This changes things so that the default values are
loaded into the play and then play_context takes them from there.
* Rename BECOME_PLUGIN_PATH to DEFAULT_BECOME_PLUGIN_PATH
As alikins said, all other plugin paths are named
DEFAULT_plugintype_PLUGIN_PATH. If we're going to rename these, that
should be done all at one time rather than piecemeal.
* One to throw away
This is a set of hacks to get setting FieldAttribute defaults to command
line args to work. It's not fully done yet.
After talking it over with sivel and jimi-c this should be done by
fixing FieldAttributeBase and _get_parent_attribute() calls to do the
right thing when there is a non-None default.
What we want to be able to do ideally is something like this:
class Base(FieldAttributeBase):
_check_mode = FieldAttribute([..] default=lambda: context.CLIARGS['check'])
class Play(Base):
# lambda so that we have a chance to parse the command line args
# before we get here. In the future we might be able to restructure
# this so that the cli parsing code runs before these classes are
# defined.
class Task(Base):
pass
And still have a playbook like this function:
---
- hosts:
tasks:
- command: whoami
check_mode: True
(The check_mode test that is added as a separate commit in this PR will
let you test variations on this case).
There's a few separate reasons that the code doesn't let us do this or
a non-ugly workaround for this as written right now. The fix that
jimi-c, sivel, and I talked about may let us do this or it may still
require a workaround (but less ugly) (having one class that has the
FieldAttributes with default values and one class that inherits from
that but just overrides the FieldAttributes which now have defaults)
* Revert "One to throw away"
This reverts commit 23aa883cbed11429ef1be2a2d0ed18f83a3b8064.
* Set FieldAttr defaults directly from CLIARGS
* Remove dead code
* Move timeout directly to PlayContext, it's never needed on Play
* just for backwards compat, add a static version of BECOME_METHODS to constants
* Make the become attr on the connection public, since it's used outside of the connection
* Logic fix
* Nuke connection testing if it supports specific become methods
* Remove unused vars
* Address rebase issues
* Fix path encoding issue
* Remove unused import
* Various cleanups
* Restore network_cli check in _low_level_execute_command
* type improvements for cliargs_deferred_get and swap shallowcopy to default to False
* minor cleanups
* Allow the su plugin to work, since it doesn't define a prompt the same way
* Fix up ksu become plugin
* Only set prompt if build_become_command was called
* Add helper to assist connection plugins in knowing they need to wait for a prompt
* Fix tests and code expectations
* Doc updates
* Various additional minor cleanups
* Make doas functional
* Don't change connection signature, load become plugin from TaskExecutor
* Remove unused imports
* Add comment about setting the become plugin on the playcontext
* Fix up tests for recent changes
* Support 'Password:' natively for the doas plugin
* Make default prompts raw
* wording cleanups. ci_complete
* Remove unrelated changes
* Address spelling mistake
* Restore removed test, and udpate to use new functionality
* Add changelog fragment
* Don't hard fail in set_attributes_from_cli on missing CLI keys
* Remove unrelated change to loader
* Remove internal deprecated FieldAttributes now
* Emit deprecation warnings now
2019-02-11 18:27:44 +01:00
If you need to specify a password for sudo, run `` ansible-playbook `` with `` --ask-become-pass `` or `` -K `` .
If you run a playbook utilizing `` become `` and the playbook seems to hang, it's probably stuck at the privilege
escalation prompt and can be stopped using `Control-C` , allowing you to re-execute the playbook adding the
appropriate password.
2013-12-25 00:51:46 +01:00
.. important ::
2015-09-14 08:55:39 +02:00
When using `` become_user `` to a user other than root, the module
arguments are briefly written into a random tempfile in `` /tmp `` .
2013-12-25 00:51:46 +01:00
These are deleted immediately after the command is executed. This
2015-04-20 19:31:36 +02:00
only occurs when changing privileges from a user like 'bob' to 'timmy',
not when going from 'bob' to 'root', or logging in directly as 'bob' or
2014-12-29 14:03:38 +01:00
'root'. If it concerns you that this data is briefly readable
2015-01-13 17:53:14 +01:00
(not writable), avoid transferring unencrypted passwords with
2015-09-14 08:55:39 +02:00
`become_user` set. In other cases, `` /tmp `` is not used and this does
2013-12-25 00:51:46 +01:00
not come into play. Ansible also takes care to not log password
parameters.
2017-05-23 16:55:59 +02:00
.. _order:
2017-06-23 22:44:48 +02:00
2017-06-09 20:09:14 +02:00
.. versionadded :: 2.4
2017-05-24 18:28:40 +02:00
2017-05-24 10:08:51 +02:00
You can also control the order in which hosts are run. The default is to follow the order supplied by the inventory::
2017-05-23 16:55:59 +02:00
- hosts: all
order: sorted
gather_facts: False
tasks:
2018-02-03 12:30:54 +01:00
- debug:
var: inventory_hostname
2017-05-23 16:55:59 +02:00
Possible values for order are:
inventory:
2017-05-24 10:08:51 +02:00
The default. The order is 'as provided' by the inventory
2017-05-23 16:55:59 +02:00
reverse_inventory:
As the name implies, this reverses the order 'as provided' by the inventory
sorted:
2017-05-24 10:08:51 +02:00
Hosts are alphabetically sorted by name
2017-05-23 16:55:59 +02:00
reverse_sorted:
2017-05-24 10:08:51 +02:00
Hosts are sorted by name in reverse alphabetical order
2017-05-23 16:55:59 +02:00
shuffle:
Hosts are randomly ordered each run
2013-12-25 00:51:46 +01:00
.. _tasks_list:
Tasks list
++++++++++
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.
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.
The goal of each task is to execute a module, with very specific arguments.
2019-01-24 23:39:24 +01:00
Variables can be used in arguments to modules.
2013-12-25 00:51:46 +01:00
2016-12-01 23:32:55 +01:00
Modules should be idempotent, that is, running a module multiple times
in a sequence should have the same effect as running it just once. One
way to achieve idempotency is to have a module check whether its desired
final state has already been achieved, and if that state has been achieved,
to exit without performing any actions. If all the modules a playbook uses
are idempotent, then the playbook itself is likely to be idempotent, so
re-running the playbook should be safe.
2013-12-25 00:51:46 +01:00
2015-09-14 09:01:07 +02:00
The **command** and **shell** modules will typically rerun the same command again,
2013-12-25 00:51:46 +01:00
which is totally ok if the command is something like
2015-09-14 08:55:39 +02:00
`` chmod `` or `` setsebool `` , etc. Though there is a `` creates `` flag available which can
2013-12-25 00:51:46 +01:00
be used to make these modules also idempotent.
2015-09-14 08:55:39 +02:00
Every task should have a `` name `` , which is included in the output from
2018-12-03 22:21:59 +01:00
running the playbook. This is human readable output, and so it is
2016-12-01 23:32:55 +01:00
useful to provide good descriptions of each task step. If the name
2013-12-25 00:51:46 +01:00
is not provided though, the string fed to 'action' will be used for
output.
2015-10-21 10:00:47 +02:00
Tasks can be declared using the legacy `` action: module options `` format, but
2015-09-14 08:55:39 +02:00
it is recommended that you use the more conventional `` module: options `` format.
2013-12-25 00:51:46 +01:00
This recommended format is used throughout the documentation, but you may
encounter the older format in some playbooks.
2014-10-26 20:44:04 +01:00
Here is what a basic task looks like. As with most modules,
2015-09-14 08:55:39 +02:00
the service module takes `` key=value `` arguments::
2013-12-25 00:51:46 +01:00
tasks:
- name: make sure apache is running
2017-09-14 05:06:33 +02:00
service:
name: httpd
state: started
2013-12-25 00:51:46 +01:00
2015-09-14 09:01:07 +02:00
The **command** and **shell** modules are the only modules that just take a list
2015-09-14 08:55:39 +02:00
of arguments and don't use the `` key=value `` form. This makes
2014-01-10 04:27:04 +01:00
them work as simply as you would expect::
2013-12-25 00:51:46 +01:00
tasks:
2017-03-18 06:18:48 +01:00
- name: enable selinux
command: /sbin/setenforce 1
2013-12-25 00:51:46 +01:00
2015-09-14 09:01:07 +02:00
The **command** and **shell** module care about return codes, so if you have a command
2013-12-25 00:51:46 +01:00
whose successful exit code is not zero, you may wish to do this::
tasks:
- name: run this command and ignore the result
shell: /usr/bin/somecommand || /bin/true
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
2015-09-14 08:55:39 +02:00
a variable called `` vhost `` in the `` vars `` section, you could do this::
2013-12-25 00:51:46 +01:00
tasks:
- name: create a virtual host file for {{ vhost }}
2017-09-14 05:06:33 +02:00
template:
src: somefile.j2
dest: /etc/httpd/conf.d/{{ vhost }}
2013-12-25 00:51:46 +01:00
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
2019-06-26 23:07:27 +02:00
make more sense to break up tasks as described in :ref: `playbooks_reuse` .
2013-12-25 00:51:46 +01:00
.. _action_shorthand:
Action Shorthand
`` ` ` ` ` ` ` ` ` ` ` ` ` ``
.. versionadded :: 0.8
2017-11-22 05:14:27 +01:00
Ansible prefers listing modules like this::
2013-12-25 00:51:46 +01:00
2017-11-22 05:14:27 +01:00
template:
src: templates/foo.j2
dest: /etc/foo.conf
2013-12-25 00:51:46 +01:00
2017-11-22 05:14:27 +01:00
Early versions of Ansible used the following format, which still works::
2013-12-25 00:51:46 +01:00
action: template src=templates/foo.j2 dest=/etc/foo.conf
.. _handlers:
Handlers: Running Operations On Change
`` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ``
2016-12-01 23:32:55 +01:00
As we've mentioned, modules should be idempotent and can relay when
2013-12-25 00:51:46 +01:00
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.
2016-08-17 16:36:33 +02:00
These 'notify' actions are triggered at the end of each block of tasks in a play, and will only be
2013-12-25 00:51:46 +01:00
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,
but apache will only be bounced once to avoid unnecessary restarts.
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
2017-09-14 05:06:33 +02:00
template:
src: template.j2
dest: /etc/foo.conf
2013-12-25 00:51:46 +01:00
notify:
- restart memcached
- restart apache
2015-09-14 08:55:39 +02:00
The things listed in the `` notify `` section of a task are called
2013-12-25 00:51:46 +01:00
handlers.
Handlers are lists of tasks, not really any different from regular
2018-12-03 22:21:59 +01:00
tasks, that are referenced by a globally unique name, and are notified
2016-03-26 00:35:04 +01:00
by notifiers. If nothing notifies a handler, it will not
run. Regardless of how many tasks notify a handler, it will run only
2015-07-19 08:36:01 +02:00
once, after all of the tasks complete in a particular play.
2013-12-25 00:51:46 +01:00
Here's an example handlers section::
handlers:
- name: restart memcached
2017-09-14 05:06:33 +02:00
service:
name: memcached
state: restarted
2013-12-25 00:51:46 +01:00
- name: restart apache
2017-09-14 05:06:33 +02:00
service:
name: apache
state: restarted
2013-12-25 00:51:46 +01:00
2019-04-11 17:23:08 +02:00
You may want your Ansible handlers to use variables. For example, if the name of a service varies slightly by distribution, you want your output to show the exact name of the restarted service for each target machine. Avoid placing variables in the name of the handler. Since handler names are templated early on, Ansible may not have a value available for a handler name like this::
handlers:
# this handler name may cause your play to fail!
- name: restart "{{ web_service_name }}"
If the variable used in the handler name is not available, the entire play fails. Changing that variable mid-play **will not** result in newly created handler.
Instead, place variables in the task parameters of your handler. You can load the values using `` include_vars `` like this:
.. code-block :: yaml+jinja
tasks:
- name: Set host variables based on distribution
include_vars: "{{ ansible_facts.distribution }}.yml"
handlers:
- name: restart web service
service:
name: "{{ web_service_name | default('httpd') }}"
state: restarted
2016-06-20 18:35:59 +02:00
As of Ansible 2.2, handlers can also "listen" to generic topics, and tasks can notify those topics as follows::
2016-06-20 16:29:52 +02:00
handlers:
- name: restart memcached
2017-09-14 05:06:33 +02:00
service:
name: memcached
state: restarted
2016-06-20 16:29:52 +02:00
listen: "restart web services"
- name: restart apache
2017-09-14 05:06:33 +02:00
service:
name: apache
2018-12-20 13:13:52 +01:00
state: restarted
2016-06-20 16:29:52 +02:00
listen: "restart web services"
tasks:
- name: restart everything
command: echo "this task will restart the web services"
notify: "restart web services"
This use makes it much easier to trigger multiple handlers. It also decouples handlers from their names,
making it easier to share handlers among playbooks and roles (especially when using 3rd party roles from
a shared source like Galaxy).
2013-12-25 00:51:46 +01:00
.. note ::
2016-06-20 16:29:52 +02:00
* Notify handlers are always run in the same order they are defined, `not` in the order listed in the notify-statement. This is also the case for handlers using `listen` .
* Handler names and `listen` topics live in a global namespace.
2019-06-27 23:29:20 +02:00
* Handler names are templatable and `listen` topics are not.
2018-12-03 22:21:59 +01:00
* Use unique handler names. If you trigger more than one handler with the same name, the first one(s) get overwritten. Only the last one defined will run.
2016-06-20 16:29:52 +02:00
* You cannot notify a handler that is defined inside of an include. As of Ansible 2.1, this does work, however the include must be `static` .
2013-12-25 00:51:46 +01:00
2015-10-21 10:00:47 +02:00
Roles are described later on, but it's worthwhile to point out that:
2018-12-03 22:21:59 +01:00
* handlers notified within `` pre_tasks `` , `` tasks `` , and `` post_tasks `` sections are automatically flushed in the end of section where they were notified,
* handlers notified within `` roles `` section are automatically flushed in the end of `` tasks `` section, but before any `` tasks `` handlers,
* handlers are play scoped and as such can be used outside of the role they are defined in.
2015-10-21 10:00:47 +02:00
2017-11-22 05:14:27 +01:00
If you ever want to flush all the handler commands immediately you can do this::
2013-12-25 00:51:46 +01:00
tasks:
- shell: some tasks go here
- meta: flush_handlers
- shell: some other tasks
2015-09-14 08:55:39 +02:00
In the above example any queued up handlers would be processed early when the `` meta ``
2013-12-25 00:51:46 +01:00
statement was reached. This is a bit of a niche case but can come in handy from
time to time.
.. _executing_a_playbook:
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
2017-09-14 17:17:56 +02:00
.. _playbook_ansible-pull:
2013-12-25 00:51:46 +01:00
Ansible-Pull
`` ` ` ` ` ` ` ` ` ``
Should you want to invert the architecture of Ansible, so that nodes check in to a central location, instead
of pushing configuration out to them, you can.
2015-09-14 08:55:39 +02:00
The `` ansible-pull `` is a small script that will checkout a repo of configuration instructions from git, and then
run `` ansible-playbook `` against that content.
2013-12-25 00:51:46 +01:00
2015-09-14 08:55:39 +02:00
Assuming you load balance your checkout location, `` ansible-pull `` scales essentially infinitely.
2013-12-25 00:51:46 +01:00
2014-01-22 06:05:21 +01:00
Run `` ansible-pull --help `` for details.
2013-12-25 00:51:46 +01:00
2015-09-14 08:55:39 +02:00
There's also a `clever playbook <https://github.com/ansible/ansible-examples/blob/master/language_features/ansible_pull.yml> `_ available to configure `` ansible-pull `` via a crontab from push mode.
2013-12-25 00:51:46 +01:00
2019-02-06 21:56:52 +01:00
.. _linting_playbooks:
Linting playbooks
`` ` ` ` ` ` ` ` ` ` ` ` ` ` ``
You can use `ansible-lint <https://docs.ansible.com/ansible-lint/index.html> `_ to run a detail check of your playbooks before you execute them.
For example, if you run `` ansible-lint `` on the :ref: `verify-apache.yml playbook <apache-playbook>` introduced earlier in this section, you'll get the following results:
.. code-block :: bash
2019-05-24 16:53:00 +02:00
$ ansible-lint verify-apache.yml
2019-02-06 21:56:52 +01:00
[403] Package installs should not use latest
verify-apache.yml:8
Task/Handler: ensure apache is at the latest version
The `ansible-lint default rules <https://docs.ansible.com/ansible-lint/rules/default_rules.html> `_ page describes each error. For `` [403] `` , the recommended fix is to change `` state: latest `` to `` state: present `` in the playbook.
2013-12-21 08:06:38 +01:00
2013-12-25 00:51:46 +01:00
2019-02-06 21:56:52 +01:00
Other playbook verification options
`` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ``
See :ref: `validate-playbook-tools` for a detailed list of tools you can use to verify your playbooks. Here are some others that you should consider:
2017-03-04 20:11:41 +01:00
2019-02-06 21:56:52 +01:00
* To check the syntax of a playbook, use `` ansible-playbook `` with the `` --syntax-check `` flag. This will run the
playbook file through the parser to ensure its included files, roles, etc. have no syntax problems.
2013-12-25 00:51:46 +01:00
2019-02-06 21:56:52 +01:00
* Look at the bottom of the playbook execution for a summary of the nodes that were targeted
and how they performed. General failures and fatal "unreachable" communication attempts are kept separate in the counts.
2013-12-25 00:51:46 +01:00
2019-02-06 21:56:52 +01: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-12-25 00:51:46 +01:00
2019-02-06 21:56:52 +01:00
* To see what hosts would be affected by a playbook before you run it, you
can do this::
2013-12-25 00:51:46 +01:00
2019-02-06 21:56:52 +01:00
ansible-playbook playbook.yml --list-hosts
2013-12-25 00:51:46 +01:00
.. seealso ::
2019-02-06 21:56:52 +01:00
`ansible-lint <https://docs.ansible.com/ansible-lint/index.html> `_
Learn how to test Ansible Playbooks syntax
2018-03-14 20:44:21 +01:00
:ref: `yaml_syntax`
2013-12-25 00:51:46 +01:00
Learn about YAML syntax
2018-03-14 20:44:21 +01:00
:ref: `playbooks_best_practices`
2013-12-25 00:51:46 +01:00
Various tips about managing playbooks in the real world
2018-03-14 20:44:21 +01:00
:ref: `all_modules`
2013-12-25 00:51:46 +01:00
Learn about available modules
2018-03-14 20:44:21 +01:00
:ref: `developing_modules`
2013-12-25 00:51:46 +01:00
Learn how to extend Ansible by writing your own modules
2018-03-14 20:44:21 +01:00
:ref: `intro_patterns`
2013-12-25 00:51:46 +01:00
Learn about how to select hosts
2019-01-15 14:53:04 +01:00
`GitHub examples directory <https://github.com/ansible/ansible-examples> `_
2013-12-25 00:51:46 +01:00
Complete end-to-end playbook examples
2018-07-21 15:48:47 +02:00
`Mailing List <https://groups.google.com/group/ansible-project> `_
2013-12-25 00:51:46 +01:00
Questions? Help? Ideas? Stop by the list on Google Groups