OPT*_*MUS 3 ubuntu mount ansible
我是 ansible 的新手,尝试检测文件系统并安装(如果存在)。我已浏览以下链接:-
1. https://docs.ansible.com/ansible/latest/modules/filesystem_module.html
2. https://docs.ansible.com/ansible/latest/modules/mount_module.html
Run Code Online (Sandbox Code Playgroud)
我已手动连接硬盘驱动器,命令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
Run Code Online (Sandbox Code Playgroud)
提前致谢,如有任何帮助,我们将不胜感激。
下面的例子创建了一个已安装设备的列表。如果未挂载mount_src ,则创建文件系统并将mount_src挂载到mount_path。
- hosts: localhost
vars:
mount_src: /dev/sdb
mount_path: /export
tasks:
- name: Create list of mounted devices
set_fact:
mounted_devices: "{{ ansible_mounts|json_query('[].device') }}"
- name: Create File System
filesystem:
fstype: ext4
dev: "{{ mount_src }}"
when: mount_src not in mounted_devices
- name: Mount File System
mount:
path: "{{ mount_path }}"
src: "{{ mount_src }}"
fstype: ext4
state: mounted
when: mount_src not in mounted_devices
Run Code Online (Sandbox Code Playgroud)
(未测试)
Ansible 不收集有关块设备的事实。在Linux中,可以使用命令lsblk代替,例如
- hosts: localhost
tasks:
- name: Get list of block devices
command: 'lsblk -lno NAME'
register: results
- name: Create variable block_devices
set_fact:
block_devices: "{{ results.stdout_lines }}"
- debug:
var: block_devices
Run Code Online (Sandbox Code Playgroud)
给出
ok: [127.0.0.1] =>
block_devices:
- sda
- sda1
- sda2
- sda3
- sda5
- sdb
- sdb1
- sdb9
- mmcblk0
- mmcblk0p1
Run Code Online (Sandbox Code Playgroud)