Sobes.tech
Junior — Middle

Can you list the key components of an Ansible playbook and their roles?

sobes.tech AI

Answer from AI

Ansible playbook consists of several key components:

  • hosts — defines on which hosts or groups of hosts the playbook will be executed.
  • tasks — a list of tasks to perform. Each task uses Ansible modules to perform specific actions.
  • vars — variables that can be used within the playbook for parameterization.
  • handlers — special tasks that are triggered upon notification (notify) from other tasks, for example, to restart services.
  • roles — a way to structure the playbook by grouping tasks, variables, and files into separate modules 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
  handlers:
    - name: Restart nginx
      service:
        name: nginx
        state: restarted