【问题标题】:How to get information of directory that is mounted or not mounted using ansible?如何获取使用ansible挂载或未挂载的目录信息?
【发布时间】:2019-09-17 19:38:38
【问题描述】:

所以我试图从 /tmp 目录中获取事实,以获取大小是否已安装和权限文件的结果。所以我使用了 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

【问题讨论】:

  • 好吧,也许解释不够清楚,但你在这里告诉的是:假设我是一个名为 /tmp 的文件夹,大小为 50Go,权限为 777,打印我的名称、大小和允许。那么答案总是/tmp; 50Go; 777

标签: ansible mounted-volumes


【解决方案1】:

问:获取大小、是否挂载、权限的结果。

答:使用stat 获取大小和权限。变量 ansible_mounts 是挂载点的列表。比如戏

- 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

【讨论】:

    猜你喜欢
    • 2015-09-11
    • 2014-07-20
    • 1970-01-01
    • 1970-01-01
    • 2014-04-15
    • 2011-01-15
    • 2011-10-13
    • 1970-01-01
    • 2017-03-15
    相关资源
    最近更新 更多