featcreate task to map iscsi drive and partition, refs #31

This commit is contained in:
Matthew McKinnon 2024-10-29 21:44:57 +10:00
parent a22c81d7b5
commit 1569d310e9
3 changed files with 86 additions and 0 deletions

4
hosts
View File

@ -6,3 +6,7 @@ vps02.comprofix.com
[docker]
docker.comprofix.xyz
[dev]
10.10.10.170

View File

@ -127,6 +127,14 @@
tags: dockerserver_install
- hosts: dev
become: yes
tasks:
- name: Connect iSCSI
import_tasks: tasks/iscsi.yml
tags: iscsi_connect

74
tasks/iscsi.yml Normal file
View File

@ -0,0 +1,74 @@
---
- name: Ensure open-iscsi is installed
package:
name: open-iscsi
state: present
- name: Ensure open-iscsi is installed
package:
name: parted
state: present
- name: Discover iSCSI targets
command: iscsiadm -m discovery -t sendtargets -p "10.10.10.2"
register: iscsi_discovery
changed_when: False
- 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: Fail if no target_iqn found
fail:
msg: "No target IQN found for iSCSI server IP 10.10.10.2"
when: target_iqn is not defined
- name: List all block devices
command: lsblk -o NAME,SIZE,TYPE,MODEL
register: lsblk_output
- name: Set iSCSI device variable
set_fact:
iscsi_device: "/dev/{{ item.split()[0] }}"
loop: "{{ lsblk_output.stdout_lines }}"
when: item.split()[2] == 'disk' and 'iSCSI' in item # Adjust based on the MODEL you observe
- name: Fail if no iSCSI device found
fail:
msg: "No iSCSI device found!"
when: iscsi_device is not defined
- name: Create a partition on iSCSI device using parted
parted:
device: "{{ iscsi_device }}"
number: 1
state: present
part_type: primary
fs_type: ext4
part_start: 0% # Start at the beginning of the device
part_end: 100% # Use the entire available space
- name: Create filesystem on new partition
filesystem:
fstype: ext4
dev: "{{ iscsi_device }}1" # Format the partition
- name: Create mount point
file:
path: /mnt/data
state: directory
- name: Mount iSCSI target
mount:
path: /mnt/data
src: "{{ iscsi_device }}1" # Mount the new partition
fstype: ext4
state: mounted
- name: Ensure iSCSI target is mounted at boot
lineinfile:
path: /etc/fstab
line: "{{ iscsi_device }}1 /mnt/iscsi ext4 defaults 0 0"
state: present