I have a role called application_management. In the tasks folder the main.yml contains :
---
- name: "{{ item }}"
include_tasks: '{{ item }}.yml'
loop: "{{ role_actions }}"
In my playbook I'm using the role with
- role: application_management
vars:
role_actions:
- taskA
- taskB
It is working BUT the 2 tasks are first included
TASK [application_management : {{ item }}] *************************************************************************************************************************************************************************************************************
included: /mnt/d/dev/ansible/roles/application_management/tasks/taskA.yml for host01 => (item=taskA)
included: /mnt/d/dev/ansible/roles/application_management/tasks/taskB.yml for host01 => (item=taskB)
and only after both tasks are executed but without anything to distinguish which one it is.
Is there a way of including taskA / executing taskA, including taskB / executing taskB ?
Or a way to retrieve the item variable inside taskA.yml/taskB.yml ?
And I see TASK [application_management : {{ item }}]. Is there a way of getting the item name displayed ?
This can be fixed with task naming. In the following example, the said role has named tasks in
taskA.ymland nameless tasks intaskB.yml:Including in Ansible means dynamic loading. What you're describing is importing (static loading), which is possible using
import_tasksmodule. But it's unsuitable for your use case because it cannot be used with a loop. With that being said,include_tasks, of course, still works as you expect in terms of running the tasks sequentially.Sure,
{{ item }}is a usual variable and will be available everywhere within the loop scope:However, I would not recommend using it like that because it restricts the reusability of your tasks.
No because the loop is not applied to it.
By the way, if you just want to include some tasks from a role and don't need the role to control its execution, you don't need a loop within a role. You can use
include_rolemodule withtasks_from: