Junior — Middle
Explain how the playbook file structure in Ansible is organized and how it is used for task automation.
sobes.tech AI
Answer from AI
A playbook in Ansible is a YAML file that describes a set of tasks for automating configuration and managing infrastructure.
The structure of a playbook typically includes:
- hosts: defines on which hosts the tasks will be executed.
- vars: variables used in the playbook.
- tasks: a list of tasks to perform, such as installing packages, copying files, running commands.
- handlers: special tasks that are triggered upon changes (e.g., restarting a service).
- roles: logical grouping of tasks and configurations for reuse.
Example of a simple playbook:
- hosts: webservers
vars:
http_port: 80
tasks:
- name: Install nginx
apt:
name: nginx
state: present
- name: Start nginx
service:
name: nginx
state: started
A playbook is used for sequential and repeatable execution of tasks on remote servers, which simplifies infrastructure management and reduces the likelihood of errors during manual administration.