0
  - name: check for file size
    stat: 
       path: /apps/InstallDir/data
    register: myDir

  - debug: var=myDir.stat.size

how to get output in required format MB or GB and debug msg when it's greater than threshold

  • 2
    Hi Sravan Kumar welcome to SO. You will need to [edit your question](https://stackoverflow.com/posts/68923986/edit) and include more specifics, ideally including your own attempt and the error that it is producing for you. This is not a code writing website, it's for helping you troubleshoot your own code, of which there is very little that relates to your question text. Good luck – mdaniel Aug 25 '21 at 15:42

1 Answers1

1

I understand that the main question here is: "How to convert, How to output in MB, MiB, GB or GiB, instead of bytes?".

You achieve this by using constructs like

- name: Check file size
  stat:
    path: "/home/{{ ansible_user }}/testfile"
  register: file_size
  tags: check_fs

- name: Report file size
  debug:
    msg:
      - "{{ ( file_size.stat.size / 1024 / 1024 ) | int }}MiB"
      - "{{ ( file_size.stat.size / 1024 | pow(2) ) | round | int }}MiB"
      - "{{ file_size.stat.size | filesizeformat(True) }}"
      - "{{ file_size.stat.size | filesizeformat }}"
  tags: check_fs

Credits to

U880D
  • 8,601
  • 6
  • 24
  • 40