Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using ansible to manage disk space

Tags:

ansible

Simple ask: I want to delete some files if partition utilization goes over a certain percentage.

I have access to "size_total" and "size_available" via "ansible_mounts". i.e.:

ansible myhost -m setup -a 'filter=ansible_mounts'
myhost | success >> {
"ansible_facts": {
    "ansible_mounts": [
        {
            "device": "/dev/mapper/RootVolGroup00-lv_root", 
            "fstype": "ext4", 
            "mount": "/", 
            "options": "rw", 
            "size_available": 5033046016, 
            "size_total": 8455118848
        }, 

How do I access those values, and how would I perform actions conditionally based on them using Ansible?

like image 454
thermans Avatar asked Nov 17 '14 21:11

thermans


People also ask

How do I make an Ansible LVM file?

Defined with tasks to create file systems, mount, fstab entry. Create Volume group with each 8 MB in PE size. New logical volume from the volume group by using 100% of the space in VG. Mount the newly created filesystem with required mount options.

What is a playbook Ansible?

Ansible Playbooks are lists of tasks that automatically execute against hosts. Groups of hosts form your Ansible inventory. Each module within an Ansible Playbook performs a specific task. Each module contains metadata that determines when and where a task is executed, as well as which user executes it.


2 Answers

In my case, all I care about is the root partition. But I found when using the example from frameloss above, that I needed a negated 'or' condition, because each mount point will get tested against the assertion. If more than one mount point existed, then that meant the assertion would always fail. In my example, I'm testing for if the size_available is less than 50% of size_total directly, rather than calculate it as frameloss did.

Secondly, at least in the version of ansible I used, it was necessary to include the {{ }} around the variable in with_items. A mistake that I made that wasn't in the example above was not aligning the 'when' clause at the same indentation as the 'fail' directive. ( If that mistake is made, then the solution does not work... )

# This works with ansible 2.2.1.0
- hosts: api-endpoints
  become: True
  tasks:

    - name: Test disk space available
      assert:
        that:
            - item.mount != '/' or {{ item.mount == '/' and item.size_available > (item.size_total|float * 0.4) }}
      with_items: '{{ ansible_mounts }}'
      ignore_errors: yes
      register: disk_free

    - name: Fail when disk space needs attention
      fail:
         msg: 'Disk space needs attention.'
      when: disk_free|failed
like image 107
Cognitiaclaeves Avatar answered Sep 18 '22 08:09

Cognitiaclaeves


My solution

- name: cleanup logs, free disk space below 20%
  sudo: yes
  command: find /var -name "*.log" \( \( -size +50M -mtime +7 \) -o -mtime +30 \) -exec truncate {} --size 0 \;
  when: "item.mount == '/var' and ( item.size_available < item.size_total * 0.2 )"
  with_items: ansible_mounts

This will truncate any *.log files on the volume /var that are either older than 7 days and greater than 50M or older than 30 days if the free disk space falls below 20%.

like image 45
Era Avatar answered Sep 18 '22 08:09

Era