# Set fact on all hosts if a directory is present on one of the hosts.

**URL:** <https://forum.ansible.com/t/set-fact-on-all-hosts-if-a-directory-is-present-on-one-of-the-hosts/22672>\
**Category:** Ansible Project\
**Created:** [September 29, 2016, 6:50am UTC](https://forum.ansible.com/t/set-fact-on-all-hosts-if-a-directory-is-present-on-one-of-the-hosts/22672 "2016-09-29T06:50:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rafal\_Radecki](https://avatars.discourse-cdn.com/v4/letter/r/bcef8e/32.png) [@Rafal\_Radecki](https://forum.ansible.com/u/Rafal_Radecki)\
**Post date:** [September 29, 2016, 6:50am UTC](https://forum.ansible.com/t/set-fact-on-all-hosts-if-a-directory-is-present-on-one-of-the-hosts/22672/1 "2016-09-29T06:50:27Z")

</div>

Hi All 🙂

I am currently trying to find a way to set a variable/fact on all hosts in case a directory for a service will already exist on one of the play’s hosts. Basically:

# check if dir exists on all hosts and set service\_home\_dir\_status on every host

- name: check if service home dir already exists  
stat:  
path: “{{ service\_home\_dir }}”  
register: service\_home\_dir\_status

# fail if the above dir already exists

- name: fail if service home dir already exists  
fail:  
msg: “Home directory for service {{ service\_home\_dir }} already exists. Please check the situation!”  
when: service\_home\_dir\_status.stat.isdir is defined and service\_home\_dir\_status.stat.isdir

The problem with above code is that if for example I have a group of hosts: host1, host2, host3 and on host1 the directory already exists the second step will only fail on host1 and succeed on host2 and host3.  
Is there a way to maybe set a fact for all hosts in between these tasks if the directory already exists on one of them?

Thanks!

BR,  
Rafal.

---

<div class="post-metadata">

**Author:** ![Dick\_Davies](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@Dick\_Davies](https://forum.ansible.com/u/Dick_Davies)\
**Post date:** [September 29, 2016, 7:44am UTC](https://forum.ansible.com/t/set-fact-on-all-hosts-if-a-directory-is-present-on-one-of-the-hosts/22672/2 "2016-09-29T07:44:15Z")

</div>

If you set a fact on a specific host you'll be able to look that up  
via hostvars from anywhere:

&nbsp;&nbsp;&nbsp;[http://docs.ansible.com/ansible/set\_fact\_module.html](http://docs.ansible.com/ansible/set_fact_module.html)

---

<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:** [September 29, 2016, 11:14am UTC](https://forum.ansible.com/t/set-fact-on-all-hosts-if-a-directory-is-present-on-one-of-the-hosts/22672/3 "2016-09-29T11:14:52Z")

</div>

To me it sound like you are trying to abort the play if one host fails,  
if so you could use "any\_errors\_fatal: true" in the playbook.

[https://docs.ansible.com/ansible/playbooks\_error\_handling.html#aborting-the-play](https://docs.ansible.com/ansible/playbooks_error_handling.html#aborting-the-play)
