Introduction to Ansible Tags

Sometimes, it is useful to invoke only parts of a role or a playbook. For example, you need to add a new user to the system but you don't want to run the whole role or playbook for that.

With Ansible, it is possible to do so by using something known as tags.

- name: install httpd
  yum: name=httpd state=installed

- name: install php and libs
  yum: name={{ item }} state=installed
  with_items:
    - php-xml
    - php
    - php-zip
    - php-mbstring
    - php-mysqlnd
    - php-gd
  tags:
    - php

We have put a tag php on the task that was installing php packages. Now we can invoke the relevant playbook to run the task that has the right tag.

$ ansible-playbook -i inventory -t php playbooks/owncloud.yml

A task can have multiple tags. And same tag can be applied to many tasks.