Excellent question. This is a classic Ansible puzzle where the error message is misleading.
The error item is not defined is almost certainly a red herring. The real problem is that the parent directory, /k3s-data, does not exist when you run the playbook in check mode.
Let's break down why this happens.
The Root Cause: A Chain of Dependencies
Based on our previous conversation, your playbook does the following:
- Create Logical Volume (
lvol): In check mode, this task reports that a volume would be created, but no actual volume is created.
- Format Filesystem (
filesystem): This task likely fails in check mode because the device it's trying to format (/dev/.../k3s-data-lv) doesn't exist.
- Mount Volume (
mount): This task also fails or is skipped because the device and filesystem don't exist. The mount point /k3s-data is never created or made available.
Now, your new task comes along:
- name: Prepare folders for K3s data
ansible.builtin.file:
path: "{{ item.folder }}" # e.g., /k3s-data/k3s
state: directory
loop:
- { folder: "/k3s-data/k3s" }
# ...
When this task runs in check mode, the ansible.builtin.file module tries to determine if it needs to create the directory /k3s-data/k3s. To do that, it must check the status of its parent directory, /k3s-data.
Since the mount task didn't actually run, the /k3s-data directory does not exist on the target system. The file module fails because it cannot create a subdirectory in a non-existent parent.
Why the Misleading Error "item is not defined"?
When a module fails inside a loop, Ansible's error reporting can sometimes get confused. Instead of giving the clear error from the module (e.g., "Parent directory /k3s-data does not exist"), it reports a failure in the loop's context, making it seem like the loop variable (item) is the problem.
In short: The module failed, and the error got misinterpreted as a loop variable issue.
The Solution: Ensure the Parent Directory is Created First
The ansible.builtin.file module is smart enough to handle nested directory creation. The key is to ensure the top-level mount point directory is created before you try to mount anything to it or create subdirectories within it.
Here is the corrected and robust order of operations for your playbook:
---
- hosts: your_hosts
become: true
tasks:
# Task 1: Create the logical volume. This works fine in check mode (reports change).
- name: Create logical volume for K3s data
community.general.lvol:
vg: "{{ ansible_distribution|lower + '-vg' | default('ubuntu-vg') }}"
lv: "k3s-data-lv"
size: "{{ k3s.storage.data_size }}"
# Task 2: Format the LV. This must be skipped in check mode.
- name: Format logical volume for K3s data
community.general.filesystem:
fstype: ext4
dev: "/dev/{{ ansible_distribution|lower + '-vg' | default('ubuntu-vg') }}/k3s-data-lv"
when: not ansible_check_mode
# Task 3: Create the mountpoint directory. This is CRITICAL.
# This task will "create" the directory in check mode, allowing subsequent tasks to succeed.
- name: Create mountpoint for K3s data
ansible.builtin.file:
path: "/k3s-data"
state: directory
mode: "0755"
# Task 4: Mount the volume. This must be skipped in check mode.
- name: Mount logical volume for K3s data
ansible.posix.mount:
path: "/k3s-data"
src: "/dev/{{ ansible_distribution|lower + '-vg' | default('ubuntu-vg') }}/k3s-data-lv"
fstype: ext4
state: mounted
when: not ansible_check_mode
# Task 5: NOW you can create subdirectories.
# This will work in check mode because Task 3 "created" /k3s-data.
- name: Prepare sub-folders for K3s data
ansible.builtin.file:
path: "{{ item.folder }}"
state: directory
mode: "0755"
loop:
- { folder: "/k3s-data/k3s" }
- { folder: "/k3s-data/k3s-pods" }
By explicitly creating the /k3s-data directory with a separate file task, you provide the necessary parent for the looped task, resolving the failure in check mode.