The documentation for import_tasks mentions
Any loops, conditionals and most other keywords will be applied to the included tasks, not to this statem
You can add a with_items
loop taking a list to every task in the imported file, and call import_tasks
with a variable which you pass to the inner with_items
loop. This moves the handling of the loops to the imported file, and requires duplication of the loop on all tasks.
Given your example, this would change the files to:
playbook.yml
---
- hosts: 192.168.33.100
gather_facts: no
tasks:
- import_tasks: msg.yml
vars:
messages:
- 1
- 2
msg.yml
---
- name: Message 1
debug:
msg: "Message 1: {{ item }}"
with_items:
- "{{ messages }}"
- name: Message 2
debug:
msg: "Message 2: {{ item }}"
with_items:
- "{{ messages }}"