You can tag almost every action in your plays. Here are some (contrived) examples:
---
- name: Get facts from your hosts
tags: configure
hosts: all
- name: Tell me all the variable data you've collected
tags: dump
hosts: localhost
tasks:
- name: Show data
tags: show
debug:
var=item
with_items: hostvars
When you then run
ansible-playbook test.yml --list-tags
You get
playbook: test.yml
play #1 (all): Get facts from your hosts TAGS: [configure]
TASK TAGS: [configure]
play #2 (localhost): Tell me all the variable data you've collected TAGS: [dump]
TASK TAGS: [dump, show]
Now you can run ansible-playbook test.yml -t configure
or ansible-playbook test.yml --skip-tags configure
To show how useful this can be, here’s the output from the “–list-tags” I’ve got on a project I’m doing at work:
playbook: site.yml
play #1 (localhost): Provision A-Side Infrastructure TAGS: [Functional_Testing,A_End]
TASK TAGS: [A_End, EXCLUDE_K5_FirewallManagers, EXCLUDE_K5_Firewalls, EXCLUDE_K5_Networks, EXCLUDE_K5_SecurityGroups, EXCLUDE_K5_Servers, Functional_Testing, K5_Auth, K5_FirewallManagers, K5_Firewalls, K5_InterProjectLinks, K5_Networks, K5_SecurityGroups, K5_Servers]
play #2 (localhost): Provision B-Side Infrastructure TAGS: [Functional_Testing,B_End]
TASK TAGS: [B_End, EXCLUDE_K5_Firewalls, EXCLUDE_K5_Networks, EXCLUDE_K5_SecurityGroups, EXCLUDE_K5_Servers, Functional_Testing, K5_Auth, K5_FirewallManagers, K5_Firewalls, K5_InterProjectLinks, K5_Networks, K5_SecurityGroups, K5_Servers]
play #3 (localhost): Provision InterProject Links - Part 1 TAGS: [Functional_Testing,InterProjectLink]
TASK TAGS: [EXCLUDE_K5_InterProjectLinks, Functional_Testing, InterProjectLink, K5_InterProjectLinks]
play #4 (localhost): Provision InterProject Links - Part 2 TAGS: [Functional_Testing,InterProjectLink]
TASK TAGS: [EXCLUDE_K5_InterProjectLinks, Functional_Testing, InterProjectLink, K5_InterProjectLinks]
play #5 (localhost): Provision TPT environment TAGS: [Performance_Testing]
TASK TAGS: [EXCLUDE_K5_FirewallManagers, EXCLUDE_K5_Firewalls, EXCLUDE_K5_Networks, EXCLUDE_K5_SecurityGroups, EXCLUDE_K5_Servers, K5_Auth, K5_FirewallManagers, K5_Firewalls, K5_InterProjectLinks, K5_Networks, K5_SecurityGroups, K5_Servers, Performance_Testing, debug]
This then means that if I get a build fail part-way through, or if I’m debugging just a particular part, I can run this: ansible-playbook site.yml -t Performance_Testing --skip-tags EXCLUDE_K5_Firewalls,EXCLUDE_K5_SecurityGroups,EXCLUDE_K5_Networks