Ubuntu 在Ansible中检测并装载文件系统

Ubuntu 在Ansible中检测并装载文件系统,ubuntu,ansible,mount,Ubuntu,Ansible,Mount,我是ansible的新手,尝试检测文件系统并在存在时进行装载。我浏览了以下链接:- 1. https://docs.ansible.com/ansible/latest/modules/filesystem_module.html 2. https://docs.ansible.com/ansible/latest/modules/mount_module.html 我已手动连接了一个硬盘驱动器,该硬盘驱动器被命令fdisk-l检测为“/dev/sdb”。我需要ansible代码来检测这个文件

我是ansible的新手,尝试检测文件系统并在存在时进行装载。我浏览了以下链接:-

1. https://docs.ansible.com/ansible/latest/modules/filesystem_module.html
2. https://docs.ansible.com/ansible/latest/modules/mount_module.html
我已手动连接了一个硬盘驱动器,该硬盘驱动器被命令fdisk-l检测为“/dev/sdb”。我需要ansible代码来检测这个文件系统并将其装载到某个位置。运行代码“df-h”时,不会显示已装入的文件系统,也不会出现故障。即使我通过ansible代码列出了所有文件系统或装载点,这个文件系统(/dev/sdb)也没有列出

代码段:

    - name: Create File System
      filesystem:
        fstype: ext4
        dev: "{{ mount_src }}"

    - name: Mount File System
      mount:
        path: "{{ mount_path }}"
        src: "{{ mount_src }}"
        fstype: ext4
        state: mounted

提前感谢,如有任何帮助,我们将不胜感激。

下面的播放将创建已安装设备的列表。如果mount_src未装入,则会创建文件系统并将mount_src装入mount_路径

-hosts:localhost
变量:
mount\u src:/dev/sdb
装载路径:/export
任务:
-名称:创建已安装设备的列表
设定事实:
挂载的_设备:{{ansible_挂载}json_查询('[].device')}
-名称:创建文件系统
文件系统:
fstype:ext4
dev:“{mount_src}}”
何时:mount_src不在mount_设备中
-名称:装载文件系统
安装:
路径:“{mount_path}}”
src:“{mount_src}}”
fstype:ext4
状态:已安装
何时:mount_src不在mount_设备中
(未测试)

Ansible不收集有关块设备的事实。在Linux中,可以使用命令
lsblk
,例如

-hosts:localhost
任务:
-名称:获取块设备列表
命令:“lsblk-lno NAME”
登记:结果
-名称:创建可变块_设备
设定事实:
块_设备:“{results.stdout_lines}”
-调试:
块设备
给予

ok:[127.0.0.1]=>
块设备:
-sda
-sda1
-sda2
-sda3
-sda5
-sdb
-sdb1
-sdb9
-mmcblk0
-mmcblk0p1