金鱼哥RHCA回忆录:RH358访问基于块的网络存储--自动化配置iSCSI initiator

举报
金鱼哥 发表于 2022/05/18 17:38:54 2022/05/18
【摘要】 第十一章 访问基于块的网络存储--自动化配置iSCSI initiator

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


🎹 个人简介:大家好,我是 金鱼哥,CSDN运维领域新星创作者,华为云·云享专家,阿里云社区·专家博主
📚个人资质:CCNA、HCNP、CSNA(网络分析师),软考初级、中级网络工程师、RHCSA、RHCE、RHCA、RHCI、ITIL😜
💬格言:努力不一定成功,但要想成功就必须努力🔥


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设备。

RHCA认证需要经历5门的学习与考试,还是需要花不少时间去学习与备考的,好好加油,可以噶🤪。

以上就是【金鱼哥】对 第十一章 访问基于块的网络存储–自动化配置iSCSI initiator 的简述和讲解。希望能对看到此文章的小伙伴有所帮助。

💾红帽认证专栏系列:
RHCSA专栏:戏说 RHCSA 认证
RHCE专栏:戏说 RHCE 认证
此文章收录在RHCA专栏:RHCA 回忆录

如果这篇【文章】有帮助到你,希望可以给【金鱼哥】点个赞👍,创作不易,相比官方的陈述,我更喜欢用【通俗易懂】的文笔去讲解每一个知识点。

如果有对【运维技术】感兴趣,也欢迎关注❤️❤️❤️ 【金鱼哥】❤️❤️❤️,我将会给你带来巨大的【收获与惊喜】💕💕!

【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。