我是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 </ strong>检测为“ / dev / sdb”。我想要ansible代码来检测此文件系统并将其安装在某个位置。运行代码“ df -h ”时不会显示已挂载的文件系统,也不会失败。即使我通过可用的代码列出了所有文件系统或挂载点,该文件系统(/ 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
在此先感谢您,我们将不胜感激。
答案 0 :(得分:0)
下面的播放创建已安装设备的列表。如果未安装 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
(未经测试)
Ansible不会收集有关块设备的事实。示例如何在Linux中创建块设备列表
- hosts: localhost
tasks:
- command: 'lsblk -lno NAME'
register: results
- name: Create list of block devices
set_fact:
block_devices: "{{ block_devices|default([]) + [ item ] }}"
loop: "{{ results.stdout_lines }}"
- debug:
var: block_devices
给予:
ok: [127.0.0.1] =>
block_devices:
- sda
- sda1
- sda2
- sda3
- sda5
- sdb
- sdb1
- sdb9
- mmcblk0
- mmcblk0p1