Home > Software design >  How to check if a file is of type Human readable in Ansible
How to check if a file is of type Human readable in Ansible

Time:01-18

I need to check in ansible if the file is human-readable i.e tail -500f <filename> should work.

Is there a way to check if the file to be tail has human readable data?

If not, I wish to use ansible's fail module and fail the Play.

In shell scripting -f and -r helps determine but not sure how to check the same in ansible.

I saw the stat module on a readable file but I'm not sure which ansible module/attribute can help achieve my requirement.

Play:

- 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 }}"

Output:

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
        }
    }
}

CodePudding user response:

There is no module in Ansible which will work out-of-box. This will leave you with the shell module, or depending on your infrastructure and other capabilities, you might be able to create a Custom Module written in Bash or Shell as a wrapper for a specific find command.

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

Using the mentioned approach and then a minimal playbook

---
- 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 }}"

will provide the files with the specified properties in a list result set.

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

Mainly Based On

Further Reading

  • Related