Intern
What is the difference between 'when' and 'tags' in Ansible?
sobes.tech AI
Answer from AI
In Ansible, when and tags serve different purposes:
-
when — a condition for executing a task. The task will only run if the condition is true. It allows dynamic control based on variables, facts, etc.
-
tags — labels for grouping tasks. They enable running only tasks with certain tags or skipping others when executing a playbook.
Example:
- name: Install package
apt:
name: nginx
state: present
when: ansible_os_family == "Debian"
tags:
- webserver
Here, the task will only run on Debian systems (when), and when running the playbook, you can specify --tags webserver to execute only tasks with this tag.
In summary:
whencontrols task execution in a specific run.tagscontrol task selection for execution from the entire playbook.