# Running a task only when tag defined

**URL:** <https://forum.ansible.com/t/running-a-task-only-when-tag-defined/22476>\
**Category:** Ansible Project\
**Created:** [September 3, 2016, 5:27pm UTC](https://forum.ansible.com/t/running-a-task-only-when-tag-defined/22476 "2016-09-03T17:27:18Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jon\_Langemak](https://avatars.discourse-cdn.com/v4/letter/j/ac91a4/32.png) [@Jon\_Langemak](https://forum.ansible.com/u/Jon_Langemak)\
**Post date:** [September 3, 2016, 5:27pm UTC](https://forum.ansible.com/t/running-a-task-only-when-tag-defined/22476/1 "2016-09-03T17:27:18Z")

</div>

I want to be able to run a task only when I pass a tag to the playbook. Namely, I want to have a tag that allows for decoming a user and one would normally never run that as part of normal playbook run.

I found the conditional ‘what: tags is defined’ but it doesnt seem to work. My task looks like this…

- name: decom user

shell: echo test

when: tag is defined

tags: decom

When I run it, I get this in the verbose output…

TASK [vyatta : decom user] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

skipping: [10.10.10.54] =\> {“changed”: false, “skip\_reason”: “Conditional check failed”, “skipped”: true}

I cant sort out why this is failing. Im passing the tag as part of the run…

ansible-playbook test.yml -t decom -v

Any ideas? Thanks!

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [September 3, 2016, 11:02pm UTC](https://forum.ansible.com/t/running-a-task-only-when-tag-defined/22476/2 "2016-09-03T23:02:01Z")

</div>

> I want to be able to run a task only when I pass a tag to the playbook.  
> Namely, I want to have a tag that allows for decoming a user and one would  
> normally never run that as part of normal playbook run.

To my knowledge this is not possible to do with tags.  
But you can do it with variables.

> I found the conditional 'what: tags is defined' but it doesnt seem to work.  
> My task looks like this...
> 
> - name: decom user  
> &nbsp;&nbsp;shell: echo test  
> &nbsp;&nbsp;when: tag is defined

This will check if the variable tag is defined, since it's not it will skip the task.

> &nbsp;&nbsp;tags: decom
> 
> When I run it, I get this in the verbose output...
> 
> TASK [vyatta : decom user]  
> \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
> 
> skipping: [10.10.10.54] =\> {"changed": false, "skip\_reason": "Conditional  
> check failed", "skipped": true}
> 
> I cant sort out why this is failing. Im passing the tag as part of the  
> run...
> 
> ansible-playbook test.yml -t decom -v
> 
> Any ideas? Thanks!

Instead of tags you can use variable.

&nbsp;&nbsp;&nbsp;- name: decom user  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;debug: msg="Yay I'm running"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: decom is defined

You can set the variable at run time like this

ansible-playbook test.yml -e "decom=true"
