How to update proxy if disk space is not used more then 70% or else play will fail by ansible

Viewed 50
---
- name: This playbook is for Testing Disk Space 
  hosts: proxy
  become: yes
  become_method: sudo
  serial: 1
  any_errors_fatal: true
  ignore_errors: yes
  gather_facts: yes
  vars:
          ansible_paython_interpreter: /usr/bin/python
  tasks:


       - name: disk usage from command module
         shell: df -h / | tail -n 1 | awk '{ print $5 }'

         register: used_space

       - debug:
           var: used_space.stdout

       - name: update the system
         yum:
           name: "*"
           state: latest
           exclude: 'kernel*'
           update_cache: yes
           update_only: yes
         when: ansible_os_family == 'RedHat' and used_space < '70%'
         failed_when: used_space >= '70%'
         register: yum_update

       - debug:
           msg: "{{ yum_update }}"

`` Help me if their is other way to do my task . I want to check the disk space and if space is not used more then 70% then continue to update or else fail the play and exit and send email that only this much space left.

1 Answers

this playbook shows an example of int test and use a block/rescue:

- hosts: localhost
  gather_facts: false

  tasks:
    - block:
      - name: disk usage from command module
        shell: df -h / | tail -n 1 | awk '{ print $5 }'
        register: used_space
        failed_when: used_space.stdout | replace('%', '')|int < 70

      - debug:
          msg: "space = {{used_space.stdout | replace('%', '')|int }}"
      rescue:
      - debug:
          msg: "do what you want"

if a task is failed inside block, you are using rescue task

Related