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
The
sizeis already of typeint. Whereby your conditional statement tries to do something likewhen: int == 'string'which will be neverTrue.A minimal example playbook
will result into an output of
Since Ansible can cast data types, it would even be possible simpler
Somehow Similar Q&A
whenclause?