# how to use ansible tags with conditional includes?

**URL:** <https://forum.ansible.com/t/how-to-use-ansible-tags-with-conditional-includes/33052>\
**Category:** Ansible Project\
**Created:** [May 29, 2020, 3:24pm UTC](https://forum.ansible.com/t/how-to-use-ansible-tags-with-conditional-includes/33052 "2020-05-29T15:24:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tony\_Walsh](https://avatars.discourse-cdn.com/v4/letter/t/e8c25b/32.png) [@Tony\_Walsh](https://forum.ansible.com/u/Tony_Walsh)\
**Post date:** [May 29, 2020, 3:24pm UTC](https://forum.ansible.com/t/how-to-use-ansible-tags-with-conditional-includes/33052/1 "2020-05-29T15:24:28Z")

</div>

Hey folks,

I have run into a problem that I have not been able to solve – I hope somebody out there has had better luck than I have. Here’s the setup:

At the top level directory there is an executive shell script that calls a playbook, the particular playbook called is based on inputs to the script. For each playbook there is a corresponding role subdirectory, and each role subdirectory is further divided into a tasks subdirectory and a vars subdirectory. There is one vars file corresponding to each task file. Each role contains a set of unique tasks. If a task is used in more than one playbook then the tasks and vars files are located under the common subdirectory.

The structure of my current ansible hierarchy is shown below. Note that the common subdirectory and the compA and compB subdirectories look similar to the compN subdirectory, but only the compN subdirectory is shown in detail for simplification.

top-level directory:

compA\_playbook.yaml

compB\_playbook.yaml

…

compN\_playbook.yaml

executive.sh

roles

> - common

> - compA

> - compB

…

> - compN

> - tasks

> task1.yaml

> task2.yaml

> …

> taskm.yaml

> - vars

task1.yaml

task2.yaml

…

taskm.yaml

A typical playbook looks like this:

---

<div class="post-metadata">

**Author:** ![Tony\_Walsh](https://avatars.discourse-cdn.com/v4/letter/t/e8c25b/32.png) [@Tony\_Walsh](https://forum.ansible.com/u/Tony_Walsh)\
**Post date:** [June 2, 2020, 4:46pm UTC](https://forum.ansible.com/t/how-to-use-ansible-tags-with-conditional-includes/33052/2 "2020-06-02T16:46:19Z")

</div>

well I guess the number of replies says it all: I must be trying to do something the language does not support. Ansible conditions operate on “include\_vars” - include\* statements in ansible are dynamic. Similarly, import\* statements in ansible are static. Tags are applicable to statically imported constructs; tags are not applicable to dynamic inclusions such as include\_roles or include\_tasks.

Therefore, since ansible conditionals are applicable to dynamic constructs, and ansible tags are applicable to static constructs; the two are mutually exclusive. The language does not support the ability to tag variables that are included via include\_vars. Bummer.

This begs the question: how does one conditionally include vars files in tagged tasks in ansible?? Anybody?

---

<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:** [June 2, 2020, 7:01pm UTC](https://forum.ansible.com/t/how-to-use-ansible-tags-with-conditional-includes/33052/3 "2020-06-02T19:01:39Z")

</div>

> well I guess the number of replies says it all: I must be trying to do something the language does not support. Ansible  
> conditions operate on "include\_vars" - include\* statements in ansible are dynamic. Similarly, import\* statements in  
> ansible are static. Tags are applicable to statically imported constructs; tags are not applicable to dynamic  
> inclusions such as include\_roles or include\_tasks.
> 
> Therefore, since ansible conditionals are applicable to dynamic constructs, and ansible tags are applicable to static  
> constructs; the two are mutually exclusive. The language does not support the ability to tag variables that are  
> included via include\_vars. Bummer.
> 
> This begs the question: how does one conditionally include vars files in tagged tasks in ansible?? Anybody?

Why don't you use the inventory for these variables? It is possible to use multiple inventories which coincide with  
your current variable files.

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