Ansible Tag

Ansible Tag
Techiio-author
Written by Nilima PaulJanuary 21, 2022
10 min read
Ansible
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Techiio-author
Nilima Paul

Technology Security Analyst

We will know in this article, what's the Ansible Tag.

Introduction to Ansible Tags

Ansible labels are utilized to run just one or a few explicit assignments from an enormous playbook as opposed to running the entire playbook. We use the "labels:" characteristic to utilize Ansible labels. For instance, we have 3 assignments in a playbook, the first is to introduce the bundle, the subsequent one is to set the arrangement and the third one is to check the situation with the introduced bundle like introduced administration is running and it is empowered, and so forth notwithstanding, we need to check just the situation with the bundles on certain hosts, we utilize Ansible labels in the present circumstance to run that particular errands rather than running all undertakings in the playbook. It assists us with keeping every one of the connected assignments in a single playbook and save saves execution time as it gives us the ability to run the particular undertaking as it were.

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

Code Explanation: In the above model, we have an Ansible playbook for document control that is utilized to make the record, erase the record or adjust the record. We have labels connected to each undertaking as referenced in the code scrap. We have labeled the document creation task as 'create_file' and the record erasure task as 'delete_file'. Along these lines, when we need to make the documents just we run just record creation task utilizing - labels choice while executing the playbook and referenced the label connected with record 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 options 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”

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”

Code Explanation: In the above model, we needed to avoid the undertaking labeled as delete_file so it has executed the excess errand that made the record as there are just two assignments 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

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­­­­

We additionally have a few exceptional labels like 'consistently', 'never', and 'troubleshoot'. To run at least one errands each time playbook runs then we can label those assignments with a 'consistently' catchphrase. Be that as it may, to run those errands because of some explanation, we need to utilize '- skip-labels generally are to skirt those undertakings. Similarly, to run at least one undertaking, label those errands with a 'never' catchphrase. Essentially, to run those undertakings here and there, we need to unequivocally utilize '- labels never'. 'troubleshoot' watchword is generally used to get the verbose result of the playbook anyway we need to utilize the investigate module while composing Ansible playbook. We label those investigate undertakings with troubleshooting watchword 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 debugging or never is explicitly requested
tags: [never, debug]

Scenario 1 – Run the above playbook without any tags as below:

$ansible-playbook special_keyword.yml

Code Explanation: In the above example, no tags have been specified but a task with the ‘always’ tag ran successfully.

Scenario 2 – Run the playbook with the ‘–skip-tags always’ option as below:

$ansible-playbook special_keyword.yml --skip-tags always

Code Explanation: In the above example, we used the ‘–skip tags option with ‘always’ hence task with the ‘always’ tag did not run.

Scenario 3 – Run the playbook with the ‘–tags never’ option as below: –

$ansible-playbook special_keyword.yml --tags never

Code Explanation: In the above model, every one of the assignments have been executed as we have unequivocally mentioned running errands having 'never' catchphrase. The undertaking with 'consistently' watchword ran because it has not skipped expressly.

Other than that we have 3 additional unique watchwords 'labeled', 'untagged' what not'. To just run the errands which have any tag, we use 'labeled' watchword and if by some stroke of good luck need to run untagged assignments. Ansible utilizations '- labels all' watchword naturally.

Conclusion

Ansible labels are exceptionally helpful when we need to run a piece of Ansible playbook. It furnishes adaptability to run the playbook with unique catchphrases. It gives better control of our playbook while executing. It tends to be acquired too while composing the intricate playbook.

Ansible
Ansible Tages
Python
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-author
Nilima Paul
Technology Security Analyst
Techiio-followerTechiio-followerTechiio-follower
201 Blog Posts
0 Discussion Threads
Trending Technologies
15
Software40
DevOps46
Frontend Development24
Backend Development20
Server Administration17
Linux Administration26
Data Center24
Sentry24
Terraform23
Ansible83
Docker70
Penetration Testing16
Kubernetes21
NGINX20
JenkinsX17
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance