>_ IzzyLab

Hands-on task

This task is step 7 of the course “Terraform and Ansible”

Ansible: run by tags

medium~15 min

Write a playbook /root/tagged.yml with two tasks: one tagged deploy (creates /tmp/deployed), the other tagged cleanup (creates /tmp/cleaned). Run it with the deploy tag only, so that just the first task is applied. Each tag must run only its own task — both deploy and cleanup.

What you'll practise:
  • ansible
  • tags
  • --tags
  • file module
  • selective run

Topic breakdown

Tags in Ansible let you run only selected tasks instead of the whole playbook: each task is given tags, and at run time the --tags filter keeps only the tagged ones. This is handy when a single playbook holds different operations — a deploy and a cleanup, say — and they need to be run separately. Without a filter both tasks run, so the whole point of the task is to run the playbook strictly with the required tag.

What's checked

  1. --tags deploy runs only the deploy task
  2. --tags cleanup runs only the cleanup task

What it looks like