Updated March 31, 2023
Introduction to Ansible Tags
Ansible tags are used to run only one or some specific tasks from a large playbook instead of running the whole playbook. We use “tags:” attribute to use Ansible tags. For example, we have 3 tasks in a playbook, the first one is to install the package, the second one is to set the configuration and the third one is to check the status of the installed package like installed service is running and it is enabled, etc. however, we want to check only the status of the packages on some hosts, we use Ansible tags in this situation to run that specific tasks rather than running the all tasks in the playbook. It helps us to keep all the related tasks in one playbook and save the execution time as it provides the capability to run the specific task only.
Code:
- name: create multiple files from a location
file:
path: ‘/home/ansible/{{ item }}’
state: touch
loop:
- file1
- file2
tags:
- create_file
- name: delete multiple files from a locatoion
file:
path: ‘/home/ansible/{{ item }}’
state: absent
loop:
- file1
- file2
tags:
- delete_file
Output:
Code Explanation: In the above example, we have an Ansible playbook for file manipulation that is used to create the file, delete the file or modify the file. We have tags attached to each task as mentioned in the code snippet. We have tagged file creation task as ‘create_file’ and file deletion task as ‘delete_file’. So, when we have to create the files only we run only file creation task using –tags option while executing the playbook and mentioned the tag related to file creation here we have ‘create_file’.
Examples of Ansible Tags
We have two ways to filter out tasks based on tags while executing a playbook: –
- use –tags and –skip-tags options while executing the playbook on the command line and
- use TAGS_RUN and TAGS_SKIPS option in the configuration file.
1. use –tags and –skip-tags options
If we have more than one task needs to be executed at the same time, we use –tags as below: –
Syntax:
$ansible-playbook <name_of_the_playbook> --tags “<tag1>, <tag2>, <tag3>, ..”
Example
$ansible-playbook file_manipulation.yml --tags “create_file, delete_file”
Output:
Code Explanation: In the above example, we used two tags that executed two tasks.
2. use –skip-tags
If we have to execute all tasks but want to skip some tasks, we can use –skip-tags as below: –
Syntax:
$ansible-playbook <name_of_the_playbook> --skip-tags “<tag1>, <tag2>, <tag3>, ..”
Example
$ansible-playbook file_manipulation.yml --skip-tags “delete_file”
Output:
Code Explanation: In the above example, we wanted to skip the task tagged as delete_file so it has executed the remaining task that created the file as there are only two tasks in the playbook.
3. use –list-tasks with –tags or –skip-tags
If we want to confirm which tasks are going to execute if we use the certain tag, we use –list-tasks with –tags or –skip-tags as below: –
Syntax:
$ansible-playbook <name_of_the_playbook> --tags “<tag1>, <tag2>, <tag3>, ..” --list-tasks
Example
$ansible-playbook file_manipulation.yml --tags “create_file, delete_file” --list-tasks
Output:
We can also apply the same tag to multiple tasks, in that case, all tasks will get executed that have a similar tag. For example, whenever we create a new webserver we need to install nginx service, configure it and verify the status like service is started and it is enabled. We can give the same tag to those tasks so that whenever we have a new web server, we will execute the playbook with that tag and it will execute all the tasks.
Code:
---
- hosts: ansible_client.lab.com
become: yes
tasks:
- name: Install nginx
yum:
name: nginx
state: present
tags: nginx_service
- name: Check nginx service is running and enabled
service:
name: nginx
state: started
enabled: yes
tags: nginx_service
Output:
We also have some special tags like ‘always’, ‘never’ and ‘debug’. If we want to run one or more tasks each time playbook runs then we can tag those tasks with ‘always’ keyword. However, if we do not want to run those tasks due to some reason, we have to use ‘–skip-tags always’ to skip those tasks. In the same way, if you never want to run one or more tasks, tag those tasks with ‘never’ keyword. Similarly, if you want to run those tasks sometimes, we have to explicitly use ‘–tags never’. ‘debug’ keyword is mostly used to get verbose output of the playbook however we have to use the debug module while writing Ansible playbook. We tag those debug tasks with debug keyword so it will print the message on the stdout.
4. tag a task with multiple keywords using []
Also, we can tag a task with multiple keywords using []. Let’s understand these keywords with the debug module as shown in the below example: –
Code:
---
- hosts: ansible_client.lab.com
become: yes
tasks:
- debug:
msg: Always run this task
tags: always
- debug:
msg: Never run this task
tags: never
- debug:
msg: Unless debug or never is explicitly requested
tags: [never, debug]
Scenario 1 – Run the above playbook without any tags as below:
$ansible-playbook special_keyword.yml
Output:
Code Explanation: In the above example, no tags has been specified but a task with ‘always’ tag ran successfully.
Scenario 2 – Run the playbook with ‘–skip-tags always’ option as below:
$ansible-playbook special_keyword.yml --skip-tags always
Output:
Code Explanation: In the above example, we used the ‘–skip-tags’ option with ‘always’ hence task with ‘always’ tag did not run.
Scenario 3 – Run the playbook with ‘–tags never’ option as below: –
$ansible-playbook special_keyword.yml --tags never
Output:
Code Explanation: In the above example, all the tasks have been executed as we have explicitly requested to run tasks having ‘never’ keyword. The task with ‘always’ keyword ran because it has not skipped explicitly.
Other than that we have 3 more special keywords ‘tagged’, ‘untagged’ and ‘all’. If we want to only run the tasks which have any tag, we use ‘tagged’ keyword and if only want to run untagged tasks. Ansible uses ‘–tags all’ keyword by default.
Conclusion
Ansible tags are very useful when we have to run a part of Ansible playbook. It provides flexibility to run the playbook with special keywords. It provides better control of our playbook while execution. It can be inherited as well while writing the complex playbook.
Recommended Articles
This is a guide to Ansible Tags. Here we also discuss the Introduction and syntax of ansible tags along with different examples and its code implementation. You may also have a look at the following articles to learn more –