This blog post will show you how to use Ansible tags with Ansible playbooks and run a subset of tasks in a playbook.
Ansible tags allow us to tag tasks \ plays inside a playbook and run a specific part of the playbook and not the entire playbook. Using tags, you can run a subset of the task inside a playbook and skip everything else.
To get started with tags the tasks we would like to run, below specifically, I am tagging two tasks inside my playbook. Note that you can add multiple tags to a task.
--- - hosts: servers tasks: - name: Update apt cache apt: update_cache: yes tags: - update_cache - name: Upgrade all packages on servers apt: name: "*" state: latest tags: - upgrade_all
After the tasks are tagged, I can go ahead and run a single task from the entire playbook, as shown below.
ansible-playbook playbooks/install-updates.yml --tags update_cache -i hosts
To run multiple tags, I can use the following command.
ansible-playbook playbooks/install-updates.yml --tags "update_cache,upgrade_all" -i hosts