Tags are another
feature in Ansible that is used to run only a subset of tasks/roles. When we
have tasks in a playbook and we executed the playbook then all tasks are
executed. Byt by using the tags we can make the playbook to execute only a subset
of tasks by defining them with tag attribute. In this article we will see how
we can create tags and use them to execute a subset of tasks in playbook.
1) Lets create a
basic sample playbook as
[root@vx111a tags]#
cat sample-playbook.yml
---
- hosts: cent
tasks:
- name: run echo Command
command: /bin/echo Hello Sample PlayBook
tags:
- sample
- name: Create Sub Directories
file:
dest: "/tmp/html"
state: directory
mode: 755
tags:
-
create
If you check the
above example, I have defined 2 tasks defining them with 2 different tags. Now
when we execute the playbook all the tasks are executed but if we want only a
specific tag to be execute we can run the playbook as,
[root@vx111a tags]#
ansible-playbook sample-playbook.yml --tags sample
PLAY [cent]
*******************************************************************
GATHERING FACTS
***************************************************************
ok: [172.16.202.96]
TASK: [run echo
Command] ******************************************************
changed:
[172.16.202.96]
PLAY RECAP
********************************************************************
172.16.202.96 : ok=2 changed=1
unreachable=0 failed=0
In the above
example we ran only the sample tag by passing the argument –tags sample to the
command line. In the below example I ran only the create tag.
[root@vx111a tags]#
ansible-playbook sample-playbook.yml --tags create
PLAY [cent]
*******************************************************************
GATHERING FACTS
***************************************************************
ok: [172.16.202.96]
TASK: [Create Sub
Directories] ************************************************
changed:
[172.16.202.96]
PLAY RECAP
********************************************************************
172.16.202.96 : ok=2 changed=1
unreachable=0 failed=0
We can add the
register attribute so that we can see what exactly changing. By default ansible
runs as if ‘–tags all’ had been specified.This about the tags in Ansible. More
to come. Happy learning
simple and clear.Keep updating Devops Online Training
ReplyDelete