On Tuesday, 21 November 2017 13.24.51 CET shashank pai wrote:
> 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. 


This is a little ugly but should work

- name: Restart web service
  service:
    name: httpd
    state: restarted
  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.


-- 
Kai Stian Olstad

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/5532255.mCaOx9v4XD%40x1.
For more options, visit https://groups.google.com/d/optout.

Reply via email to