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).
Support for running things from sudo is also available::
---
---
- hosts: webservers
- hosts: webservers
roles:
remote_user: yourname
-common
sudo: yes
-{role:foo_app_instance,dir:'/opt/a',port:5000}
-{role:foo_app_instance,dir:'/opt/b',port:5001}
Whileit's probably not something you should do often, you can also conditionally apply roles like so::
You can also use sudo on a particular task instead of the whole play::
what something is, rather than how to make something look like something. It's no longer "apply this handful of THINGS" to these hosts, you say "these hosts are a dbservers" or "these hosts are webservers". In programming, we might call that 'encapsulating' how things work. For instance,
you can drive a car without knowing how the engine works.
Playbook Language Example
Roles in Ansible build on the idea of include files and combine them to form clean, reusable abstractions -- they allow you to focus
`````````````````````````
more on the big picture and only dive down into the details when needed.
Playbooks are expressed in YAML format and have a minimum of syntax.
We'll start with understanding includes so roles make more sense, but our ultimate goal should be understanding roles -- roles
Each playbook is composed of one or more 'plays' in a list.
are great and you should use them every time you write playbooks.
The goal of a play is to map a group of hosts to some well defined roles, represented by
See the `ansible-examples repository on github <https://github.com/ansible/ansible-examples>`_ for lots of examples of all of this
things ansible calls tasks. At a basic level, a task is nothing more than a call
put together. You may wish to have this open in a seperate tab as you dive in.
to an ansible module, which you should have learned about in earlier chapters.
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.
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.
Basics
``````
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 :ref:`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).
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.