【问题标题】:How to check if a file is of type Human readable in Ansible如何在 Ansible 中检查文件是否属于人类可读类型
【发布时间】: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
        }
    }
}

【问题讨论】:

标签: file ansible tail human-readable charset


【解决方案1】:

Ansible 中没有开箱即用的模块。这将为您留下 shell 模块,或者根据您的基础架构和其他功能,您可以创建一个用 Bash 或 Shell 编写的自定义模块作为特定 find 命令的包装器。

find $path -type f -exec grep -Iq . {} ; -printf '%P
'

使用提到的方法,然后使用最小的剧本

---
- hosts: localhost
  become: false
  gather_facts: false

  tasks:

  - name: Get human-readable files
    human_readable:
      path: "/home/{{ ansible_user }}/test/library"
    register: result

  - name: Show result
    debug:
      msg: "{{ result }}"

  - name: Show human-readable files
    debug:
      msg: "{{ item | basename }}"
    loop: "{{ result.stdout_lines }}"

将在列表结果集中提供具有指定属性的文件。

TASK [Show human-readable files] *************
ok: [localhost] => (item=size.sh) =>
  msg: size.sh
ok: [localhost] => (item=human_readable.sh) =>
  msg: human_readable.sh
ok: [localhost] => (item=between.sh) =>
  msg: between.sh
ok: [localhost] => (item=icmp_ping.py) =>
  msg: icmp_ping.py
ok: [localhost] => (item=hardware_facts.py) =>
  msg: hardware_facts.py

主要依据

进一步阅读

【讨论】:

    猜你喜欢
    • 2012-10-02
    • 1970-01-01
    • 2018-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-09
    相关资源
    最近更新 更多