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

时间:2018-04-02 10:32:33

标签: ansible ubuntu-16.04 readonly ansible-2.x mount-point

我正在尝试检测特定文件系统是否在Linux上以只读或读写方式挂载(Ubuntu 16.04)。使用 stat 模块将无法工作,因为它始终返回posix权限,无论实际写入目录的能力如何。我可以使用下面相当具有侵入性和繁琐的代码来完成此任务,该代码尝试创建一个点文件。我很欣赏一个更干净,更优雅的替代方案,它还可以检测目录是否不是一个挂载点(这将是一个错误)。

- name: Determine whether we have write access to the shared dir
    command: touch /mnt/shared-data/.WriteTest
    register: shared_dir_write_test
    failed_when: "shared_dir_write_test.rc != 0 and 'read-only' not in (shared_dir_write_test.stderr | lower)"
    changed_when: shared_dir_write_test.rc == 0

Ansible建议我使用 state = touch 代替文件模块,但下面的代码失败,因为似乎没有办法检查临时文件的结果

  - name: Determine whether we have write access to the shared dir
    file: path=/mnt/shared-data/.WriteTest state=touch
    register: shared_dir_write_test
    failed_when: "shared_dir_write_test.failed and 'read-only' not in (shared_dir_write_test.msg | lower)"
  

条件检查'shared_dir_write_test.failed和'只读'   不在(shared_dir_write_test.stderr | lower)'失败。错误是:   评估条件时出错(shared_dir_write_test.failed和   '只读'不在(shared_dir_write_test.stderr | lower)):'dict   对象'没有属性'失败'

2 个答案:

答案 0 :(得分:1)

该信息可以从Ansible事实中获得。完成此任务的Ansible代码:

- 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:

when: "'rw' in shared_dir_access_flags"

when: "'ro' in shared_dir_access_flags"

我之前使用的另一种更简洁但可能不太干净的方法是从/ proc / self / mountinfo获取信息。比我希望的更具有平台性,但它只取决于记录的表面。

- 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的表达式会变得有点麻烦:

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

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

答案 1 :(得分:1)

您可以收集Ansible事实,每个mount都有挂载选项:

ansible localhost -m setup -a "filter=ansible_mounts"

示例输出:

[root@ansible ansible]# ansible localhost -m setup -a "filter=ansible_mounts"
localhost | SUCCESS => {
    "ansible_facts": {
        "ansible_mounts": [
            {
                "block_available": 3007928, 
                "block_size": 4096, 
                "block_total": 3929600, 
                "block_used": 921672, 
                "device": "/dev/mapper/fedora-root", 
                "fstype": "xfs", 
                "inode_available": 7787042, 
                "inode_total": 7864320, 
                "inode_used": 77278, 
                "mount": "/", 
                "options": "rw,relatime,attr2,inode64,noquota", 
                "size_available": 12320473088, 
                "size_total": 16095641600, 
                "uuid": "5faf23bb-281b-41d9-bd20-f8da6463eba0"
            }, 
            {
                "block_available": 185772, 
                "block_size": 4096, 
                "block_total": 249830, 
                "block_used": 64058, 
                "device": "/dev/sda1", 
                "fstype": "ext4", 
                "inode_available": 65179, 
                "inode_total": 65536, 
                "inode_used": 357, 
                "mount": "/boot", 
                "options": "rw,relatime,data=ordered", 
                "size_available": 760922112, 
                "size_total": 1023303680, 
                "uuid": "866845e7-6c62-41c9-bbd2-87b463326601"
            }
        ]
    }, 
    "changed": false
}
[root@ansible ansible]# ansible localhost -m setup -a "filter=ansible_mounts"