Run an Ansible Playbook With Tags

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
    - name: Update apt cache 
       update_cache: yes
        - update_cache 
    - name: Upgrade all packages on servers
        name: "*"
        state: latest
       - 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

Success! You're on the list.