Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to conventional task format #3542

Merged
merged 1 commit into from
Jul 19, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
43 changes: 24 additions & 19 deletions docsite/latest/rst/playbooks.rst
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,16 @@ For starters, here's a playbook that contains just one play::
user: root
tasks:
- name: ensure apache is at the latest version
action: yum pkg=httpd state=latest
yum: pkg=httpd state=latest
- name: write the apache config file
action: template src=/srv/httpd.j2 dest=/etc/httpd.conf
template: src=/srv/httpd.j2 dest=/etc/httpd.conf
notify:
- restart apache
- name: ensure apache is running
action: service name=httpd state=started
service: name=httpd state=started
handlers:
- name: restart apache
action: service name=httpd state=restarted
service: name=httpd state=restarted

Below, we'll break down what the various features of the playbook language are.

Expand Down Expand Up @@ -165,7 +165,7 @@ porting over from those other configuration systems.
How about an example. 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
template: src=/srv/templates/motd.j2 dest=/etc/motd

And in /srv/templates/motd.j2::

Expand Down Expand Up @@ -205,33 +205,38 @@ 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.

Tasks can be declared using the legacy "action: module options" format, but
it is recommeded that you use the more conventional "module: options" format.
This recommended format is used throughout the documentation, but you may
encounter the older format in some playbooks.

Here is what a basic task looks like, as with most modules,
the service module takes key=value arguments::

tasks:
- name: make sure apache is running
action: service name=httpd state=running
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::

tasks:
- name: disable selinux
action: command /sbin/setenforce 0
command: /sbin/setenforce 0

The command and shell module care about return codes, so if you have a command
who's successful exit code is not zero, you may wish to do this::

tasks:
- name: run this command and ignore the result
action: shell /usr/bin/somecommand || /bin/true
shell: /usr/bin/somecommand || /bin/true

Or this::

tasks:
- name: run this command and ignore the result
action: shell /usr/bin/somecommand
shell: /usr/bin/somecommand
ignore_errors: True


Expand All @@ -240,15 +245,15 @@ a space and indent any continuation lines::

tasks:
- name: Copy ansible inventory file to client
action: copy src=/etc/ansible/hosts dest=/etc/ansible/hosts
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 }}
action: template src=somefile.j2 dest=/etc/httpd/conf.d/{{ vhost }}
template: src=somefile.j2 dest=/etc/httpd/conf.d/{{ vhost }}

Those same variables are usable in templates, which we'll get to later.

Expand All @@ -262,7 +267,7 @@ Action Shorthand

Rather than listing out the explicit word, "action:", like so::

action: template src=templates/foo.j2 dest=/etc/foo.conf
template: src=templates/foo.j2 dest=/etc/foo.conf

It is also possible to say:

Expand Down Expand Up @@ -290,7 +295,7 @@ 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
template: src=template.j2 dest=/etc/foo.conf
notify:
- restart memcached
- restart apache
Expand All @@ -308,9 +313,9 @@ Here's an example handlers section::

handlers:
- name: restart memcached
action: service name=memcached state=restarted
service: name=memcached state=restarted
- name: restart apache
action: service name=apache state=restarted
service: name=apache state=restarted

Handlers are best used to restart services and trigger reboots. You probably
won't need them for much else.
Expand Down Expand Up @@ -345,9 +350,9 @@ A task include file simply contains a flat list of tasks, like so::
---
# possibly saved as tasks/foo.yml
- name: placeholder foo
action: command /bin/foo
command: /bin/foo
- name: placeholder bar
action: command /bin/bar
command: /bin/bar

Include directives look like this, and can be mixed in with regular tasks in a playbook::

Expand Down Expand Up @@ -398,7 +403,7 @@ 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
service: name=apache state=restarted

And in your main playbook file, just include it like so, at the bottom
of a play::
Expand All @@ -419,7 +424,7 @@ For example::
tasks:
- name: say hi
tags: foo
action: shell echo "hi..."
shell: echo "hi..."

- include: load_balancers.yml
- include: webservers.yml
Expand Down