--- - name: Require iscsi_portal when logging out absent targets ansible.builtin.assert: that: - iscsi_portal is defined - (iscsi_portal | string | length) > 0 fail_msg: "iscsi_absent_targets is set but iscsi_portal is missing/empty." when: (iscsi_absent_targets | default([]) | length) > 0 - name: Unmount absent iSCSI mountpoints ansible.builtin.mount: path: "{{ item.path | default(item) }}" state: unmounted loop: "{{ iscsi_absent_mounts | default([]) }}" loop_control: label: "{{ item.path | default(item) }}" tags: - iscsi_cleanup - name: Remove absent iSCSI fstab entries ansible.builtin.lineinfile: path: /etc/fstab state: absent regexp: "^\\s*\\S+\\s+{{ (item.path | default(item)) | regex_escape }}\\s+" loop: "{{ iscsi_absent_mounts | default([]) }}" loop_control: label: "{{ item.path | default(item) }}" tags: - iscsi_cleanup - name: Check for active sessions for absent targets ansible.builtin.shell: >- iscsiadm -m session 2>/dev/null | grep -Fq -- {{ item | quote }} register: _iscsi_absent_target_session_checks changed_when: false failed_when: false loop: "{{ iscsi_absent_targets | default([]) }}" loop_control: label: "{{ item }}" when: (iscsi_absent_targets | default([]) | length) > 0 tags: - iscsi_cleanup - name: Logout absent iSCSI targets (active sessions only) ansible.builtin.command: >- iscsiadm -m node -T {{ item.item }} -p {{ iscsi_portal }} --logout register: _iscsi_absent_target_logout changed_when: false failed_when: false loop: "{{ _iscsi_absent_target_session_checks.results | default([]) }}" loop_control: label: "{{ item.item }}" when: - (iscsi_absent_targets | default([]) | length) > 0 - item.rc == 0 tags: - iscsi_cleanup - name: Delete node records for absent iSCSI targets (best-effort) ansible.builtin.command: >- iscsiadm -m node -o delete -T {{ item }} -p {{ iscsi_portal }} changed_when: false failed_when: false loop: "{{ iscsi_absent_targets | default([]) }}" loop_control: label: "{{ item }}" when: (iscsi_absent_targets | default([]) | length) > 0 tags: - iscsi_cleanup