chore: update for iscsi

This commit is contained in:
2025-10-12 00:00:24 +10:00
parent e85df2798a
commit a892037efd
2 changed files with 88 additions and 51 deletions

View File

@@ -9,6 +9,11 @@
pre_tasks: pre_tasks:
- name: Connect iscsi - name: Connect iscsi
import_tasks: tasks/iscsi.yml import_tasks: tasks/iscsi.yml
vars:
iscsi_targets:
- iqn: iqn.2005-10.org.freenas.ctl:iscsi-docker
portal: 10.10.10.2
mount_point: /data
tags: iscsi_connect tags: iscsi_connect
- name: Connect NFS - name: Connect NFS

View File

@@ -9,80 +9,112 @@
name: parted name: parted
state: present state: present
- name: Discover iSCSI targets - name: Check existing iSCSI sessions
command: sudo iscsiadm -m discovery -t sendtargets -p "10.10.10.2"
register: iscsi_discovery
- name: Set target_iqn variable based on discovery
set_fact:
target_iqn: "{{ item.split(' ')[1] }}"
loop: "{{ iscsi_discovery.stdout_lines }}"
when: item.startswith("10.10.10.2")
- name: Check if iSCSI target is already connected
command: iscsiadm -m session command: iscsiadm -m session
register: iscsi_sessions register: iscsi_sessions
changed_when: false changed_when: false
failed_when: iscsi_sessions.rc not in [0, 21] # Allow success if the return code is 0 or 21 failed_when: iscsi_sessions.rc not in [0, 21]
- name: Connect to iSCSI target - name: Discover iSCSI targets for each portal
command: sudo iscsiadm -m node -T "{{ target_iqn }}" -p "10.10.10.2" --login command: iscsiadm -m discovery -t sendtargets -p "{{ item.portal }}"
when: target_iqn is defined and target_iqn not in iscsi_sessions.stdout register: iscsi_discovery
loop: "{{ iscsi_targets }}"
loop_control:
label: "{{ item.iqn }}"
changed_when: false
- name: Set iSCSI target for automatic login - name: Connect to iSCSI targets for this host
command: sudo iscsiadm -m node -T "{{ target_iqn }}" -p "10.10.10.2" --op update --name node.startup --value automatic command: iscsiadm -m node -T "{{ item.iqn }}" -p "{{ item.portal }}" --login
when: target_iqn is defined loop: "{{ iscsi_targets }}"
when: item.iqn not in iscsi_sessions.stdout
loop_control:
label: "{{ item.iqn }}"
- name: Fail if no target_iqn found - name: Set iSCSI targets for automatic login
fail: command: iscsiadm -m node -T "{{ item.iqn }}" -p "{{ item.portal }}" --op update --name node.startup --value automatic
msg: "No target IQN found for iSCSI server IP 10.10.10.2" loop: "{{ iscsi_targets }}"
when: target_iqn is not defined loop_control:
label: "{{ item.iqn }}"
- name: List all block devices # --------------------------
command: lsblk -o NAME,SIZE,TYPE,MODEL # Wait for the iSCSI device to appear
register: lsblk_output # --------------------------
- name: Wait for iSCSI device to appear
wait_for:
path: "/dev/disk/by-path/ip-{{ item.portal }}:3260-iscsi-{{ item.iqn }}-lun-0"
state: present
timeout: 30
loop: "{{ iscsi_targets }}"
loop_control:
label: "{{ item.iqn }}"
- name: Set iSCSI device variable # --------------------------
set_fact: # Check if device is raw
iscsi_device: "/dev/{{ item.split()[0] }}" # --------------------------
loop: "{{ lsblk_output.stdout_lines }}" - name: Get block device info for each target
when: item.split()[2] == 'disk' and 'iSCSI' in item # Adjust based on the MODEL you observe command: "blkid /dev/disk/by-path/ip-{{ item.portal }}:3260-iscsi-{{ item.iqn }}-lun-0"
register: blkid_output
failed_when: false
changed_when: false
loop: "{{ iscsi_targets }}"
loop_control:
label: "{{ item.iqn }}"
- name: Fail if no iSCSI device found # --------------------------
fail: # Create partition if device is raw
msg: "No iSCSI device found!" # --------------------------
when: iscsi_device is not defined - name: Create partition if device is raw
- name: Create a partition on iSCSI device using parted
parted: parted:
device: "{{ iscsi_device }}" device: "/dev/disk/by-path/ip-{{ item[1].portal }}:3260-iscsi-{{ item[1].iqn }}-lun-0"
number: 1 number: 1
state: present state: present
part_type: primary part_type: primary
fs_type: ext4 fs_type: ext4
part_start: 0% # Start at the beginning of the device part_start: 0%
part_end: 100% # Use the entire available space part_end: 100%
loop: "{{ blkid_output.results | zip(iscsi_targets) | map('flatten') | list }}"
loop_control:
label: "{{ item[1].iqn }}"
when: item[0].stdout == ""
- name: Create filesystem on new partition - name: Create filesystem if partition is raw
filesystem: filesystem:
fstype: ext4 fstype: ext4
dev: "{{ iscsi_device }}1" # Format the partition dev: "/dev/disk/by-path/ip-{{ item[1].portal }}:3260-iscsi-{{ item[1].iqn }}-lun-0-part1"
loop: "{{ blkid_output.results | zip(iscsi_targets) | map('flatten') | list }}"
loop_control:
label: "{{ item[1].iqn }}"
when: item[0].stdout == ""
- name: Create mount point - name: Create mount points
file: file:
path: /data path: "{{ item.mount_point }}"
state: directory state: directory
mode: "0777"
owner: root
group: root
loop: "{{ iscsi_targets }}"
loop_control:
label: "{{ item.iqn }}"
- name: Mount iSCSI target - name: Mount iSCSI targets
mount: mount:
path: /data path: "{{ item.mount_point }}"
src: "{{ iscsi_device }}1" # Mount the new partition src: "/dev/disk/by-path/ip-{{ item.portal }}:3260-iscsi-{{ item.iqn }}-lun-0-part1"
fstype: ext4 fstype: ext4
opts: defaults,_netdev opts: defaults,_netdev
state: mounted state: mounted
loop: "{{ iscsi_targets }}"
loop_control:
label: "{{ item.iqn }}"
# - name: Ensure iSCSI target is mounted at boot - name: Ensure mounted directories are world-writable
# lineinfile: file:
# path: /etc/fstab path: "{{ item.mount_point }}"
# line: "{{ iscsi_device }}1 /data ext4 _netdev 0 0" state: directory
# state: present mode: "0777"
owner: root
group: root
loop: "{{ iscsi_targets }}"
loop_control:
label: "{{ item.iqn }}"