Answers: <p>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 <a href="https://docs.ansible.com/ansible/latest/reference_appendices/special_variables.html#facts" rel="nofollow noreferrer">Ansible facts</a> that are returned when <code>setup:</code> is run. From the Ansible docs:</p>
  
  <blockquote>
    <p>Facts</p>
    
    <p>These are variables that contain information pertinent to the current host (inventory_hostname). They are only available if gathered first.</p>
    
    <p>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.</p>
    
    <p>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.</p>
  </blockquote>
  
  <p>As an example:</p>
  
  <pre><code>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
  }
  </code></pre>
  
  <p>In your playbook your task to reset swap should then have the conditional like:</p>
  
  <p><code>when: ansible_swap_free_mb &lt; 500</code></p>
  
  <p>In your case, however you want to trigger on swap used, so you would have to compute the difference between total and free.</p>
  
Answers: CC BY-SA 4.0
Answers: 2019-10-28T12:58:25.983Z
Answers: 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
Answers: Peutre
Answers: CC BY-SA 4.0
Answers: 2019-10-22T07:16:28.367Z
Answers: 2019-10-22T07:16:28.367Z
Answers: Bruce Becker
Answers: <p>I have a solution on that way :</p>
  
  <pre><code>---
  - hosts: hostnametest
    tasks:
     - 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) &gt; 100
  
     - name: Turn on swap
       shell: "swapon -a"
       when: (swap_used.stdout_lines[0] | int) &gt; 100
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2019-10-31T09:30:19.483Z
Answers: 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.
Answers: M_dk
Answers: CC BY-SA 4.0
Answers: 2019-10-31T10:27:11.730Z
Answers: 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" :)
Answers: Peutre
Answers: CC BY-SA 4.0
Answers: 2019-10-31T09:04:17.130Z
Answers: 2019-10-31T09:04:17.130Z
Answers: Peutre
Body: <p>I am newbie to Ansible, i failed using ansible via command that way : </p>
  
  <pre><code>ansible host -a "/sbin/swapoff -a &amp;&amp; /sbin/swapon -a"
  </code></pre>
  
  <p>Only this worked </p>
  
  <pre><code>ansible host -a "/sbin/swapoff -a
  ansible host -a "/sbin/swapon -a
  </code></pre>
  
  <p>I am thinking of making a playbook, starting maybe with this:</p>
  
  <pre><code>- name: Turn off swap
    shell : "swapoff -a"
  
  - name: Turn on swap
    shell : "swapon -a"
  </code></pre>
  
  <p>I would like to <strong>reset swap space</strong> with a condition, only when the swap used space is <strong>greater</strong> than <strong>100mb</strong>. Is it possible to do that ?</p>
  
  <p>Many thanks for your help</p>
  
Comments: CC BY-SA 4.0
Comments: 2019-10-31T09:32:48.813Z
Comments: While I agree that there are cases where you want to clear swap space, do read https://askubuntu.com/a/1379/561783 to make sure you are not spending too much time on a non-issue :)
Comments: M_dk
Comments: CC BY-SA 4.0
Comments: 2019-10-31T09:34:43.327Z
Comments: and https://unix.stackexchange.com/questions/220065/is-clearing-a-full-swap-necessary-wise
Comments: M_dk
Comments: CC BY-SA 4.0
Comments: 2019-10-31T13:01:19.493Z
Comments: Thanks for the links :)
Comments: Peutre
Tags: <ansible><linux>
Title: Ansible resetting swap linux
UserDisplayName: Peutre
ThreadText: # Ansible resetting swap linux
  
  (Asked by: username_0 on 2019-10-22)
  
  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
  
  **Tags:** ansible, linux
  
  > **Comments:**
  > **username_1**: While I agree that there are cases where you want to clear swap space, do read https://askubuntu.com/a/1379/561783 to make sure you are not spending too much time on a non-issue :)  
  > **username_1**: and https://unix.stackexchange.com/questions/220065/is-clearing-a-full-swap-necessary-wise  
  > **username_0**: Thanks for the links :)  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_2 on 2019-10-22. Score: 2 | Accepted answer
  
  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](https://docs.ansible.com/ansible/latest/reference_appendices/special_variables.html#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 => {
      "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.
  
  > **Comments:**
  > **username_0**: 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  
  
  ---
  
  ### Answer by username_0 on 2019-10-31. Score: 1
  
  I have a solution on that way :
  
  ```
  ---
  - hosts: hostnametest
    tasks:
     - 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
  
  ```
  
  > **Comments:**
  > **username_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.  
  > **username_0**: 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" :)  
  
  ---
  
  