# loop on register

**URL:** <https://forum.ansible.com/t/loop-on-register/34652>\
**Category:** Ansible Project\
**Created:** [February 19, 2021, 9:30am UTC](https://forum.ansible.com/t/loop-on-register/34652 "2021-02-19T09:30:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael\_Rossler](https://avatars.discourse-cdn.com/v4/letter/m/f19dbf/32.png) [@Michael\_Rossler](https://forum.ansible.com/u/Michael_Rossler)\
**Post date:** [February 19, 2021, 9:30am UTC](https://forum.ansible.com/t/loop-on-register/34652/1 "2021-02-19T09:30:54Z")

</div>

Dear ansible community,

System: CentOS 7, ansible version 2.9.17

I like to check if two files are already deployed on a node and if not it should copy the files.

For this I have the folling tasks/main.yml

_# tasks file for cve_  
_- name: Register loop output as a variable_  
_stat:_  
_path: “{{ item }}”_  
_loop:_  
_- “/tmp/test.1”_  
_- “/tmp/test.2”_  
_register: files_

_- name: Copy if file does not exist_  
_copy:_  
_src: test.2_  
_dest: “/tmp/test.2”_  
_when: item.stat.exists == false_  
_loop: “{{ files.results }}”_

And I have following files in files/:

_test.1 test.2_

I like to replace the following…

_src: test.2_  
_dest: “/tmp/test.2”_

…through

_src: “{{item.src}}”_  
_dest: “{{item.dest}}”_

…but I don’t know how to edit…

_loop: “{{ files.results }}”_

Is there a way to solve that? If yes, please provide the solution.

Kind regards

Michael Roessler

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [February 19, 2021, 10:06am UTC](https://forum.ansible.com/t/loop-on-register/34652/2 "2021-02-19T10:06:08Z")

</div>

Maybe i'm missing something but the whole point of the copy module is  
to make sure files are available on the target.  
No need to stat/register/etc. This should be enough:

&nbsp;&nbsp;- name: Ensure file is copied  
&nbsp;&nbsp;&nbsp;&nbsp;copy:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;src: "{{ item }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest: "/tmp/{{ item }}"  
&nbsp;&nbsp;&nbsp;&nbsp;loop:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- test.1  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- test.2

I'd like to have task names that are condense and to the point, so I'm  
a big fan of the simple present "ensure".

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [February 19, 2021, 10:07am UTC](https://forum.ansible.com/t/loop-on-register/34652/3 "2021-02-19T10:07:32Z")

</div>

Actually it should read "Ensure file is available" - as the copying  
only happens when the file is NOT available 😛

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [February 19, 2021, 10:33am UTC](https://forum.ansible.com/t/loop-on-register/34652/4 "2021-02-19T10:33:16Z")

</div>

The copy happens also if source file changes.

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [February 19, 2021, 2:20pm UTC](https://forum.ansible.com/t/loop-on-register/34652/5 "2021-02-19T14:20:48Z")

</div>

> Dear ansible community,
> 
> System: CentOS 7, ansible version 2.9.17
> 
> I like to check if two files are already deployed on a node and if not it should copy the files.
> 
> For this I have the folling tasks/main.yml
> 
> /# tasks file for cve/  
> /- name: Register loop output as a variable/  
> / stat:/  
> / path: "{{ item }}"/  
> / loop:/  
> / - "/tmp/test.1"/  
> / - "/tmp/test.2"/  
> / register: files/  
> /  
> /  
> /- name: Copy if file does not exist/  
> / copy:/  
> / src: test.2/  
> / dest: "/tmp/test.2"/  
> / when: item.stat.exists == false/  
> / loop: "{{ files.results }}"/
> 
> And I have following files in files/:
> 
> /test.1 test.2/
> 
> I like to replace the following...
> 
> / src: test.2/  
> / dest: "/tmp/test.2"/  
> ...through
> 
> / src: "{{item.src}}"/  
> / dest: "{{item.dest}}"/  
> ...but I don't know how to edit...
> 
> / loop: "{{ files.results }}"/
> 
> Is there a way to solve that? If yes, please provide the solution.

Hello Michael,

please try:

&nbsp;&nbsp;- name: Copy if file does not exist  
&nbsp;&nbsp;&nbsp;&nbsp;copy:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;src: "{{ item.item | basename }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest: "{{ item.item }}"  
&nbsp;&nbsp;&nbsp;&nbsp;when: item.stat.exists == false  
&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ files.results }}"

Hints:

- use debug: task to understand of the structure of {{ files.results }}  
- basename: takes a path to a file and the last segment of that path (/tmp/test.1 =\> test.1)

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke
