# Variables in include statements

**URL:** <https://forum.ansible.com/t/variables-in-include-statements/16398>\
**Category:** Ansible Project\
**Created:** [October 1, 2014, 9:26pm UTC](https://forum.ansible.com/t/variables-in-include-statements/16398 "2014-10-01T21:26:03Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alexey\_Malov](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@Alexey\_Malov](https://forum.ansible.com/u/Alexey_Malov)\
**Post date:** [October 1, 2014, 9:26pm UTC](https://forum.ansible.com/t/variables-in-include-statements/16398/1 "2014-10-01T21:26:03Z")

</div>

Hello all,  
Hope you are doing well.

Could you please tell me if it’s possible to use variables in the include statement?  
I’m trying to do next thing:

1. Have some role, for example ‘superrole’.
2. And have some tasks inside it.
3. Use main task to include subtasks based on variables like

# main.yml

---

<div class="post-metadata">

**Author:** ![James\_Cammarata](https://avatars.discourse-cdn.com/v4/letter/j/7c8e57/32.png) [@James\_Cammarata](https://forum.ansible.com/u/James_Cammarata)\
**Post date:** [October 3, 2014, 6:54pm UTC](https://forum.ansible.com/t/variables-in-include-statements/16398/2 "2014-10-03T18:54:14Z")

</div>

Hi Alexey,

It is possible to use variables in include statements, however there is one limitation - the variables must be known at the time the playbook YAML is parsed, and therefore cannot be based on “inventory” variables (facts, group\_vars or host\_vars) or parameterized role variables (which is why your test case is failing).

What you can do instead is a conditional include:

- include: one\_sub\_task.yml  
when: subtask == ‘one\_sub\_task’
- include: another\_sub\_task.yml  
when: subtask == ‘another\_sub\_task’

Hope that helps!
