Ansible terminology - What is an Ansible Playbook?
A step-by-step guide inside the Ansible Playbook anatomy: play, tasks, modules, conditional, loop, handler, variable, list.


What is an Ansible Playbook?
I will show you a live demo with some simple Ansible code. I’m Luca Berton, and welcome to today’s episode of Ansible Pilot.
Ansible Playbook
- blueprint for automation
- YAML format
- Ansible language
The Ansible Playbook is the blueprint for your automation. The Ansible Playbook enables you to execute any, again again, operation in a specified order.
It’s like a recipe book for someone who likes to cook a cake. Every ingredient needs to be added in a specific order at a particular moment of the execution.
The code is human-readable in YAML format, a well-known easy-to-read coding format. Every line of coding is the Ansible language that is changed a little bit yearly, but the principles are always the same.
Like all programming languages, you can declare variables, include other files, execute actions on conditions, and repeat with loops.
Other particular actions, called handlers, execute only when another task is performed.
You can execute your Ansible Playbook using the ansible-playbook
command line utility included in any ansible installation.
When the execution is successful, you obtain a green result, otherwise a failure with a relative error.
Links
The Best Resources For Ansible
Video Course
Printed Book
eBooks
- Ansible by Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps
- Ansible For Windows By Examples: 50+ Automation Examples For Windows System Administrator And DevOps
- Ansible For Linux by Examples: 100+ Automation Examples For Linux System Administrator and DevOps
- Ansible Linux Filesystem By Examples: 40+ Automation Examples on Linux File and Directory Operation for Modern IT Infrastructure
- Ansible For Containers and Kubernetes By Examples: 20+ Automation Examples To Automate Containers, Kubernetes and OpenShift
- Ansible For Security by Examples: 100+ Automation Examples to Automate Security and Verify Compliance for IT Modern Infrastructure
- Ansible Tips and Tricks: 10+ Ansible Examples to Save Time and Automate More Tasks
- Ansible Linux Users & Groups By Examples: 20+ Automation Examples on Linux Users and Groups Operation for Modern IT Infrastructure
- Ansible For PostgreSQL by Examples: 10+ Examples To Automate Your PostgreSQL database
- Ansible For Amazon Web Services AWS By Examples: 10+ Examples To Automate Your AWS Modern Infrastructure
demo
A step-by-step guide inside the Ansible Playbook anatomy.
code
- example.yml
---
- name: example playbook
hosts: all
vars:
myvar: "example text"
mybool: true
cities:
- New York
- Paris
tasks:
- name: print var
ansible.builtin.debug:
var: myvar
notify: reload
- name: condition
ansible.builtin.debug:
msg: "example condition"
when: mybool
- name: print cities
ansible.builtin.debug:
var: item
loop: "{{ cities }}"
handlers:
- name: reload
ansible.builtin.debug:
msg: "example handler"
execution
$ ansible-playbook -i ../vmware/inventory example.yml
PLAY [example playbook] *****************************************************************
TASK [Gathering Facts] ******************************************************************
[WARNING]: Platform darwin on host localhost is using the discovered Python interpreter
at /opt/homebrew/bin/python3.10, but future installation of another Python interpreter
could change the meaning of that path. See https://docs.ansible.com/ansible-
core/2.13/reference_appendices/interpreter_discovery.html for more information.
ok: [localhost]
TASK [print var] ************************************************************************
ok: [localhost] => {
"myvar": "example text"
}
TASK [condition] ************************************************************************
ok: [localhost] => {
"msg": "example condition"
}
TASK [print cities] *********************************************************************
ok: [localhost] => (item=New York) => {
"ansible_loop_var": "item",
"item": "New York"
}
ok: [localhost] => (item=Paris) => {
"ansible_loop_var": "item",
"item": "Paris"
}
PLAY RECAP ******************************************************************************
localhost : ok=4 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Recap
Now you know what an Ansible Playbook is and how to use it. You know how to use it based on your use case.
Subscribe to the YouTube channel, Medium, Website, Twitter, and Substack to not miss the next episode of the Ansible Pilot.Academy
Learn the Ansible automation technology with some real-life examples in my
My book Ansible By Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps
Donate
Want to keep this project going? Please donate