How to create Playbooks in Ansible for an easy way

Playbooks

Each playbook is composed of one or more ‘modules’ in a list All sections begin with “-” & its attributes & parameters beneath it Indentation is imp, use only spaces & not tabs

Sample Playbooks

#Every YAML file starts with ---
---
- hosts: webservers
vars: http_port: 80
max_clients: 200
remote_user: root
tasks:
-name: ensure apache is at the latest version
apt: 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

Writing Playbooks

#Generate the SSH Key and connect hosts to control
machine before writing and running playbooks.
#Create a Playbook
$ vi <name of your file>.yml
#To write the playbook refer to the snapshot here.
#Run the playbook
$ ansible-playbook <name of your file>.yml

Leave a Reply

Your email address will not be published.