Variables in include statements

75 views
Skip to first unread message

Alexey Malov

unread,
Oct 1, 2014, 5:26:03 PM10/1/14
to ansible...@googlegroups.com
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
---
 - include {{ subtask }}.tml
3. To po next in the playbooks:
- { role: superrole, subtask: one_sub_task }
- { role: superrole, subtask: another_sub_task }

Thank you very much,
Alex.

James Cammarata

unread,
Oct 3, 2014, 2:54:25 PM10/3/14
to ansible...@googlegroups.com
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!


--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/ca6eddd4-884c-42eb-acb8-917560e97887%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages