Help In Understanding/Resolving Loop Variables Within include_tasks Loops Error

Hi All,

I’m having trouble sorting out the below issue, and so I’m looking for a little help. I know (actually, I suspect) that its something simple I’ve missed, but after several hours of googling, etc, I can’t work it out.

My role is (tasks/main.yml):

- name: Include Tasks
  ansible.builtin.include_tasks:
    file: "{{Task_Item}}.yml"
  loop: "{{Task_List}}"
  loop_control:
    loop_var: Task_Item

My vars/main.yml is:

Python_Version: 3.12.1

Task_List:
  - task_1
  - task_2

PIP_List:
  - passlib
  - proxmoxer
  - requests

My tasks/task_1.yml is:

- name: Task 1 - Install Python Packages
  ansible.builtin.dnf:
    name: "{{item}}"
    state: present
  with_items:
    - "python{{Python_Version}}"
    - "python{{Python_Version}}-pip"

My tasks/task_2.yml is:

- name: Task 2 - Install via PIP
  ansible.builtin.pip:
    name: "{{item}}"
    state: latest
    loop: "{{PIP_List}}"

The Issue:

Task 1 runs AOK, but Task 2 give the following error message (which is what I’m having trouble understanding/resolving - I mean, I unserstand what its saying, but I don’t understand why its saying it ie how to resolve it):

[ERROR]: Task failed: Finalization of task args for 'ansible.builtin.pip' failed: Error while resolving value for 'name': 'item' is undefined

So, could someone please help educate me as to why this is happening (& if a solution was included that’d be great) - thanks.

I don’t know if it’s just incorrectly copy-pasted but the loopkeyword in your pip task is indented on wrong level, it should be:

- name: Task 2 - Install via PIP
  ansible.builtin.pip:
    name: "{{item}}"
    state: latest
  loop: "{{PIP_List}}"

Thanks @mkrizek (Martin),

DOH!

Like I said, it’s something simple that I was missing.

Cheers :smile: