CHAPTER 14
Sometimes we have the requirement of only running one specific task within a plethora of tasks configured in a playbook. In other words, instead of executing the playbook itself, we might choose to run only a part. Ansible enables this scenario with the tags attribute. Tags are annotations to the task that identify or group them with other tasks.
Let’s quickly see an example of how to configure a tag.
Code Listing 93: Playbook with tags
--- - name: Tags Playbook hosts: localhost gather_facts: true connection: local tasks: - name: Display information debug: msg: - "Distro of {{ ansible_facts['hostname'] }}: {{ ansible_facts['distribution'] }}" - "IP of {{ ansible_facts['hostname'] }}: {{ ansible_default_ipv4.address }}" tags: info
- name: Apply changes debug: msg: "Some changes executed" tags: execute - name: Post execution debug: msg: "Command executed successfully" tags: [info, execute]
- name: Never debug: msg: "This command has to be explicitly called" tags: [never, debug] |
Through the tags keyword, we have marked all of the tasks, de facto attaching a label to them. We can see that more than one tag can be assigned at the same time. As mentioned previously, this is very useful if we want to group certain tasks together, and sometimes one given task may belong to more than one group.
There are two special tags defined by Ansible: never and always. The never tag, if specified, will prevent the execution of the tasks, unless this is not explicitly specified to run. On the other hand, always is the default value of any tag.
When executing the playbook, we have a few possibilities on how to include or exclude certain tags from being executed
Table 12: Tag command line options
Example | Description |
|---|---|
ansible-playbook p.yml -t all | all is a special keyword that will run all the tasks (except tasks marked as never). |
ansible-playbook p.yml -t tagged | tagged is a special keyword that will run all of the tasks that have been explicitly tagged (at least one tag). |
ansible-playbook p.yml -t untagged | untagged is a special keyword that will run all of the tasks that have not been explicitly tagged (at least one tag). |
ansible-playbook p.yml -t "info, debug" | Executes tasks with multiple tags. |
ansible-playbook p.yml --skip-tags info | Runs all the tags, but not the one(s) specified. |
ansible-playbook p.yml -–list-tags | Lists all of the tags defined in the current playbook. |
ansible-playbook p.yml -t info -–list-tasks | Lists all the tasks that are tagged with the label info. |
Let’s see a few examples.
If we set the playbook.yml file to run only the tasks labeled info, only the tasks named Display information and Post execution will run, as shown in the following figure.

Figure 46: Running tasks tagged "info"
Instead, if we were to run everything but not tags with the info label, then only the Apply changes task would be executed.

Figure 47: Skipping all tasks tagged "info"
In both cases, we can see that the task named never was never executed. If we want to execute this task, too, we need to explicitly specify it when running the playbook.

Figure 48: Explicitly executing the “debug” and “execute” tasks