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?
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.
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: