# with\_items and when

**URL:** <https://forum.ansible.com/t/with-items-and-when/25818>\
**Category:** Ansible Project\
**Created:** [October 25, 2017, 12:17pm UTC](https://forum.ansible.com/t/with-items-and-when/25818 "2017-10-25T12:17:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sidh](https://avatars.discourse-cdn.com/v4/letter/s/bbce88/32.png) [@Sidh](https://forum.ansible.com/u/Sidh)\
**Post date:** [October 25, 2017, 12:17pm UTC](https://forum.ansible.com/t/with-items-and-when/25818/1 "2017-10-25T12:17:13Z")

</div>

Hi,

Basically would like to check if file is present on remote system or not. If not present, then create one, otherwise skip this step.

- name: Checking if files already present  
stat:  
path: “{{ item }}”  
register: file\_path  
with\_items:

- /var/abc.key

- /var/xyz.key

- /var/spk.key

- name: Creating files  
file:  
path: “{{ item }}”  
state: touch  
owner: root  
group: named  
mode: 0644  
with\_items:

- /var/abc.key

- /var/xyz.key

- /var/spk.key  
when: file\_path.stat.exists == False

Any ideas ?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [October 25, 2017, 1:33pm UTC](https://forum.ansible.com/t/with-items-and-when/25818/2 "2017-10-25T13:33:03Z")

</div>

- copy: content=' ' dest={{item}} force=no  
&nbsp;&nbsp;with\_items:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- /var/abc.key  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...

---

<div class="post-metadata">

**Author:** ![Sidh](https://avatars.discourse-cdn.com/v4/letter/s/bbce88/32.png) [@Sidh](https://forum.ansible.com/u/Sidh)\
**Post date:** [October 26, 2017, 10:21am UTC](https://forum.ansible.com/t/with-items-and-when/25818/3 "2017-10-26T10:21:46Z")

</div>

Thanks Brian…
