I am newbie to Ansible, i failed using ansible via command that way :

ansible host -a "/sbin/swapoff -a && /sbin/swapon -a"

Only this worked

ansible host -a "/sbin/swapoff -a
ansible host -a "/sbin/swapon -a

I am thinking of making a playbook, starting maybe with this:

- name: Turn off swap
  shell : "swapoff -a"

- name: Turn on swap
  shell : "swapon -a"

I would like to reset swap space with a condition, only when the swap used space is greater than 100mb. Is it possible to do that ?

Many thanks for your help


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:


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 => {
    "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 < 500

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

  • Many thanks for your answer, i need to check how i can use handler or task. It's very new for me. About one simple playbook i try that : ``` - hosts: host-test-bdd1 tasks: - name: We desactivate swap command: swapoff -a when: ansible_swap_free_mb < 1600 - name: We active swap command: swapon -a ``` But i still don't know where to put the condition "when" exactly – Peutre Oct 28 '19 at 12:58

I have a solution on that way :

- hosts: hostnametest
   - name: we take swap space used (megabytes)
     shell : free -m | grep Swap | awk '{print $3}'
     register: swap_used

   - name: Turn off swap
     shell: "swapoff -a"
     when: (swap_used.stdout_lines[0] | int) > 100

   - name: Turn on swap
     shell: "swapon -a"
     when: (swap_used.stdout_lines[0] | int) > 100
  • 1
    Your first task should only be marked as changed when the threshold you want is exceeded. So move the "when" clause to a "changed_when" for the first task. – M_dk Oct 31 '19 at 9:30
  • 1
    Thanks for the comment, really perfect, i just did the "when" to "changed_when" for the first task, it works well ! Nice to learn this "changed_when" :) – Peutre Oct 31 '19 at 10:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.