且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Ansible:检测Linux文件系统是否以只读方式安装

更新时间:2023-11-29 13:42:16

信息可以从Ansible事实中获取. Ansible代码完成了此任务:

The information can be obtained from Ansible facts. Ansible code that accomplishes this:

- name: Determine shared-dir mount point
command: "/usr/bin/env stat -c '%m' {{ shared_dir_real_path }}"
register: shared_dir_mount_point
changed_when: False

- name: Determine the mount point's filesystem type and mount options
set_fact:
    "shared_dir_mount_{{ item }}": "{{ ansible_mounts | selectattr('mount', 'equalto', shared_dir_mount_point.stdout) | map(attribute = item) | join(',') }}"
with_items:
    - fstype
    - options

- name: Determine the access to the shared-data directory
set_fact:
    shared_dir_access_flags: "{{ ['ro', 'rw']  | intersect( shared_dir_mount_options.split(',') )}}"

- name: Verify Access mode sanity
assert:
    that: shared_dir_access_flags | length == 1

然后确定安装座是R/W还是我使用的R/O:

Then to determine whether the mount is R/W or R/O I use:

when: "'rw' in shared_dir_access_flags"

when: "'ro' in shared_dir_access_flags"

我以前使用的另一种更简洁但也许不太干净的方法是从/proc/self/mountinfo获取信息.平台特定的功能要比我希望的要多一些,但这仅取决于记录在案的接口.

Another, more terse but perhaps less clean approach that I used previously, was to obtain the information from /proc/self/mountinfo. A little more platform-specific than I hoped, but it only depends on documented intrefaces.

- name: Get Shared dir mount options
shell: "grep -F `stat -c '%m' {{ shared_dir_path }}` /proc/self/mountinfo | cut -d' ' -f 6"
register: shared_dir_mount_options
changed_when: False

然后使用表达式确定挂载是R/W还是R/O,我会变得更加麻烦:

Then the expressions to determine whether the mount is R/W or R/O I would become a bit more cumbersome:

when: "'rw' in shared_dir_mount_options.stdout.split(',')"

when: "'ro' in shared_dir_mount_options.stdout.split(',')"