【发布时间】:2023-01-18 01:54:10
【问题描述】:
如果文件是人类可读的,我需要检查 ansible,即 tail -500f <filename> 应该可以工作。
有没有办法检查tail 的文件是否有人类可读的数据?
如果没有,我希望使用 ansible 的 fail 模块并使 Play 失败。
在 shell 脚本中 -f 和 -r 有助于确定但不确定如何在 ansible 中检查相同的内容。
我在可读文件中看到了 stat 模块,但我不确定哪个 ansible 模块/属性可以帮助实现我的要求。
玩:
- hosts: localhost
gather_facts: no
tasks:
- name: Get stats of a file
ansible.builtin.stat:
path: ~/notes.txt
register: st
- name: displayx
debug:
msg: "{{ st }}"
输出:
PLAY [localhost] *********************************************************************
TASK [Get stats of a file] ***********************************************************
Tuesday 17 January 2023 07:33:06 -0600 (0:00:00.013) 0:00:00.013 *******
ok: [localhost]
TASK [displayx] **********************************************************************
Tuesday 17 January 2023 07:33:06 -0600 (0:00:00.446) 0:00:00.459 *******
ok: [localhost] => {
"msg": {
"changed": false,
"failed": false,
"stat": {
"atime": 1667926553.8257182,
"attr_flags": "",
"attributes": [],
"block_size": 4096,
"blocks": 8,
"charset": "us-ascii",
"checksum": "f427d59898770c15084a339bb2cd0d7e5354a4d3",
"ctime": 1667918971.8145092,
"dev": 64772,
"device_type": 0,
"executable": false,
"exists": true,
"gid": 64395,
"gr_name": "aces",
"inode": 3529825,
"isblk": false,
"ischr": false,
"isdir": false,
"isfifo": false,
"isgid": false,
"islnk": false,
"isreg": true,
"issock": false,
"isuid": false,
"mimetype": "text/plain",
"mode": "0644",
"mtime": 1667918971.812509,
"nlink": 1,
"path": "/home/wladmin/notes.txt",
"pw_name": "wladmin",
"readable": true,
"rgrp": true,
"roth": true,
"rusr": true,
"size": 700,
"uid": 600000008,
"version": "1489589917",
"wgrp": false,
"woth": false,
"writeable": true,
"wusr": true,
"xgrp": false,
"xoth": false,
"xusr": false
}
}
}
【问题讨论】:
-
在指定人类可读的含义并遵循Find human-readable files 和Finding human-readable files on Unix 之后,最好的方法可能是为此创建自己的Custom Module。这也可以在 Bash 中完成,经过简短的测试后它会提供结果。
标签: file ansible tail human-readable charset