How to fail Ansible playbook if filesize is zero (0)

99 Views Asked by At

How this playbook should work? I would like playbook to fail when filesize is zero

---
- hosts: localhost
  gather_facts: no

  tasks:
    - name: "Get stats of a file"
      stat:
        path: files/file1.txt
      register: file1

    - name: "Fail if the filesize 0"
      fail:
        msg: "error: filesize is 0"
      when: file1.stat.size == '0'

In practice task is just being skipped:

ansible-playbook 1.yml -i inventory-it1 --diff

PLAY [localhost] ********************************************************************************************************************************************************************

TASK [Get stats of a file] **********************************************************************************************************************************************************
ok: [localhost]

TASK [Fail if the filesize 0] *******************************************************************************************************************************************************
skipping: [localhost]

PLAY RECAP **************************************************************************************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0

Expected playbook to fail

2

There are 2 best solutions below

0
U880D On

The size is already of type int. Whereby your conditional statement tries to do something like when: int == 'string' which will be never True.

A minimal example playbook

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

  tasks:

  - name: "Get stats of a file"
    stat:
      path: test.file
    register: test

  - name: "Show size and type"
    debug:
      msg: "size is {{ test.stat.size }} bytes and of type {{ test.stat.size | type_debug }}"

  - name: "Fail if the filesize 0"
    fail:
      msg: "error: filesize is 0"
    when: test.stat.size == 0

will result into an output of

TASK [Get stats of a file] ************************************************
ok: [localhost]

TASK [Show size and type] *************************************************
ok: [localhost] =>
  msg: size is 0 bytes and of type int

TASK [Fail if the filesize 0] *********************************************
fatal: [localhost]: FAILED! => changed=false
  msg: 'error: filesize is 0'

PLAY RECAP ****************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=1

Since Ansible can cast data types, it would even be possible simpler

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

  vars:

    int: 0

  tasks:

  - fail:
      msg: "is 0"
    when: not int # is zero

Somehow Similar Q&A

1
Vladimir Botka On

For example,

shell> cat pb.yml
- hosts: localhost

  tasks:

    - assert:
        that: lookup('pipe', cmd)|int != 0
        fail_msg: '[ERR] file is empty.'
        success_msg: '[OK]  file is not empty.'
      vars:
        cmd: stat -c %s /tmp/file1.txt

if the file is empty the play fails

shell> cat /dev/null > /tmp/file1.txt 
shell> ansible-playbook pb.yml

PLAY [localhost] ******************************************

TASK [assert] *********************************************
fatal: [localhost]: FAILED! => changed=false 
  assertion: lookup('pipe', cmd)|int != 0
  evaluated_to: false
  msg: '[ERR] file is empty.'
  ...

, otherwise

shell> cp /etc/passwd /tmp/file1.txt 
shell> ansible-playbook pb.yml

PLAY [localhost] ****************************************

TASK [assert] *******************************************
ok: [localhost] => changed=false 
  msg: '[OK]  file is not empty.'
  ...

Note: The lookup plugins work on the localhost only.