1

I need to check all the linux servers which servers disk size is less than 10GB and also should display the output if it is in MB , GB , 1TB it should display as the same. I have used the code as below to check disk size in ansible playbook

- name: Get disk info
    set_fact:
      disk_size: "{{ item.value.size }}"      
    with_dict:
      - "{{ ansible_facts['devices'] }}"  
    ignore_errors: yes    
    tags: get_disk_info
  # - debug:
  #     msg: "{{ disk_size }}"
  #   ignore_errors: yes
  #   tags: get_disk_info
  - debug:
      msg: >
           {% if disk_size > 10 %} 
            Disk Size {{ disk_size }} -> PASS 
           {% else %} 
            Disk Size {{ disk_size }} -> FAIL
           {% endif %}
    tags: get_disk_info

if the server has 55MB as disk size it also displaying the output as PASS, but as per above condition in the code it should display as FAIL..

please help me out in this playbook to get PASS if it should greater than 10GB in all the aspects

Suganthan Raj
  • 2,330
  • 6
  • 31
  • 42
  • 55 IS greater than 10 though. try converting everything to bytes before comparing. – mahbad Nov 09 '21 at 17:59
  • if it is 55MB it also displaying as PASS, but it should not display as PASS, it should display as FAIL. – Suganthan Raj Nov 09 '21 at 18:35
  • 3
    right. thats not what the comparison is doing though. as it is, `55 > 10 ?` is going to "pass". converting to bytes before the comparison would be `57671680 > 10737418240 ?` and "fail" – mahbad Nov 09 '21 at 18:46
  • i need to this bytes value to convert into MB,GB, TB if it is MB,GB,TB calcuation in jinja2 template. please help on this – Suganthan Raj Nov 09 '21 at 19:39
  • 1
    Does [How to use arithmetic when setting a variable value in Ansible?](https://stackoverflow.com/questions/33505521/), [Ansible: stat module directory size output in MiB/GiB. How to convert the output?](https://stackoverflow.com/a/69003609/6771046) or `filesizeformat(True)` answer your question? – U880D Nov 09 '21 at 19:44
  • unsupported operand type(s) for /: 'AnsibleUnsafeText' and 'int'" – Suganthan Raj Nov 09 '21 at 19:56

1 Answers1

1

For converting all units to GB, for example,i suggest you to use a custom filter:

you create a folder filter_plugins in your playbook folder (i have named the file myfilters.py and the filter convertToGB)

the filter:

#!/usr/bin/python
import re
class FilterModule(object):
    def filters(self):
        return {
            'convertToGB': self.convertToGB
        }

    def convertToGB(self, obj):
        # keep only digit and . 
        value = float(re.sub('[^\d\.]', '', obj))
        if obj.endswith('GB'):
            return value
        elif obj.endswith('TB'):
            return value * 1024

        return value

you could easily modify the custom plugin...

the playbook:

- name: playbook1.0
  hosts: all
  gather_facts: yes

  tasks:
    - name: Get disk info
      set_fact:
        disk_size: "{{ item.value.size }}"      
      with_dict:
        - "{{ ansible_facts['devices'] }}"  

    - debug:
        msg: >
            {% if (disk_size | convertToGB) > 10 %} 
              Disk Size {{ disk_size }} -> PASS 
            {% else %} 
              Disk Size {{ disk_size }} -> FAIL
            {% endif %}

without custom filter you have to modify your template like this:

  tasks:
    - name: Get disk info
      set_fact:
        disk_size: "{{ item.value.size }}"      
      with_dict:
        - "{{ ansible_facts['devices'] }}"  
      

    - debug:
        msg: >
            {% set value = (disk_size.split(' ')[0] | float) %}
            {% set unit = disk_size.split(' ')[1] %} 
            {% set multiplier = 1.0 %}            
            {% if 'GB' in unit %}
            {%   set multiplier = 1.0 %}
            {% elif 'TB' in unit %}
            {%   set multiplier = 1024 %}
            {% endif %}
            {% if (value * multiplier) > 10 %} 
              Disk Size {{ disk_size }} -> PASS 
            {% else %} 
              Disk Size {{ disk_size }} -> FAIL
            {% endif %}
Frenchy
  • 16,386
  • 3
  • 16
  • 39