# or condition in ansible skips the hosts that are not matched in condition

**URL:** <https://forum.ansible.com/t/or-condition-in-ansible-skips-the-hosts-that-are-not-matched-in-condition/26077>\
**Category:** Ansible Project\
**Created:** [November 20, 2017, 6:15am UTC](https://forum.ansible.com/t/or-condition-in-ansible-skips-the-hosts-that-are-not-matched-in-condition/26077 "2017-11-20T06:15:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![shashank\_pai](https://avatars.discourse-cdn.com/v4/letter/s/e19adc/32.png) [@shashank\_pai](https://forum.ansible.com/u/shashank_pai)\
**Post date:** [November 20, 2017, 6:15am UTC](https://forum.ansible.com/t/or-condition-in-ansible-skips-the-hosts-that-are-not-matched-in-condition/26077/1 "2017-11-20T06:15:41Z")

</div>

> down vot[favorite](https://stackoverflow.com/questions/47330287/or-condition-in-ansible-skips-the-hosts-that-are-not-matched-in-condition/47330708#)  
>   
>   
> |   
>   
>   
>   
> I am trying to check memory consumption of four machines and any of the machines memory consumption is less than 10 GB I want to restart services on all machines.  
>   
>   
>   
> I have used or operator in when condition, I am able to restart the services only on the ones that have memory less than 10 GB others get skipped. My requirement is if any of the hosts memory goes down below 10GB I want services to be restarted on all machines not just on machines that have memory below 10GB.  
>   
>   
>   
> Below is the Ansible playbook:  
>   
>   
>   
>   
>   
>   
>   
>   
> `<br>- hosts: webapp<br>become: true<br>become_method: sudo<br>gather_facts: yes<br>vars:<br>servers:<br>"10.0.0.1": "machine1"<br>"10.0.0.2": "machine2"<br>"10.0.0.3": "machine3"<br>"10.0.0.4": "machine4"<br>tasks:<br>- name: get mem free for machine1<br>shell: free -g | grep Mem | awk '{print $4}'<br>register: machine1_mem<br>- debug: var=machine1_mem.stdout<br>when: servers[ansible_default_ipv4.address] == "machine1"<br>- name: get mem free for machine2<br>shell: free -g | grep Mem | awk '{print $4}'<br>register: machine2_mem<br>- debug: var=machine2_mem.stdout<br>when: servers[ansible_default_ipv4.address] == "machine2"<br>- name: get mem free for machine3<br>shell: free -g | grep Mem | awk '{print $4}'<br>register: machine3_mem<br>- debug: var=machine3_mem.stdout<br>when: servers[ansible_default_ipv4.address] == "machine3"<br>- name: get mem free for machine4<br>shell: free -g | grep Mem | awk '{print $4}'<br>register: machine4_mem<br>- debug: var=machine4_mem.stdout<br>when: servers[ansible_default_ipv4.address] == "machine4"<br>- name: restart web services<br>when: ( machine4_mem.stdout|int <= 10 or machine1_mem.stdout|int <= 10 or machine3_mem.stdout|int <= 10 or machine2_mem.stdout|int <= 10 )<br> action: service name=httpd state=restarted<br>`  
>   
>   
>   
> |

---

<div class="post-metadata">

**Author:** ![Pshem\_Kowalczyk](https://avatars.discourse-cdn.com/v4/letter/p/e480ec/32.png) [@Pshem\_Kowalczyk](https://forum.ansible.com/u/Pshem_Kowalczyk)\
**Post date:** [November 20, 2017, 7:52am UTC](https://forum.ansible.com/t/or-condition-in-ansible-skips-the-hosts-that-are-not-matched-in-condition/26077/2 "2017-11-20T07:52:16Z")

</div>

Ansible operates on an internal loop. Tasks are executed (and facts set) on ‘per-device’ basis. If you need the results of all the checks I suggest you do the following:

1. Register the results and store them into files on the controller (either can run as internal ansible loop or manual set of tasks)
2. Add another play in your playbook that’s executed on the localhost - check the content of each of the files and identify if you have to restart or not. You can store this as a fact against ‘localhost’ so it’s accessible via hostvars[‘localhost’][‘var\_name’]
3. Add yet another play that conditionally restarts all the servers if the content of the variable indicates the need for a restart

the whole thing can be also done without storing info in the files - you can loop over all hosts in a group so you can access the results of the check.

kind regards  
Pshem

---

<div class="post-metadata">

**Author:** ![shashank\_pai](https://avatars.discourse-cdn.com/v4/letter/s/e19adc/32.png) [@shashank\_pai](https://forum.ansible.com/u/shashank_pai)\
**Post date:** [November 21, 2017, 12:24pm UTC](https://forum.ansible.com/t/or-condition-in-ansible-skips-the-hosts-that-are-not-matched-in-condition/26077/3 "2017-11-21T12:24:51Z")

</div>

Thanks Pshem for your solution, Is there anything i can use as ANY condition like if any of the servers return value less than 10 do a service restart on all servers. Below is my play book which i tried.

— # first playbook

- hosts: apacheweb  
remote\_user: ansible  
become: true  
become\_method: sudo  
gather\_facts: yes  
tasks:
- name: Register free memory  
shell: free -g | grep Mem | awk ‘{print $4}’  
register: memory
- set\_fact: mem={{ memory.stdout }}
- set\_fact: Mem\_check=False  
when: mem|int \> 10
- name: Restart web service  
service:  
name: httpd  
state: restarted  
when: not Mem\_check

Here i am trying to use set\_fact with boolean values if it changes to True from false do a restart on all machines . But still it is only restarting the services on machines that are effected.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [November 22, 2017, 5:04pm UTC](https://forum.ansible.com/t/or-condition-in-ansible-skips-the-hosts-that-are-not-matched-in-condition/26077/4 "2017-11-22T17:04:50Z")

</div>

This is a little ugly but should work

- name: Restart web service  
&nbsp;&nbsp;service:  
&nbsp;&nbsp;&nbsp;&nbsp;name: httpd  
&nbsp;&nbsp;&nbsp;&nbsp;state: restarted  
&nbsp;&nbsp;when: {{ group['apacheweb'] | map('extract', hostvars) | list | selectattr('Mem\_check') | list | length \< group['apacheweb'] | length }}

One possible caveat, if one or more host doesn't respond the service on the host that work will restarted.
