Case Study on Tags
Akshay Suruse
Certified Kubernetes Administrator | AWS Solutions Architect | Devops | Linux Administration | Docker | Kubernetes | RHCSA | RHCE | Ansible |
Sometimes we have large playbook, but we want to run only specific parts of it instead of running the entire playbook, you can do this with Ansible Tags. Using this its a two-step verification process either to execute or skip selected tasks.
Here is the Two Step Verification Process: 1. Add tags to tasks, either individually or with inheritance from block, play, role, or import. 2. Select or skip tags while running your playbook.
Ansible-playbook offers five tag-related command-line options:
- --tags all - run all tasks, ignore tags (default behavior)
- --tags [tag1, tag2] - run only tasks with the tags tag1 and tag2
- --skip-tags [tag3, tag4] - run all tasks except those with the tags tag3 and tag4
- --tags tagged - run only tasks with at least one tag
- --tags untagged - run only tasks with no tags
Ansible reserves two tag name for special behavior: Always and Never. If we assign the always in playbook, ansible will always run that task unless we specifically skip it (--skip-tags always). If we assign that the never in playbook, ansible will skip that task unless we specifically request it (--tags never)