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

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

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设备。
  • 若喜欢金鱼哥的文章,顺手点个赞。也可点个关注,因为后续会不断上干货。

目录
相关文章
|
4月前
|
监控 安全 Linux
Arista CloudVision 2025.1 - 多云和数据中心网络自动化、监控和分析
Arista CloudVision 2025.1 - 多云和数据中心网络自动化、监控和分析
136 2
Arista CloudVision 2025.1 - 多云和数据中心网络自动化、监控和分析
|
4月前
|
域名解析 监控 网络协议
21.7K star!再见吧花生壳,没有公网ip,也能搞定所有网络访问!内网穿透神器NPS
“nps”是一款强大的内网穿透工具,支持多种协议与功能。它无需公网IP或复杂配置,可实现远程桌面、文件共享和私有云搭建等。项目采用Go语言开发,具有高性能与轻量级特点,适用于远程办公、物联网管理等场景。其核心亮点包括零配置穿透、多级代理网络、实时流量监控及企业级安全特性。对比同类工具,“nps”在安装复杂度、协议支持和管理界面方面表现优异。项目地址:https://github.com/ehang-io/nps。
811 12
|
6月前
|
域名解析 网络协议 Linux
网络基础知识与配置
本文介绍了网络基础知识,涵盖网络概念、协议、拓扑结构及IP地址等内容。网络是由计算机设备通过通信线路连接而成的系统,用于资源共享与信息传递。文中详细解析了TCP/IP协议族(如IP、TCP、UDP)、常见应用层协议(如HTTP、FTP、SMTP、DNS)的功能与应用场景。同时,阐述了多种网络拓扑结构(总线型、星型、环型、树型、网状)的特点与优缺点。此外,还讲解了IP地址分类、子网掩码的作用,以及如何在Windows和Linux系统中配置网络接口、测试连通性(Ping、Traceroute)和查看默认网关与路由表的方法。这些内容为理解和管理计算机网络提供了全面的基础知识。
469 6
|
6月前
|
安全 网络协议 网络安全
当虚拟机出现网络连接问题时,应该先检查Hyper-V的网卡连接配置
当虚拟机出现网络连接问题时,应首先检查Hyper-V的网卡配置。具体步骤包括:确认虚拟机运行状态、检查虚拟交换机类型和物理网卡连接、确保虚拟机网络适配器正确连接到虚拟交换机,并验证网络配置(IP地址等)。常见问题如虚拟交换机配置错误、网络适配器未连接或防火墙阻止连接,可通过重新配置或调整设置解决。必要时重启虚拟机和宿主机,查看事件日志或联系技术支持以进一步排查问题。
|
6月前
|
监控 安全 网络协议
Hyper V上网实战:多虚拟机网络环境配置
在Hyper-V环境中配置多虚拟机网络以实现上网功能,需完成以下步骤:1. 确认Hyper-V安装与物理网络连接正常;2. 配置虚拟交换机(外部、内部或专用)以支持不同网络需求;3. 设置虚拟机网络适配器并关联对应虚拟交换机;4. 验证虚拟机网络连接状态;5. 根据场景需求优化多虚拟机网络环境。此外,还需注意网络隔离、性能监控及数据备份等事项,确保网络安全稳定运行。
|
6月前
|
虚拟化 网络虚拟化 Windows
导入虚拟机到Hyper-V环境时,理解并配置网络适配器设置是确保网络通信的关键
在Hyper-V环境中,正确配置虚拟机的网络适配器是确保其网络通信的关键。需先启用Hyper-V功能并创建虚拟交换机。接着,在Hyper-V管理器中选择目标虚拟机,添加或配置网络适配器,选择合适的虚拟交换机(外部、内部或私有),并根据需求配置VLAN、MAC地址等选项。最后,启动虚拟机并验证网络连接,确保其能正常访问外部网络、与主机及其他虚拟机通信。常见问题包括无法访问外部网络或获取IP地址,需检查虚拟交换机和适配器设置。
|
2月前
|
运维 监控 安全
从实践到自动化:现代运维管理的转型与挑战
本文探讨了现代运维管理从传统人工模式向自动化转型的必要性与路径,分析了传统运维的痛点,如效率低、响应慢、依赖经验等问题,并介绍了自动化运维在提升效率、降低成本、增强系统稳定性与安全性方面的优势。结合技术工具与实践案例,文章展示了企业如何通过自动化实现运维升级,推动数字化转型,提升业务竞争力。
|
11月前
|
运维 Linux Apache
,自动化运维成为现代IT基础设施的关键部分。Puppet是一款强大的自动化运维工具
【10月更文挑战第7天】随着云计算和容器化技术的发展,自动化运维成为现代IT基础设施的关键部分。Puppet是一款强大的自动化运维工具,通过定义资源状态和关系,确保系统始终处于期望配置状态。本文介绍Puppet的基本概念、安装配置及使用示例,帮助读者快速掌握Puppet,实现高效自动化运维。
208 4
|
6月前
|
机器学习/深度学习 人工智能 运维
机器学习+自动化运维:让服务器自己修Bug,运维变轻松!
机器学习+自动化运维:让服务器自己修Bug,运维变轻松!
253 14
|
8月前
|
机器学习/深度学习 人工智能 运维
基于AI的自动化事件响应:智慧运维新时代
基于AI的自动化事件响应:智慧运维新时代
351 11

热门文章

最新文章