Linux lun mount and format/filesystem

47 views
Skip to first unread message

anonymus

unread,
Oct 6, 2020, 1:35:07 PM10/6/20
to Ansible Project
i am newbie on ansible
i wanted to do the below task using ansible , is there any playbook for

1. Discovering an iscsi target
2. Login in 
3. rescanning iscsi bus
4. formatting the newly discovered lun
5. creating filesystem on it.
6. updating /etc/fstab with newly mounted


Paul Martin

unread,
Oct 8, 2020, 12:05:26 PM10/8/20
to ansible...@googlegroups.com
Hi,
I did this in the past for block storage, guessing something similar to this example,  I've not formatted the devices as this was for mounting a clone

  tasks:
# Rescan storage layer to pull in new devices to host
# Example here is for emulex hba
    - name: Rescan HBA
      command: /usr/bin/rescan-scsi-bus.sh

    - name: unmount filesystems by label
      mount:
        path: "{{ item }}"
        state: unmounted
      with_items:
        - /database
        - /reports
        - /logs

    - name: Create Directory
      file:
        path: "{{ item }}"
        state: directory
      with_items:
        - /database
        - /reports
        - /logs

    - name: Mount filesystems by label
      mount:
        path: "{{ item.mountpoint }}"
        src: LABEL="{{ item.label }}"
        fstype: ext4
        state: mounted
      with_items:
        - { label: 'database', mountpoint: '/database'}
        - { label: 'reports', mountpoint: '/reports'}
        - { label: 'logs', mountpoint: '/logs'}

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/256cb4ce-e826-43d4-b70e-87cfbe49c0e3n%40googlegroups.com.


--
Paul Martin

anonymus

unread,
Oct 18, 2020, 11:20:06 AM10/18/20
to Ansible Project

Thank you Paul.

gajendra....@gmail.com

unread,
Oct 21, 2020, 12:55:17 PM10/21/20
to Ansible Project

Hello,
I have created below playbook, but i have not tested on live
check if its work for you. 

---
- name: disk addition
  hosts: localhost
  connection: local
  tasks:
  - name: Identify exiting disk
    shell: lsblk  | grep '^[a-z]' | grep -v sr0 | cut -f1 -d' '
    register: disk_list1

  - name: Scan Storage LUN
    shell: for host in `ls /sys/class/scsi_host/`;do echo "- - -" >/sys/class/scsi_host/${host}/scan; done

  - name: Identify exiting disk with new disk
    shell: lsblk  | grep '^[a-z]' | grep -v sr0 | cut -f1 -d' '
    register: disk_list2

  - debug: var=disk_list2
  - set_fact:
      new_disk: "{{ disk_list2.stdout_lines | difference(disk) }}"

  - name: Search storage LUN ID
    shell: ls -la /dev/disk/by-id/ | grep {{ new_disk[0] }} | grep scsi | awk -F'scsi-' '{print $NF}' | cut -f1 -d' '
    register: lun_id
    when: new_disk[0] is defined

  - name: Search Storage NAME DEIVCE PATH
    shell: multipath -ll "{{ lun_id.stdout }}" | cut -f1 -d' '
    register: device_name
    when: lun_id is defined and lun_id.stdout | length > 4

  - name: Create a PV
    shell: pvcreate /dev/mapper/{{ device_name }}
    register: pv_state
    when:  device_name.stdout is defined

  - name: Find VG name
    shell: df -hP "{{ filesystem }}" | sed -n '2p' | awk '{print $1}'|awk -F "/" '{print $NF}'|awk -F "-" '{print $(NF -1)}'
    register: vg_name
    when: pv_state.stdout is defined and pv_state.rc == 0

  - name: Extend VG {{ vg_name }}
    shell: vgextend {{ vg_name }} /dev/mapper/{{ device_name }}
    register: vg_state
    when: pv_state.stdout is defined and pv_state.rc == 0
Reply all
Reply to author
Forward
0 new messages