# specific task is not executing by using tags in roles

**URL:** <https://forum.ansible.com/t/specific-task-is-not-executing-by-using-tags-in-roles/34521>\
**Category:** Ansible Project\
**Created:** [January 27, 2021, 12:19pm UTC](https://forum.ansible.com/t/specific-task-is-not-executing-by-using-tags-in-roles/34521 "2021-01-27T12:19:01Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ramesh\_AR](https://avatars.discourse-cdn.com/v4/letter/r/3e96dc/32.png) [@Ramesh\_AR](https://forum.ansible.com/u/Ramesh_AR)\
**Post date:** [January 27, 2021, 12:19pm UTC](https://forum.ansible.com/t/specific-task-is-not-executing-by-using-tags-in-roles/34521/1 "2021-01-27T12:19:01Z")

</div>

when I defined tags in main.yml and roles, i couldn’t able to execute specific tags by using --tags or --skip-tags…

when I run the below with --tags=tag1 , I see whole playbook has executing  
I have defined a task ( 127.0.0.1 ) in role, that’s also not working after role executed

The below are my tags,

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [January 27, 2021, 3:50pm UTC](https://forum.ansible.com/t/specific-task-is-not-executing-by-using-tags-in-roles/34521/2 "2021-01-27T15:50:22Z")

</div>

tags: on role lis applying those tags to ALL tasks, not 'selecting  
which tags to run'

so you need to use ALL of the tags in --tags to run those tasks.

---

<div class="post-metadata">

**Author:** ![Ramesh\_AR](https://avatars.discourse-cdn.com/v4/letter/r/3e96dc/32.png) [@Ramesh\_AR](https://forum.ansible.com/u/Ramesh_AR)\
**Post date:** [January 27, 2021, 5:05pm UTC](https://forum.ansible.com/t/specific-task-is-not-executing-by-using-tags-in-roles/34521/3 "2021-01-27T17:05:21Z")

</div>

Thank you Brian. yes after remove tag from role specific task works fine ! but localhost task executes fails …it worked till prompt syntax…

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [January 27, 2021, 5:09pm UTC](https://forum.ansible.com/t/specific-task-is-not-executing-by-using-tags-in-roles/34521/4 "2021-01-27T17:09:50Z")

</div>

That seems unrelated (no tags?) also .. without an error its hard to  
know what "doesn't work" means.

---

<div class="post-metadata">

**Author:** ![Ramesh\_AR](https://avatars.discourse-cdn.com/v4/letter/r/3e96dc/32.png) [@Ramesh\_AR](https://forum.ansible.com/u/Ramesh_AR)\
**Post date:** [January 27, 2021, 5:54pm UTC](https://forum.ansible.com/t/specific-task-is-not-executing-by-using-tags-in-roles/34521/5 "2021-01-27T17:54:45Z")

</div>

Ok here’s the playbook and output . and am using only --tags=tag1 but am getting all output as mentioned ( with\_items ) … since tag1 executing should show only tag1 correct

$ validation.yml

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [January 27, 2021, 6:48pm UTC](https://forum.ansible.com/t/specific-task-is-not-executing-by-using-tags-in-roles/34521/6 "2021-01-27T18:48:12Z")

</div>

Yes, this is correct.  
There is a list of 5 tags on this task, using any single one with  
"ansible-playbook --tags" will cause the entire task to run - which  
includes a loop.  
You can't use --tags on ansible-playbook to limit the items in a loop on a task.

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [January 27, 2021, 7:29pm UTC](https://forum.ansible.com/t/specific-task-is-not-executing-by-using-tags-in-roles/34521/7 "2021-01-27T19:29:37Z")

</div>

> > Ok here's the playbook and output . and am using only --tags=tag1 but am getting all output as mentioned ( with\_items ) .. since tag1 executing should show only tag1 correct
> > 
> > $ validation.yml  
> > ---  
> > - hosts: hosts  
> > &nbsp;&nbsp;connection: local  
> > &nbsp;&nbsp;gather\_facts: no  
> > &nbsp;&nbsp;roles:  
> > &nbsp;&nbsp;&nbsp;&nbsp;- role: validation
> > 
> > tasks:  
> > &nbsp;&nbsp;- name: Comment on ticket  
> > &nbsp;&nbsp;&nbsp;&nbsp;tags: ['tag1', 'tag2', 'tag3',' tag4', 'tag5']  
> > &nbsp;&nbsp;&nbsp;&nbsp;jira:  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;uri: '[https://ccp.sys.comcast.net](https://ccp.sys.comcast.net)'  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;username: "{{ username }}"  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;password: "{{ password }}"  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;issue: "{{ issue }}"  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;operation: comment  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;comment: "\*{{ item.title }}\* \n \n {{ lookup('file', '/tmp/{{ item.file }}') }}"  
> > &nbsp;&nbsp;&nbsp;&nbsp;with\_items:  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- { title: tag1 created, file: tag1.txt }  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- { title: tag2 created, file: tag2.txt }  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- { title: tag3 created, file: tag3.txt }  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- { title: tag4 created, file: tag4.txt }  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- { title: tag5 created, file: tag5.txt }
> 
> Yes, this is correct.  
> There is a list of 5 tags on this task, using any single one with  
> "ansible-playbook --tags" will cause the entire task to run - which  
> includes a loop.  
> You can't use --tags on ansible-playbook to limit the items in a loop on a task.

You can try though the following condition to run the matching items from your list:

&nbsp;&nbsp;&nbsp;when: item.title.split()[0] in ansible\_run\_tags

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Ramesh\_AR](https://avatars.discourse-cdn.com/v4/letter/r/3e96dc/32.png) [@Ramesh\_AR](https://forum.ansible.com/u/Ramesh_AR)\
**Post date:** [January 27, 2021, 7:49pm UTC](https://forum.ansible.com/t/specific-task-is-not-executing-by-using-tags-in-roles/34521/8 "2021-01-27T19:49:45Z")

</div>

Racke,

No, still its going through all tags … not working on specific tag in loop

Thanks,  
Ramesh

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [January 27, 2021, 9:00pm UTC](https://forum.ansible.com/t/specific-task-is-not-executing-by-using-tags-in-roles/34521/9 "2021-01-27T21:00:11Z")

</div>

> Racke,
> 
> No, still its going through all tags .. not working on specific tag in loop

You are sure?

My task:

&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "{{ item.title.split()[0] }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: item.title.split()[0] in ansible\_run\_tags  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with\_items:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- { title: tag1 created, file: tag1.txt }  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- { title: tag2 created, file: tag2.txt }  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- { title: tag3 created, file: tag3.txt }  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- { title: tag4 created, file: tag4.txt }  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- { title: tag5 created, file: tag5.txt }  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tags: ['tag1', 'tag2', 'tag3','tag4', 'tag5']

Executed with --tags=tag4:

TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
skipping: [buster-test-box] =\> (item={'title': 'tag1 created', 'file': 'tag1.txt'})  
skipping: [buster-test-box] =\> (item={'title': 'tag2 created', 'file': 'tag2.txt'})  
skipping: [buster-test-box] =\> (item={'title': 'tag3 created', 'file': 'tag3.txt'})  
ok: [buster-test-box] =\> (item={'title': 'tag4 created', 'file': 'tag4.txt'}) =\>  
&nbsp;&nbsp;msg: tag4  
skipping: [buster-test-box] =\> (item={'title': 'tag5 created', 'file': 'tag5.txt'})

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Ramesh\_AR](https://avatars.discourse-cdn.com/v4/letter/r/3e96dc/32.png) [@Ramesh\_AR](https://forum.ansible.com/u/Ramesh_AR)\
**Post date:** [January 27, 2021, 10:45pm UTC](https://forum.ansible.com/t/specific-task-is-not-executing-by-using-tags-in-roles/34521/10 "2021-01-27T22:45:29Z")

</div>

Not sure, what’s the problem. I see all skipping …

tasks:

- name: run a task  
jira:  
uri: ‘[https://xxxx](https://xxxx)’  
username: “{{ username }}”  
password: “{{ password }}”  
issue: “{{ issue }}”  
operation: comment  
comment: “_{{ item.title }}_ \n \n {{ lookup(‘file’, ‘/tmp/{{ item.file }}’) }}”
- debug:  
msg: “{{ item.title.split()[0] }}”  
when: item.title.split()[0] in ansible\_run\_tags  
with\_items:
- { title: tag1 created, file: tag1.txt }
- { title: tag2 created , file: tag2.txt }
- { title: tag3 created, file: tag3.txt }
- { title: tag4 created, file: tag4.txt }
- { title: tag5 created, file: tag5txt }  
tags: [‘tag1’, ‘tag2’, ‘tag3’, ‘tag4’, ‘tag5’]

executed for tag5

PLAY [127.0.0.1] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
skipping: [localhost] =\> (item={‘title’: ‘tag1 created’, ‘file’: ‘tag1.txt’})  
skipping: [localhost] =\> (item={‘title’: ‘tag2 created’, ‘file’: ‘tag2.txt’})  
skipping: [localhost] =\> (item={‘title’: ‘tag3 created’, ‘file’: ‘tag3.txt’})  
skipping: [localhost] =\> (item={‘title’: ‘tag4 created’, ‘file’: ‘tag4.txt’})  
skipping: [localhost] =\> (item={‘title’: ‘tag5 created’, ‘file’: ‘tag5.txt’})  
skipping: [localhost]

PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
host1 : ok=4 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0  
localhost : ok=0 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [January 28, 2021, 2:12am UTC](https://forum.ansible.com/t/specific-task-is-not-executing-by-using-tags-in-roles/34521/11 "2021-01-28T02:12:42Z")

</div>

> Not sure, what's the problem. I see all skipping ..

Sometimes it's possible to spot a problem immediately. Unfortunately,  
this is not the case. Loop No.10 and still nothing. The resolution of  
this problem needs more effort.

When in trouble, minimise the code and make it both complete and  
reproducible. See "Minimal working example"  
[https://en.wikipedia.org/wiki/Minimal\_working\_example](https://en.wikipedia.org/wiki/Minimal_working_example)

For example

&nbsp;&nbsp;- hosts: localhost  
&nbsp;&nbsp;&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var: ansible\_run\_tags  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tags: always  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "{{ item.title.split().0 }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: item.title.split().0 in ansible\_run\_tags  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- title: tag1 created  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- title: tag2 created  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- title: tag3 created  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tags: [tag1, tag2, tag3]

&nbsp;&nbsp;\> ansible-playbook pb.yml -t tag2

&nbsp;&nbsp;PLAY [localhost] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

&nbsp;&nbsp;TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
&nbsp;&nbsp;ok: [localhost] =\>  
&nbsp;&nbsp;&nbsp;&nbsp;ansible\_run\_tags:  
&nbsp;&nbsp;&nbsp;&nbsp;- tag2

&nbsp;&nbsp;TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
&nbsp;&nbsp;skipping: [localhost] =\> (item={'title': 'tag1 created'})  
&nbsp;&nbsp;ok: [localhost] =\> (item={'title': 'tag2 created'}) =\>  
&nbsp;&nbsp;&nbsp;&nbsp;msg: tag2  
&nbsp;&nbsp;skipping: [localhost] =\> (item={'title': 'tag3 created'})

Proceed step by step adding complexity until you find what's causing  
the problem. Then post minimal, complete, and reproducible details if  
you can't fix it.

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [January 28, 2021, 4:52am UTC](https://forum.ansible.com/t/specific-task-is-not-executing-by-using-tags-in-roles/34521/12 "2021-01-28T04:52:18Z")

</div>

This is not clear.  
What is the _exact_ command you invoke here?
