# An employee record
martin:
name: Martin D'vloper
job: Developer
skill: Elite
Abbreviated form
martin: {name: Martin D'vloper, job: Developer, skill: Elite}
Ansible Playbooks
Each playbook is composed of one or more ‘plays’ in a list.
The goal of a play is to execute some tasks against a group of hosts
A task is essentially a call to an ansible module
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.
Ansible Playbooks
IN other words, a playbook consists of one or more plays. A play consists of one or more tasks.
Ansible will execute plays in order of hosts file. Can use sorted option if you want to execute alphabetically. (shuffle, reverse_sorted, etc...)
Ansible Playbooks (One play)
---
- hosts: webservers
vars:
http_port: 80
max_clients: 200
remote_user: root
tasks:
- name: ensure apache is at the latest version
yum: name=httpd 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 (and enable it at boot)
service: name=httpd state=started enabled=yes
handlers:
- name: restart apache
service: name=httpd state=restarted
Ansible Playbooks (multiple plays)
---
- hosts: webservers
remote_user: root
tasks:
- name: ensure apache is at the latest version
yum: name=httpd state=latest
- name: write the apache config file
template: src=/srv/httpd.j2 dest=/etc/httpd.conf
- hosts: databases
remote_user: root
tasks:
- name: ensure postgresql is at the latest version
yum: name=postgresql state=latest
- name: ensure that postgresql is started
service: name=postgresql state=started
For each play, we can choose what target hosts the tasks should be executed on.
---
- hosts: webservers
remote_user: root #who to do this as on remote system
Remote users can also be defined on a per-task basis
Ansible Playbooks (remote users)
Can become another user (on a play, or on individual task)
---
- hosts: webservers
remote_user: yourname
become: yes
become_user: fred #default value here is root
Ansible Playbooks (tasks)
Tasks will be executed sequentially against all the hosts indicated.
Within a play, all hosts will have same tasks
If a host fails a task, they are taken out of play for the remainder of playbook.
Modules should be idempotent, that is, running a module multiple times in a sequence should have the same effect as running it just once. (rerunning a playbook shouldn't hurt things)
Ansible Playbooks (example task)
tasks:
- name: make sure apache is running
service: name=httpd state=started
Ansible Playbooks (task handler)
A task handler will run when notified. A notification is sent when a previous task has caused a change.