Junior — Middle
What components and structures do you use when writing Ansible playbooks?
sobes.tech AI
Answer from AI
The following components and constructs are typically used when writing Ansible playbooks:
- Tasks — individual actions performed on hosts.
- Modules — built-in or third-party tools for executing specific operations (e.g., copy, yum, service).
- Variables — for storing data that can be used within the playbook.
- Handlers — tasks that are executed when notified, usually for restarting services.
- Conditions (when) — allow tasks to be executed only if certain conditions are met.
- Loops — for repeating tasks over a list of items.
- Roles — structured sets of tasks, files, and variables for reuse.
Example of a simple task with a condition and a loop:
- name: Install packages
yum:
name: "{{ item }}"
state: present
loop:
- httpd
- git
when: ansible_os_family == "RedHat"