Ansible resetting swap linux
You can certainly do this through a handler or task, but first you need to generate the trigger conditions. The swap space is in the Ansible facts that are returned when setup: is run. From the Ansible docs:


  Facts
  
  These are variables that contain information pertinent to the current host (inventory_hostname). They are only available if gathered first.
  
  ansible_facts
     Contains any facts gathered or cached for the inventory_hostname Facts are normally gathered by the setup module automatically in a play, but any module can return facts.
  
  ansible_local
     Contains any ‘local facts’ gathered or cached for the inventory_hostname. The keys available depend on the custom facts created. See the setup module for more details.


As an example:

ansible -i localhost, all -c local -m setup -a filter=*swap*
localhost | SUCCESS =&gt; {
    "ansible_facts": {
        "ansible_swapfree_mb": 437,
        "ansible_swaptotal_mb": 979
    },
    "changed": false
}


In your playbook your task to reset swap should then have the conditional like:

when: ansible_swap_free_mb &lt; 500

In your case, however you want to trigger on swap used, so you would have to compute the difference between total and free.