Bootstrap FreeKB - Ansible - Blank out a file using the command or shell module
Ansible - Blank out a file using the command or shell module

Updated:   |  Ansible articles

Let's say the foo.txt file contains a few lines of text, like this.

[root@server1 ~] cat foo.txt
Line 1
Line 2
Line 3

 

The ls (list) command shows that the file contains 21 bytes of data.

[root@server1 ~]$ ls -l foo.txt
-rw-r--r-- 1 root root 21 Jun 22 19:23 foo.txt

 

The shell module can used to completely blank out the file, including all whitespace and empty lines, so that the file is 0 bytes.

---
- hosts: all
  tasks:
  - name: blank out foo.txt
    ansible.builtin.shell: "> /tmp/foo.txt"
    register: out

  - name: get the stats of foo.txt
    ansible.builtin.stat:
      path: /tmp/foo.txt
    register: out

  - name: display the stats of foo.txt
    ansible.builtin.debug:
      var: out
...

 

Running this playbook should return something like this. Notice in this example that size is 0 (bytes).

TASK [blank out foo.txt] 
changed: [server1.example.com]

TASK [get the stats of foo.txt] 
ok: [server1.example.com]

TASK [display the stats of foo.txt]
ok: [server1.example.com] => {
    "stat": {
        "changed": false,
        "failed": false,
        "stat": {
            "atime": 1624409036.2991908,
            "attr_flags": "",
            "attributes": [],
            "block_size": 4096,
            "blocks": 0,
            "charset": "binary",
            "checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709",
            "ctime": 1624409116.359043,
            "dev": 64768,
            "device_type": 0,
            "executable": false,
            "exists": true,
            "gid": 10,
            "gr_name": "wheel",
            "inode": 50466431,
            "isblk": false,
            "ischr": false,
            "isdir": false,
            "isfifo": false,
            "isgid": false,
            "islnk": false,
            "isreg": true,
            "issock": false,
            "isuid": false,
            "mimetype": "inode/x-empty",
            "mode": "0644",
            "mtime": 1624409116.359043,
            "nlink": 1,
            "path": "/tmp/foo.txt",
            "pw_name": "jeremy.canfield",
            "readable": true,
            "rgrp": true,
            "roth": true,
            "rusr": true,
            "size": 0,
            "uid": 1000,
            "version": "1050713058",
            "wgrp": false,
            "woth": false,
            "writeable": true,
            "wusr": true,
            "xgrp": false,
            "xoth": false,
            "xusr": false
        }
    }
}

 

Or like this to only display the size.

---
- hosts: all
  tasks:
  - name: blank out foo.txt
    ansible.builtin.shell: "> /tmp/foo.txt"
    register: out

  - name: get the stats of foo.txt
    ansible.builtin.stat:
      path: /tmp/foo.txt
    register: out

  - name: display the size of foo.txt
    ansible.builtin.debug:
      var: out.stat.size
...

 

Which should return the following, showing that foo.txt is 0 bytes.

TASK [blank out foo.txt] 
changed: [server1.example.com]

TASK [get the stats of foo.txt] 
ok: [server1.example.com]

TASK [display the size of foo.txt]
ok: [server1.example.com] => {
    "out.stat.size": "0"
}

 

Let's say the foo.txt file contains a few lines of text, like this.

[root@server1 ~] cat foo.txt

 

And the ls command shows that the file is now 0 bytes.

[root@server1 ~]$ ls -l foo.txt
-rw-r--r-- 1 root root 0 Jun 22 19:31 foo.txt

 




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 623ff0 in the box below so that we can be sure you are a human.