To run specific part or exclude specific part in a playbook we an use tags attrubute.
Here is my example playbook with name tags_example.yml:
---
- hosts: all
tasks:
- name: Hello
shell: "echo hello"
tags:
- hello
- name: Bye
shell: "echo bye"
tags:
- bye
In above playbook we have two tasks Hello and Bye with tags hello and bye tags respectively.
To execute above playbook use following command:
$ ansible-playbook yml/tags_example.yml
Above command will execute both tasks Hello and Bye respectively.
Now lets see how to execute specific part of play book.
We can use --tags "<tagName>"
argument with ansible-playbook to execute only specific tasks and use --skip-tasks "<tagName>"
to skip specific tasks from execution.
To execute all tasks with hello tag use following command:
$ ansible-playbook yml/tags_example.yml --tags "hello"
To skip all tasks with hello tag use following command:
$ ansible-playbook yml/tags_example.yml --skip-tags "hello"
To execute multiple tags use following command:
$ ansible-playbook yml/tags_example.yml --tags "hello,bye"
To skip multiple tags use following command:
$ ansible-playbook yml/tags_example.yml --skip-tags "hello,bye"
I hope this will helps to understand tags concept in Ansible.