Ansible挂载模块(mount module)在执行时默认会检查挂载点的状态,并报告状态是否已挂载。然而,有时我们可能只想检查挂载点的状态,而不希望Ansible报告状态。下面是一种解决方法,可以通过设置register
参数和failed_when
条件来实现:
- name: Check mount status
command: mountpoint -q /mnt/mountpoint
register: mount_status
failed_when: False
- name: Mount the filesystem
mount:
src: /dev/sdb1
path: /mnt/mountpoint
fstype: ext4
state: mounted
when: mount_status.rc != 0
在上述示例中,我们首先使用command
模块执行mountpoint -q /mnt/mountpoint
命令来检查挂载点/mnt/mountpoint
的状态,并将结果保存到mount_status
变量中。然后,我们使用failed_when
条件将failed_when: False
设置为command
模块,这样无论挂载点的状态如何,都不会导致Ansible任务失败。
接下来,我们使用mount
模块来挂载文件系统。通过添加when: mount_status.rc != 0
条件,只有在挂载点未挂载时才会执行挂载操作。
注意:这种方法只是在Ansible中检查挂载状态而不报告状态的一种解决方案。如果你仍然希望在Ansible的输出中看到挂载的状态,请考虑将failed_when
条件去除,或者根据实际需求进行相应的调整。