This project is maintained by p-avery
A playbook is where you list the steps you would like to automate into a repeatable set of plays and tasks. It is recommended that you store playbooks in a source control management (SCM) system so you will have version control and branches assigned.
A playbook can have multiple plays and a play can have one or multiple tasks. The goal of a play is to map a group of hosts. The goal of a task is to implement modules against those hosts.
Step 1:
We will be using our playbook that includes all the ad-hoc commands we just ran, this will demonstrate what we just did, However; We can see how the individual adhoc jobs are now put into a list of tasks.
---
- name: Playbook Demo
hosts: web
tasks:
- name: ping the hosts
ping:
- name: Ensure Apache is installed
yum:
name: httpd
state: present
become: true
- name: Ensure Apache is running
service:
name: httpd
state: started
enabled: true
become: true
- name: Get a list of all services
service_facts:
- name: Read the redhat-release file
shell: cat /etc/redhat-release
check_mode: no
changed_when: false
This is the entire playbook
Breaking the playbook down we can see to start with the play we specify our target audience
---
- name: Playbook Demo
hosts: web
--- Defines the beginning of YAML
name: Playbook Demo This describes our play
hosts: web Defines the host group in your inventory on which this play will run against
The hosts is our target and is set to web, this is the same group name we used in the ad-host command, this value could be as global as
hosts: all
or can be a group or node name and it can include multiple if needed.
hosts: node1,node2,node3
Section 3: Adding Tasks to a Play ====================================
Now that playbook has been defined, We need to add some tasks to get some things done.
tasks:
- name: Ping the hosts
ping:
- name: Ensure Apache is installed
yum:
name: httpd
state: present
become: true
- name: Ensure Apache is running
service:
name: httpd
state: started
enabled: true
become: true
- name: Get list of all services on box
service_facts:
- name: Read the redhat-release file
shell: cat /etc/redhat-release
check_mode: no
changed_when: false
tasks: This denotes that one or more tasks are about to be defined
- name: Each task requires a name which will print to standard output when you run your playbook. Therefore, give your tasks a name that is short, sweet, and to the point
- name: Ping the hosts
ping:
- name: ensure Apache is installed
yum:
name: httpd
state: present
become: true
yum to install the Apache Web Server package, we have also stated become is set to true this will elevate our permissions upto a root account which is the same as when we checked the Enable Priviledge Escalation box in the Ad-Hoc command - name: ensure Apache is running
service:
name: httpd
state: started
enabled: true
become: true
- name: get list of all services on box
service_facts:
- name: read the redhat-release file
shell: cat /etc/redhat-release
check_mode: no
changed_when: false
check_mode: no
changed_when: false
Note
Ansible (well, YAML really) can be a bit particular about formatting especially around indentation/spacing. When you get back to the office, read up on this YAML Syntax a bit more and it will save you some headaches later.