# Conditional copy based on the existence of a directory

**URL:** <https://forum.ansible.com/t/conditional-copy-based-on-the-existence-of-a-directory/33801>\
**Category:** Ansible Project\
**Created:** [September 14, 2020, 7:35am UTC](https://forum.ansible.com/t/conditional-copy-based-on-the-existence-of-a-directory/33801 "2020-09-14T07:35:16Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Victor\_Sudakov](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/victor_sudakov/32/9830_2.png) [@Victor\_Sudakov](https://forum.ansible.com/u/Victor_Sudakov)\
**Post date:** [September 14, 2020, 7:35am UTC](https://forum.ansible.com/t/conditional-copy-based-on-the-existence-of-a-directory/33801/1 "2020-09-14T07:35:16Z")

</div>

Dear Colleagues,

I’d like to be able to copy/template a file to a directory on a remote host depending upon the existence of the directory there (from a list of possible variants). Below I publish what I currently use but I hope there is a more scalable and elegant method, can you advise one?

- name: check for new zabbix  
stat:  
path: /etc/zabbix/zabbix\_agentd.d/  
register: newpath

- name: check for old zabbix  
stat:  
path: /etc/zabbix/zabbix\_agentd.conf.d/  
register: oldpath

- name: copy consul userparameters for new zabbix  
when: newpath.stat.exists and newpath.stat.isdir  
template:  
src: files/consul-userparameter.conf.j2  
dest: /etc/zabbix/zabbix\_agentd.d/consul.conf

- name: copy consul userparameters for old zabbix  
when: oldpath.stat.exists and oldpath.stat.isdir  
template:  
src: files/consul-userparameter.conf.j2  
dest: /etc/zabbix/zabbix\_agentd.conf.d/consul.conf
