如何使用ansible获取挂载或未挂载的目录信息?

How to get information of directory that is mounted or not mounted using ansible?

所以我试图从 /tmp 目录中获取事实,以获取是否安装了 sized 的结果以及权限文件。所以我使用了 find 模块,它给了我结果,但不是我要找的东西。 例如:如果我的临时目录是 50gb,权限是 777,结果应该显示目录名称、安装大小和权限代码。所以我想要的是显示临时目录文件夹的完整大小,而不是每个信息文件。

- name: "get the facts"
  find:
    path: /tmp
    file_type: directory
    recurse: no
    size: 50g
  register: find_result
- name: "print the result"
  debug: var=find_result

Q: Get result of size, is being mounted or not, and permission.

A:使用stat获取大小和权限。变量 ansible_mounts 是挂载点列表。例如 play

- hosts: localhost
  gather_facts: True
  vars:
    my_dir: /mnt
  tasks:
    - stat:
        path: "{{ my_dir }}"
      register: result
    - debug:
        msg: "size:{{ result.stat.size }} mode:{{ result.stat.mode }}"
    - debug:
        msg: "{{ my_dir }} is mount-point"
      when: my_dir in ansible_mounts|json_query('[].mount')
    - debug:
        msg: "{{ my_dir }} is not mount-point"
      when: my_dir not in ansible_mounts|json_query('[].mount')

给予

"msg": "size:32768 mode:0755"
"msg": "/mnt is mount-point"
skipping: [localhost]

备注

  • 如果设置了变量 gather_facts True,Ansible 将创建变量 ansible_mounts .