# Create a file, if not exist, inside a loop

**URL:** <https://forum.ansible.com/t/create-a-file-if-not-exist-inside-a-loop/17309>\
**Category:** Ansible Project\
**Created:** [January 19, 2015, 10:51am UTC](https://forum.ansible.com/t/create-a-file-if-not-exist-inside-a-loop/17309 "2015-01-19T10:51:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tristan\_Bessoussa](https://avatars.discourse-cdn.com/v4/letter/t/b782af/32.png) [@Tristan\_Bessoussa](https://forum.ansible.com/u/Tristan_Bessoussa)\
**Post date:** [January 19, 2015, 10:51am UTC](https://forum.ansible.com/t/create-a-file-if-not-exist-inside-a-loop/17309/1 "2015-01-19T10:51:03Z")

</div>

Hi,

I’m trying to achieve something basic (v1.8.2): create a sitemap.xml if not exist for prestahsop projects.

`

- name: Check if sitemap is present  
stat: path={{ ansible\_www\_home }}/{{ item.dest }}/sitemap.xml  
with\_items: repositories  
when: item.type == “prestashop”  
register: sitemap

- debug: var=sitemap

- name: Create sitemap.xml if necessary  
file: \>  
path={{ ansible\_www\_home }}/{{ item.dest }}/sitemap.xml  
owner=www-data  
group=www-data  
mode=0644  
with\_items: repositories  
when: item.type == “prestashop” and sitemap.results.item is defined and sitemap.results.stat.exists == False

`

The debug statement gives me

`

“sitemap”: {  
“changed”: false,  
“msg”: “All items completed”,  
“results”: [  
{  
“changed”: false,  
“skipped”: true  
},  
{  
“changed”: false,  
“skipped”: true  
},  
{  
“changed”: false,  
“invocation”: {  
“module\_args”: “path=/home/yprox/ansible-www/project/sitemap.xml”,  
“module\_name”: “stat”  
},  
“item”: {  
“dest”: “top-kids”,  
“repo”: “[git@github.com](mailto:git@github.com):Organisation/project.git”,  
“type”: “prestashop”,  
“version”: “master”  
},  
“stat”: {  
“exists”: false,  
}

},  
…

`

I have at least one node which has “exists” false. But it does not create the file. It just goes “Skipped”.

Why ? Thanks 🙂

---

<div class="post-metadata">

**Author:** ![junkmailtrapenator](https://avatars.discourse-cdn.com/v4/letter/j/c77e96/32.png) [@junkmailtrapenator](https://forum.ansible.com/u/junkmailtrapenator)\
**Post date:** [January 19, 2015, 12:52pm UTC](https://forum.ansible.com/t/create-a-file-if-not-exist-inside-a-loop/17309/2 "2015-01-19T12:52:30Z")

</div>

Remember` **s** ` **itemap** is a list. So you need to [loop over that](http://docs.ansible.com/playbooks_loops.html#using-register-with-a-loop).

You should be able to do something like:

```auto
`
- name: Create sitemap.xml if necessary
file: >
path={{ ansible_www_home }}/{{ item.dest }}/sitemap.xml
owner=www-data
group=www-data
mode=0644
with_items: sitemap.results
when: item.item.type is defined and item.item.type == "prestashop" and item.stat.exists == False
`

`
```

---

<div class="post-metadata">

**Author:** ![Tristan\_Bessoussa](https://avatars.discourse-cdn.com/v4/letter/t/b782af/32.png) [@Tristan\_Bessoussa](https://forum.ansible.com/u/Tristan_Bessoussa)\
**Post date:** [January 19, 2015, 1:25pm UTC](https://forum.ansible.com/t/create-a-file-if-not-exist-inside-a-loop/17309/3 "2015-01-19T13:25:20Z")

</div>

Oh great, but _sitemap.results_ doesn’t know the _item.dest_ which was contained in the first array (_repositories)._

Is there a way to cross arrays in order to avoid loosing the reference to item.dest (because I want to know where to create the sitemap, and right now with your solution, it doesn’t know)

---

<div class="post-metadata">

**Author:** ![Tristan\_Bessoussa](https://avatars.discourse-cdn.com/v4/letter/t/b782af/32.png) [@Tristan\_Bessoussa](https://forum.ansible.com/u/Tristan_Bessoussa)\
**Post date:** [January 19, 2015, 1:33pm UTC](https://forum.ansible.com/t/create-a-file-if-not-exist-inside-a-loop/17309/4 "2015-01-19T13:33:43Z")

</div>

Ok nvm, there were a small error, but it works like a charm with :

`

- name: Create sitemap.xml if necessary  
file: \>  
path={{ ansible\_www\_home }}/{{ item.item.dest }}/sitemap.xml  
owner=www-data  
group=www-data  
mode=0644  
file=touch

`

Thanks very much sir 😉
