Sobes.tech
Junior — Middle

Explain what task conditions are in an Ansible playbook and how they are applied

sobes.tech AI

Answer from AI

Conditional execution (when) in Ansible playbooks allows tasks to run only when certain conditions are met. This helps manage execution logic and avoid unnecessary operations.

For example, a task can be executed only if a variable has a specific value or if the host belongs to a certain group.

Example:

- name: Install package only on Ubuntu
  apt:
    name: nginx
    state: present
  when: ansible_facts['os_family'] == 'Debian'

Here, the nginx installation task will only run on Debian family systems (e.g., Ubuntu). This increases the flexibility and adaptability of the playbook.