ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

RH358访问基于块的网络存储--自动化配置iSCSI initiator

2022-01-22 23:58:46  阅读:185  来源: 互联网

标签:initiator target -- iSCSI playbook data iscsi name


RH358访问基于块的网络存储–自动化配置iSCSI initiator

本章节介绍如何使用Ansible连接iSCSI存储和管理。

RH358专栏地址:https://blog.csdn.net/qq_41765918/category_11532281.html

文章目录

1. 使用Ansible连接iSCSI Targets

使用Ansible open_iscsi模块发现并登录远程iSCSl target。该模块依赖于iscsiadm命令,该命令由iscsi-initiator-utils包提供。

下面的剧本演示了如何使用open_iscsi模块。

---
- 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
# 使用Ansible yum模块安装iscsi-initiator-utils包。
      yum:
        name: iscsi-initiator-utils
        state: present
        
- name: the IQN is set for the initiator
# 使用copy或template模块在/etc/iscsi/ initiatorname.iscsi文件中设置启动器IQN。本例中的任务使用ansible_facts['hostname ']变量中的系统短主机名来设置IQN中的可选字符串。当IQN发生变化时,任务通知restart iscsid处理器重新启动iscsid服务
      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_handlers元任务强制通知的处理程序在该点运行。如果上述任务更改了IQN,则启动器登录目标器之前必须运行重启iscsid服务的处理程序。
    
- name: the iSCSI target is discovered and available
#使用 open_iscsi 模块来 discover 并 login 到指定的 target。discover: yes 选项执行发现,但是 target: iqn.2014-06.com.example:rack1 和 login: yes 执行登录特定的IQN(如果可用)。通过 register 指令记录输出,可在后续任务中引用这些设备
      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
# debug任务显示目标变量的内容。这对play来说不是必要的,但是显示了open_iscsi模块可以通过注册变量提供的信息。
      debug:
        var: target
        
  handlers:
    - name: restart iscsid
      service:
        name: iscsid
        state: restarted

下面的输出显示了剧本的执行。

[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 ...

target变量包含open_iscsi模块任务的注册输出的字典。在该字典中,devicenodes变量包含使用门户192.168.0.10:3260执行发现后创建的本地设备列表。您可以使用loop遍历target['devicenodes ']中的列表,或者尝试直接从列表中提取特定项。

例如,要获取列表中第一项的值,可以使用该变量目标器[‘devicenodes’][0],值为/dev/sdc,第二个项目可以作为目标['devicenodes '][1]引用(在前面的例子中它的值是/dev/sdd)。很多时候,目标只提供一个LUN,您可以只引用列表中的第一项。

当目标提供多个设备时,例如在前面的输出中,没有简单的方法从列表中识别特定的设备。一种解决方案是在open_iscsi任务之后调用setup模块来刷新Ansible事实。ansible_facts[‘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...

断开与 target 的连接

使用 open_iscsi 模块来从远程 iSCSI target 注销。将 discover、login 和 auto_node_startup 参数设置为 no。以下任务会将 initiator 从 iqn.2014-06.com.example:rack1 target 断开连接

- 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. 格式化iSCSI设备

rhel-system-roles包提供了Ansible存储系统角色,可以使用该角色格式化和持久地挂载新设备。

该角色可以在未分区的磁盘上设置LVM,或者直接用文件系统格式化该磁盘。角色在整个磁盘上工作。它不能创建分区,也不能使用已经分区的磁盘。

下面的脚本使用存储系统角色将/dev/sdc磁盘格式化和持久挂载/data下。

---
- 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
# type参数指定角色是在磁盘(LVM)上设置LVM,还是直接格式化整个磁盘(disk)。
          disks:
# disks参数列出了需要配置的设备。在配置LVM时,角色将这些磁盘作为物理卷使用。如果将type设置为disk,则只能在该列表中提供一个磁盘。
            - sdc
          mount_point: /data
# mount_point参数提供文件系统挂载点。如果目录不存在,角色将创建该目录。
          fs_type: ext4
# 参数fs_type表示文件系统类型。
          mount_options: '_netdev'
# mount_options参数提供了挂载选项。角色将这些选项添加到/etc/fstab

**警告:**如果修改角色以在fs_type参数中指定不同的格式,然后重新运行playbook,角色将用新的文件系统类型重新格式化设备。此操作将破坏旧文件系统上以前的所有数据。

擦除iSCSI设备

可以使用Ansible存储系统角色卸载和擦除文件系统。为此,将state参数设置为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'

警告:如果state设置为absent,存储系统角色将卸载文件系统,将其从/etc/fstab中删除,并销毁设备上的所有数据。要持久地卸载文件系统,但保留其数据,请使用state设置为absent的Ansible mount模块。下面的任务使用mount模块卸载/data,并从/etc/fstab中删除它的条目。

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

3. 课本练习

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

在本练习中,您将使用Ansible在服务器上配置iSCS Iinitiator来访问服务器提供的target。将生成的块设备格式化并挂载到/data下。

1. 熟悉项目及其现状。

[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. 回顾并完成playbook.yml Ansible剧本。

该脚本在服务器上配置iSCSI启动器,从服务器发现并登录iSCSI目标,然后将新的块设备格式化并挂载在/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. 验证剧本的语法并运行。

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

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

4. 在servera上,检查/data下的文件系统是否可以访问。

在该目录中创建一个测试文件,以确认可以向文件系统写入数据。

[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. 在workstation上检查unmount.yml的剧本和运行它。

这个剧本卸载服务器上的数据,然后从iSCSI目标退出。

[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. 运行playbook.yml重新发现并再次登录目标。

Ansible存储系统角色检测到设备已经有一个文件系统,不会重新格式化它。

[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

完成实验

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

总结

  • 介绍如何使用Ansible连接iSCSI Targets。
  • 介绍如何使用Ansible断开和格式化iSCSI设备。
  • 若喜欢金鱼哥的文章,顺手点个赞。也可点个关注,因为后续会不断上干货。

标签:initiator,target,--,iSCSI,playbook,data,iscsi,name
来源: https://blog.csdn.net/qq_41765918/article/details/122646059

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有