Variables in include statements

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

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!