我有一个 ansible 任务,其中我需要验证我是否拥有所有 1987 个文件,并且所有这些文件都是在 1 分钟内生成的。
- name: check all 1987 files
find: paths=/proc/files
file_type=file
age=-{{ window }}m
age_stamp=mtime
register: files
failed_when: files.matched = total | int
但每当我运行上述任务时,它总是会失败并显示此错误消息。我已经检查过我是否已正确生成所有 1987 个文件。
fatal: [machineA]: FAILED! => {"msg": "The conditional check 'files.matched = total | int' failed. The error was: template error while templating string: expected token 'end of statement block', got '='. String: {% if files.matched = total | int %} True {% else %} False {% endif %}"}
以下是我传递这些参数的方式:
-e 'total=1987' -e 'window=1'
这是我收到的错误:它在 json 数组中显示所有 1987 个文件,所以我将其缩短了。
{ "changed": false, "examined": 1987, "failed_when_result": true, "files": [{ "atime": 1524849737.1382372, "ctime": 1524849737.4822407, "dev": 64785, "gid": 5000, "inode": 55052752, "isblk": false, "ischr": false, "isdir": false, "isfifo": false, "isgid": false, "islnk": false, "isreg": true, "issock": false, "isuid": false, "mode": "0644", "mtime": 1524849737.4822407, "nlink": 1, "path": "/proc/files/abc_1680_log.data", "rgrp": true, "roth": true, "rusr": true, "size": 2687406, "uid": 5000, "wgrp": false, "woth": false, "wusr": true, "xgrp": false, "xoth": false, "xusr": false }], "matched": 1987, "msg": "" }
答案1
这种逻辑似乎是倒退的:
failed_when: files.matched = total | int
它应该是
failed_when: files.matched != total | int
答案2
使用两个等号进行比较:
- name: check all 1987 files
find: paths=/proc/files
file_type=file
age=-{{ window }}m
age_stamp=mtime
register: files
failed_when: files.matched == total | int
Ansible 使用 Jinja 模板,您可以在此处看到比较:http://jinja.pocoo.org/docs/dev/templates/#comparisons