ansible - 如何检查文件列表的大小

标签 ansible

使用 Ansible,我需要检查目录中的文件是否大于 50M。 我用 ls 的结果做了一个 set_fact,然后我循环统计所有文件。这可行,但当文件大于 50M 时,我无法解析结果以创建失败条件。

我这样做:

  - shell: ls -1 "{{ logs_directory }}"*.log  
    register: list_logs
  - set_fact:
      list_logs: "{{ list_logs.stdout_lines }}"
  - debug:
      msg: "{{ list_logs}}"
  
  - name: Get size
    stat:
      path: "{{ item }}"
    with_items: "{{ list_logs }}"
    register: size_log

  - name: debug
    fail:
       msg: "Problem with log size > 50M"
    when: 'size_log.stat.size / 1024 / 1024 | int > 50'

如果我只有 1 个文件,它可以工作,但是如果有多个服务器和多个文件,它就不能工作...如何解析 size_log_result

这里是一个输出 size_log 的例子:

ok: [192.168.1.2] => {
    "msg": {
        "changed": false,
        "msg": "All items completed",
        "results": [
            {
                "ansible_loop_var": "item",
                "changed": false,
                "failed": false,
                "invocation": {
                    "module_args": {
                        "path": "/var/logs/admin.log"
                    }
                },
                "item": "/var/logs/admin.log",
                "stat": {
                      "size": 21711,
                }
            },
            {
                "ansible_loop_var": "item",
                "changed": false,
                "failed": false,
                "invocation": {
                    "module_args": {
                          "path": "/var/logs/database.log"
                    }
                },
                "item": "/var/logs/database.log",
                "stat": {
                    "size": 11162,
                }          
            }
        ]
    }
}
ok: [192.168.1.5] => {
    "msg": {
        "changed": false,
        "msg": "All items completed",
        "results": [
            {
                "ansible_loop_var": "item",
                "changed": false,
                "failed": false,
                "invocation": {
                    "module_args": {
                             "path": "/var/logs/database.log"
                    }
                },
                "item": "/var/logs/database.log",
                "stat": {
                    "size": 128453958,
                }
            }
        ]
    }
}

最佳答案

在我看来,您是在重新发明轮子,而 find module似乎已经完美地回答了您的用例。

给定:

- hosts: localhost
  gather_facts: no

  tasks:
    ####
    ## You do not need this task, it is just to create a file
    ## big enough for demonstration purpose
    ####
    - community.general.filesize:
        path: /var/log/heavy.log
        size: 51m

    - find:
        paths: /var/log
        patterns: '*.log'
        size: 51m
      register: _logs

    - fail:
        msg: >- 
          Due to file(s): {{ 
            _logs.files | map(attribute='path') | join(', ') 
          }}
      when: _logs.files | length > 0

这将产生:

TASK [community.general.filesize] *********************************
ok: [localhost]

TASK [find] *******************************************************
ok: [localhost]

TASK [fail] *******************************************************
fatal: [localhost]: FAILED! => changed=false 
  msg: 'Due to file(s): /var/log/heavy.log'

关于ansible - 如何检查文件列表的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70259783/

相关文章:

ansible - 如何检查文件是否已在ansible中下载

azure - 我们可以对同一存储库的不同文件夹中的两个 main.yml 使用变量吗

python-3.x - 如何在 ubuntu 18.04 上安装 ansible 2.9+ 并使用 python3?

ansible - 使用 ansible 插入 etc 主机文件

deployment - 如何使用 Ansible 强制重新安装软件包?

docker - 通过 ansible 模板化构建容器以使用配置文件

mysql - 如何使用 Ansible 创建大型数据库

environment-variables - Ansible - 包括来自外部 YML 的环境变量

ansible - 如何为特定任务覆盖 ansible_become?

ansible:播放回顾:这些数字是什么意思?