tododifferentthings.It's not as if you were just defining one particular state or model, and you
can run different plays at different times.
For starters, here'saplaybookthatcontainsjustoneplay::
---
-hosts:webservers
vars:
http_port:80
max_clients:200
remote_user:root
tasks:
-name:ensureapacheisatthelatestversion
yum:pkg=httpdstate=latest
-name:writetheapacheconfigfile
template:src=/srv/httpd.j2dest=/etc/httpd.conf
notify:
-restartapache
-name:ensureapacheisrunning
service:name=httpdstate=started
handlers:
-name:restartapache
service:name=httpdstate=restarted
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.
The `hosts` line is a list of one or more groups or host patterns,
separated by colons, as described in the :doc:`intro_patterns`
documentation. The `remote_user` is just the name of the user account::
---
- hosts: webservers
remote_user: root
.. Note::
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).
Remote users can also be defined per task::
---
- hosts: webservers
remote_user: root
tasks:
- name: test connection
ping:
remote_user: yourname
.. Note::
The `remote_user` parameter for tasks was added in 1.4.
Support for running things from sudo is also available::
---
- hosts: webservers
remote_user: yourname
sudo: yes
You can also use sudo on a particular task instead of the whole play::
---
- hosts: webservers
remote_user: yourname
tasks:
- service: name=nginx state=started
sudo: yes
You can also login as you, and then sudo to different users than root::
---
- hosts: webservers
remote_user: yourname
sudo: yes
sudo_user: postgres
If you need to specify a password to sudo, run `ansible-playbook` with ``--ask-sudo-pass`` (`-K`).
If you run a sudo playbook and the playbook seems to hang, it'sprobablystuckatthesudoprompt.
There's also a `clever playbook <https://github.com/ansible/ansible-examples/blob/master/language_features/ansible_pull.yml>`_ available to using ansible in push mode to configure ansible-pull via a crontab!
Tips and Tricks
```````````````
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.
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.
Ansible playbook output is vastly upgraded if the cowsay
package is installed. Try it!
To see what hosts would be affected by a playbook before you run it, you
can do this::
ansible-playbook playbook.yml --list-hosts.
.. seealso::
:doc:`YAMLSyntax`
Learn about YAML syntax
:doc:`playbooks_best_practices`
Various tips about managing playbooks in the real world
:doc:`index`
Hop back to the documentation index for a lot of special topics about playbooks
:doc:`modules`
Learn about available modules
:doc:`developing_modules`
Learn how to extend Ansible by writing your own modules