Bootstrap FreeKB - Ansible - Determine if a file or directory is readable or writeable or executable using stat
Ansible - Determine if a file or directory is readable or writeable or executable using stat

Updated:   |  Ansible articles

stat can be used to determine if a file or directory is readable, writable, executable. In this example, the stats of the /tmp/foo.txt file are stored in a variable named out using register and debug is used to print the "out" dictionary.

---
- hosts: all
  tasks:
  - ansible.builtin.stat:
      path: /tmp/foo.txt
    register: out

  - debug:
      var: out
...

 

Which should return something like this. Notice in this output

  • executable: true
  • readable: true
  • writeable: true
TASK [debug] 
ok: [server1.example.com] => {
    "out": {
        "changed": false,
        "failed": false,
        "stat": {
            "atime": 1610186400.7513745,
            "attr_flags": "",
            "attributes": [],
            "block_size": 4096,
            "blocks": 8,
            "charset": "binary",
            "ctime": 1610187202.1764085,
            "dev": 64768,
            "device_type": 0,
            "executable": true,
            "exists": true,
            "gid": 0,
            "gr_name": "root",
            "inode": 50331777,
            "isblk": false,
            "ischr": false,
            "isdir": true,
            "isfifo": false,
            "isgid": false,
            "islnk": false,
            "isreg": false,
            "issock": false,
            "isuid": false,
            "mimetype": "inode/directory",
            "mode": "1777",
            "mtime": 1610187202.1764085,
            "nlink": 10,
            "path": "/tmp/foo",
            "pw_name": "root",
            "readable": true,
            "rgrp": true,
            "roth": true,
            "rusr": true,
            "size": 4096,
            "uid": 0,
            "version": "1309840072",
            "wgrp": true,
            "woth": true,
            "writeable": true,
            "wusr": true,
            "xgrp": true,
            "xoth": true,
            "xusr": true
        }
    }
}

 

The fail module and when parameter with out.stat.writeable can now be used to fail if /tmp/foo.txt is or is not writeable.

---
- hosts: all
  tasks:
  - ansible.builtin.stat:
      path: /tmp/foo.txt
    register: out

  - name: fail when /tmp/foo is not writeable
    ansible.builtin.fail: 
     msg: /tmp/foo is not writeable
    when: out.stat.writeable == false
...

 

If /tmp/foo.txt had writeable: false something like this should be returned.

PLAY [all]

TASK [store the statistics of /tmp/foo in the 'out' variable]
ok: [server1.example.com]

TASK [fail when /tmp/foo is not writeable]
fatal: [server1.example.com]: FAILED => {"changed": false, "msg": "/tmp/foo is not writeable"}

PLAY RECAP
server1.example.com : ok=2  changed=0  unreacable=0  failed=1

 

It is important to recognize that Ansible makes an SSH connection to target systems as a certain user. For more details on this, check out my article Ansible - Getting Started with SSH. For example, let's say the playbook includes remote_user: john.doe. In this scenario, stat will determine if the target file or directory is readable, writeable, executable by john.doe.

---
- hosts: all
  remote_user: john.doe
  tasks:
  - ansible.builtin.stat:
      path: /tmp/foo.txt
    register: out

  - debug:
      var: out
...

 

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 44d405 in the box below so that we can be sure you are a human.