Files
homelab/tasks/iscsi.yml

121 lines
3.3 KiB
YAML

---
- name: Ensure open-iscsi is installed
package:
name: open-iscsi
state: present
- name: Ensure parted is installed
package:
name: parted
state: present
- name: Check existing iSCSI sessions
command: iscsiadm -m session
register: iscsi_sessions
changed_when: false
failed_when: iscsi_sessions.rc not in [0, 21]
- name: Discover iSCSI targets for each portal
command: iscsiadm -m discovery -t sendtargets -p "{{ item.portal }}"
register: iscsi_discovery
loop: "{{ iscsi_targets }}"
loop_control:
label: "{{ item.iqn }}"
changed_when: false
- name: Connect to iSCSI targets for this host
command: iscsiadm -m node -T "{{ item.iqn }}" -p "{{ item.portal }}" --login
loop: "{{ iscsi_targets }}"
when: item.iqn not in iscsi_sessions.stdout
loop_control:
label: "{{ item.iqn }}"
- name: Set iSCSI targets for automatic login
command: iscsiadm -m node -T "{{ item.iqn }}" -p "{{ item.portal }}" --op update --name node.startup --value automatic
loop: "{{ iscsi_targets }}"
loop_control:
label: "{{ item.iqn }}"
# --------------------------
# Wait for the iSCSI device to appear
# --------------------------
- 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 }}"
# --------------------------
# Check if device is raw
# --------------------------
- name: Get block device info for each target
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 }}"
# --------------------------
# Create partition if device is raw
# --------------------------
- name: Create partition if device is raw
parted:
device: "/dev/disk/by-path/ip-{{ item[1].portal }}:3260-iscsi-{{ item[1].iqn }}-lun-0"
number: 1
state: present
part_type: primary
fs_type: ext4
part_start: 0%
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 if partition is raw
filesystem:
fstype: ext4
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 points
file:
path: "{{ item.mount_point }}"
state: directory
mode: "0777"
owner: root
group: root
loop: "{{ iscsi_targets }}"
loop_control:
label: "{{ item.iqn }}"
- name: Mount iSCSI targets
mount:
path: "{{ item.mount_point }}"
src: "/dev/disk/by-path/ip-{{ item.portal }}:3260-iscsi-{{ item.iqn }}-lun-0-part1"
fstype: ext4
opts: defaults,_netdev
state: mounted
loop: "{{ iscsi_targets }}"
loop_control:
label: "{{ item.iqn }}"
- name: Ensure mounted directories are world-writable
file:
path: "{{ item.mount_point }}"
state: directory
mode: "0777"
owner: root
group: root
loop: "{{ iscsi_targets }}"
loop_control:
label: "{{ item.iqn }}"