RH358 accessing block based networked storage - automated configuration of iSCSI initiator

Posted by titangf on Mon, 24 Jan 2022 07:35:26 +0100

RH358 accessing block based networked storage – automated configuration of iSCSI initiator

This section describes how to use Ansible to connect iSCSI storage and management.

RH358 column address: https://blog.csdn.net/qq_41765918/category_11532281.html

1. Use Ansible to connect iSCSI Targets

Use ansible open_ The iSCSI module discovers and logs in to the remote iSCSI target. This module relies on the iSCSI ADM command, which is provided by the iSCSI initiator utils package.

The following script demonstrates how to use open_iscsi module.

---
- name: Ensure logged in to iSCSI target on 192.168.0.10
  hosts: host.example.com
  become: true
  
  tasks:
- name: the iscsi-initiator-utils package is installed
# Install the iSCSI initiator utils package using the Ansible yum module.
      yum:
        name: iscsi-initiator-utils
        state: present
        
- name: the IQN is set for the initiator
# Use the copy or template module in / etc / iSCSI / initiatorname Set the initiator IQN in the iSCSI file. The task in this example uses ansible_ Set the optional string in IQN by using the system short hostname in the facts ['hostname '] variable. When the IQN changes, the task notifies the restart iscsid processor to restart the iscsid service
      copy:
        dest: /etc/iscsi/initiatorname.iscsi
        content: "InitiatorName=iqn.2014-06.com.example:{{ ansible_facts['hostname'] }}\n"
        mode: '644'
        owner: root
        group: root
      notify: restart iscsid
      
- meta: flush_handlers
# flush_ The handlers meta task forces the notification handler to run at this point. If the above task changes the IQN, the initiator must run the process of restarting the iSCSI ID service before logging in to the target.
    
- name: the iSCSI target is discovered and available
#Use open_iscsi module to discover and login to the specified target. The discover: yes option performs discovery, but target: IQN 2014-06. com. Example: Rack1 and login: yes perform login to a specific IQN (if available). These devices can be referenced in subsequent tasks by recording the output through the register instruction
      open_iscsi:
        portal: 192.168.0.10
        port: '3260'
        target: iqn.2014-06.com.example:rack1
        discover: yes
        login: yes
      register: target
      
- name: display the discovered devices
# The debug task displays the contents of the target variable. This is not necessary for play, but it shows open_iscsi modules can register the information provided by variables.
      debug:
        var: target
        
  handlers:
    - name: restart iscsid
      service:
        name: iscsid
        state: restarted

The following output shows the execution of the script.

[root@host ~]$ ansible-playbook playbook.yml
PLAY [Ensure the iSCSI target on host is discovered] *********************

TASK [Gathering Facts] ***************************************************
ok: [host.example.com]

TASK [the iscsi-initiator-utils package is installed] ********************
changed: [host.example.com]

TASK [the IQN is set for the initiator] **********************************
changed: [host.example.com]

RUNNING HANDLER [restart iscsid] *****************************************
changed: [host.example.com]

TASK [the iSCSI target is discovered and available] **********************
changed: [host.example.com]

TASK [display the discovered devices] ************************************
ok: [host.example.com] => {
    "target": {
        "automatic_changed": false,
        "cache_updated": true,
        "changed": true,
        "connection_changed": true,
        "devicenodes": [
            "/dev/sdc",
            "/dev/sde",
            "/dev/sdd"
        ],
        "failed": false
    }
}

PLAY RECAP ******************************************************************
host.example.com : ok=6 changed=4 unreachable=0 failed=0 ...

The target variable contains open_ The dictionary of the registered output of the iSCSI module task. In this dictionary, the devicenodes variable contains the list of local devices created after discovery using portal 192.168.0.10:3260. You can use loop to traverse the list in target['devicenodes'], or try to extract specific items directly from the list.

For example, to get the value of the first item in the list, you can use the variable target ['devicenodes'] [0], with the value of / dev/sdc, and the second item can be referenced as the target ['devicenodes'] [1] (in the previous example, its value is / dev/sdd). Many times, the target provides only one LUN, and you can only refer to the first item in the list.

When the target provides multiple devices, for example, in the previous output, there is no simple way to identify a specific device from the list. One solution is open_ After the iSCSI task, the setup module is called to refresh the Ansible fact. ansible_facts ['devices'] dictionary groups the information of all devices on the system and provides detailed information about new devices.

[root@host ~]$ ansible host.example.com -m setup
...output omitted...
        "ansible_devices": {
            "sdc": {
                "holders": [],
                "host": "",
                "links": {
                    "ids": [
                        "scsi-36001405b1c74dbfcce04ccda76f8c21f",
                        "wwn-0x6001405b1c74dbfcce04ccda76f8c21f"
                    ],
                    "labels": [],
                    "masters": [],
                    "uuids": []
                },
                "model": "target.disk1",
                "partitions": {},
                "removable": "0",
                "rotational": "1",
                "sas_address": null,
                "sas_device_handle": null,
                "scheduler_mode": "mq-deadline",
                "sectors": "2097152",
                "sectorsize": "512",
                "size": "1.00 GB",
                "support_discard": "0",
                "vendor": "LIO-ORG",
                "virtual": 1,
                    "wwn": "0x6001405b1c74dbfcce04ccda76f8c21f"
                },
            "sdd": {
                "holders": [],
                "host": "",
                "links": {
                    "ids": [
                        "scsi-360014058f584b514fe943b59d9931965",
                        "wwn-0x60014058f584b514fe943b59d9931965"
                ],
                ...output omitted...

Disconnect from target

Use open_iscsi module to log out of the remote iSCSI target. Set discover, login, and auto_ node_ The startup parameter is set to no. The following tasks remove the initiator from iqn 2014-06. com. Example: Rack1 target disconnected

- name: the iSCSI target is disconnected
  open_iscsi:
    portal: 192.168.0.10
    port: '3260'
    target: iqn.2014-06.com.example:rack1
    discover: no
    login: no
    auto_node_startup: no

2. Format iSCSI devices

The RHEL system roles package provides the Ansible storage system role, which can be used to format and persistently mount new devices.

This role can set LVM on an unpartitioned disk, or format the disk directly with the file system. The role works on the entire disk. It cannot create partitions or use partitioned disks.

The following script formats and persistently mounts the / dev/sdc disk under / data using the storage system role.

---
- name: Ensure the device is formatted and mounted
  hosts: host.example.com
  become: true
    
  roles:
    - role: rhel-system-roles.storage
      storage_volumes:
        - name: datadisk
          state: present
          type: disk
# The type parameter specifies whether the role sets LVM on the disk (LVM) or directly formats the entire disk (disk).
          disks:
# The disks parameter lists the devices to be configured. When configuring LVM, the role uses these disks as physical volumes. If the type is set to disk, only one disk can be provided in the list.
            - sdc
          mount_point: /data
# mount_ The point parameter provides a file system mount point. If the directory does not exist, the role creates it.
          fs_type: ext4
# Parameter fs_type indicates the file system type.
          mount_options: '_netdev'
# mount_ The options parameter provides mount options. Roles add these options to / etc/fstab

**Warning: * * if the role is modified to be in FS_ Specify a different format in the type parameter, and then rerun the playbook. The role will reformat the device with the new file system type. This operation will destroy all previous data on the old file system.

Erasing iSCSI devices

You can unmount and erase file systems using the Ansible storage system role. To do this, set the state parameter to absent.

roles:
  - role: rhel-system-roles.storage
    storage_volumes:
      - name: datadisk
        state: absent
        type: disk
        disks:
        - sdc
        mount_point: /data
        fs_type: ext4
        mount_options: '_netdev'

Warning: if state is set to absent, the storage system role unmounts the file system, removes it from / etc/fstab, and destroys all data on the device. To permanently unmount a file system but retain its data, use the Ansible mount module with state set to absent. The following tasks uninstall / data using the mount module and remove its entry from / etc/fstab.

- name: the device is not mounted
  mount:
    path: /data
    state: absent

3. Textbook exercises

[student@workstation ~]$ lab iscsi-automation start

In this exercise, you will use Ansible to configure the iSCS Iinitiator on the server to access the target provided by the server. Format and mount the generated block device under / data.

1. Be familiar with the project and its current situation.

[student@workstation ~]$ cd /home/student/iscsi-automation
[student@workstation iscsi-automation]$ tree
.
├── ansible.cfg
├── cleanup.yml
├── inventory
├── playbook.yml
├── solution
│   └── playbook.yml
├── templates
│   └── initiatorname.iscsi.j2
└── unmount.yml

2 directories, 7 files

2. Review and complete playbook YML ansible script.

The script configures the iSCSI initiator on the server, discovers and logs in the iSCSI target from the server, and then formats and mounts the new block device under / data.

[student@workstation iscsi-automation]$ cat playbook.yml 
---
- name: Ensure /data is mounted from serverd iSCSI target
  hosts: initiators
  become: true

  tasks:
    - name: the iscsi-initiator-utils package is installed
      yum:
        name: iscsi-initiator-utils
        state: present

    - name: the IQN is set for the initiator
      template:
        dest: /etc/iscsi/initiatorname.iscsi
        src: templates/initiatorname.iscsi.j2
        mode: '644'
        owner: root
        group: root
      notify: restart iscsid

    # Forces the handler to run so that the iscsid service is restarted
    # and is aware of the new initiator IQN
    - meta: flush_handlers

    - name: the iSCSI target is discovered and available
      open_iscsi:
        portal: 172.25.250.13
        port: '3260'
        target: iqn.2014-06.com.example:serverd
        discover: yes
        login: yes
      register: target

    - name: display the discovered devices
      debug:
        var: target['devicenodes']

    - name: the new device is formatted and mounted under /data
      include_role:
        name: rhel-system-roles.storage
      vars:
        storage_volumes:
          - name: devdata
            state: present
            type: disk
            disks:
              - "{{ target['devicenodes'][0] }}"
            mount_point: /data
            fs_type: xfs
            mount_options: '_netdev'

  handlers:
    - name: restart iscsid
      service:
        name: iscsid
        state: restarted

3. Verify the grammar of the script and run it.

[student@workstation iscsi-automation]$ ansible-playbook playbook.yml --syntax-check

playbook: playbook.yml
[student@workstation iscsi-automation]$ ansible-playbook playbook.yml

4. On servera, check whether the file system under / data can be accessed.

Create a test file in this directory to confirm that data can be written to the file system.

[root@servera ~]# lsblk --fs
NAME   FSTYPE LABEL UUID                                 MOUNTPOINT
sda    xfs          e2afea80-317e-40f2-a3d3-d4168929795d /data
vda                                                      
└─vda1 xfs          f7614c41-2835-4125-bb13-50772dc2f30c /
vdb        
[root@servera ~]# echo Hello World > /data/test.txt
[root@servera ~]# cat /data/test.txt
Hello World

5. Check unmount on workstation YML script and run it.

This scenario unmounts the data on the server and exits from the iSCSI target.

[student@workstation iscsi-automation]$ cat unmount.yml 
---
- name: Ensure /data is not mounted
  hosts: initiators
  become: true

  tasks:
    - name: the new device is not mounted
      mount:
        path: /data
        state: absent

    - name: the iSCSI target is disconnected
      open_iscsi:
        portal: 172.25.250.13
        port: '3260'
        target: iqn.2014-06.com.example:serverd
        discover: no
        login: no
        auto_node_startup: no
[student@workstation iscsi-automation]$ ansible-playbook unmount.yml

[root@servera ~]# lsblk --fs
NAME   FSTYPE LABEL UUID                                 MOUNTPOINT
vda                                                      
└─vda1 xfs          f7614c41-2835-4125-bb13-50772dc2f30c /
vdb   

6. Run playbook YML rediscover and log in to the target again.

The Ansible storage system role has detected that the device already has a file system and will not reformat it.

[student@workstation iscsi-automation]$ ansible-playbook playbook.yml

[root@servera ~]# lsblk --fs
NAME   FSTYPE LABEL UUID                                 MOUNTPOINT
sda    xfs          e2afea80-317e-40f2-a3d3-d4168929795d /data
vda                                                      
└─vda1 xfs          f7614c41-2835-4125-bb13-50772dc2f30c /
vdb                         
[root@servera ~]# ls /data
test.txt
[root@servera ~]# cat /data/test.txt
Hello World

Complete the experiment

[student@workstation ~]$ lab iscsi-automation finish

summary

  • Describes how to connect iSCSI Targets using Ansible.
  • Describes how to use Ansible to disconnect and format iSCSI devices.
  • If you like brother goldfish's article, please praise it. You can also pay attention, because the follow-up will continue to dry goods.

Topics: Linux Operation & Maintenance server