Senior
Using what mechanism in Ansible can you split runs and not execute some tasks during a regular run?
sobes.tech AI
Answer from AI
In Ansible, the mechanism of tags is used to separate runs and exclude certain tasks during a normal execution. Tags allow marking tasks, blocks, or roles, and then running the playbook specifying which tags to execute or skip.
For example, if you have tasks with tags setup, deploy, and cleanup, you can run only deploy during a normal execution:
ansible-playbook site.yml --tags deploy
Or, conversely, exclude tasks with the cleanup tag:
ansible-playbook site.yml --skip-tags cleanup
This is convenient for separating heavy or rarely needed operations from the standard deployment process.
Example in a playbook:
- name: Install packages
apt:
name: nginx
state: present
tags:
- setup
- name: Deploy application
copy:
src: app/
dest: /var/www/app/
tags:
- deploy
- name: Clean temporary files
file:
path: /tmp/app_cache
state: absent
tags:
- cleanup
Thus, tags can be used to flexibly manage task execution.