How best to compare filesystem size against another number?

Hi there!

I’m trying to get the size of a filesystem (/opt in this case) and ensure it is larger than another number (3Gb). Here’s what I have in my test playbook:

---
- name: Assert that /opt has more than 3GB available
  hosts: localhost
  gather_facts: yes
  tasks:

    - name: Set available bytes for /opt
      set_fact:
        opt_available_bytes: "{{ item.size_available | int }}"
      loop: "{{ ansible_mounts }}"
      when: item.mount == '/opt'

    - name: Assert /opt has more than 3GB free
      assert:
        that:
          - opt_available_bytes > (3 * 1024 * 1024 * 1024)
        fail_msg: "/opt has less than 3GB free ({{ opt_available_bytes }} bytes)"
        success_msg: "/opt has sufficient space ({{ opt_available_bytes }} bytes)"

But when I run it, I get:

'>' not supported between instances of 'AnsibleUnsafeText' and 'int'. '>' not supported between instances of 'AnsibleUnsafeText' and 'int'"}

I’m sure it’s something stupid that I’m doing but I thought the “{{ item.size_available | int }}” sets that fact to be an int but apparently not. What is the right way to do this?

Thanks!

Use type casting on consumption, 2.19 and later Ansible core will preserve types better, but for all other versions templating tends to convert to text.

   assert:
        that:
          - opt_available_bytes|int > (3 * 1024 * 1024 * 1024)

1 Like

I threw a debug statement in to verify what the opt_available_bytes variable type was being stored as and even though it’s being passed to | int, it’s being stored as AnsibleUnsafeText:

TASK [debug] **********************
ok: [localhost] => {
    "msg": "opt_available_bytes type: AnsibleUnsafeText"
}

Just wanted to provide that information if it’s of any use.

That was it!!! Thank you SO much! Yeah, this box is running v2.17.12.

Thank you again!

test case:

- hosts: localhost
  gather_facts: false
  tasks:
      - set_fact:
          benumber: '{{ 3 * 4}}'

      - debug: msg={{ benumber |type_debug }}

Under 2.18:

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

TASK [set_fact] **************************************************************************************************************************************************************
ok: [localhost]

TASK [debug] *****************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": "str"
}

PLAY RECAP *******************************************************************************************************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

Under 2.19

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

TASK [set_fact] **************************************************************************************************************************************************************
ok: [localhost]

TASK [debug] *****************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": "int"
}

PLAY RECAP *******************************************************************************************************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0