Sobes.tech
Junior — Middle

What are the main components included in an Ansible playbook?

sobes.tech AI

Answer from AI

Ansible playbook consists of several main parts:

  • hosts — defines the group or specific hosts where tasks will be executed.
  • vars — variables used for parameterization.
  • tasks — list of tasks to perform on target hosts, usually using specific Ansible modules.
  • handlers — special tasks triggered by events, e.g., restarting a service upon configuration change.
  • roles — structuring method to group tasks, variables, and files for reuse.

Example of a minimal playbook:

- hosts: webservers
  vars:
    http_port: 80
  tasks:
    - name: Install nginx
      apt:
        name: nginx
        state: present
    - name: Start nginx
      service:
        name: nginx
        state: started
  handlers:
    - name: Restart nginx
      service:
        name: nginx
        state: restarted