# with\_items and register in a role/main.yml

**URL:** <https://forum.ansible.com/t/with-items-and-register-in-a-role-main-yml/23133>\
**Category:** Ansible Project\
**Created:** [November 30, 2016, 2:29am UTC](https://forum.ansible.com/t/with-items-and-register-in-a-role-main-yml/23133 "2016-11-30T02:29:24Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![stewart.paul](https://avatars.discourse-cdn.com/v4/letter/s/dfb087/32.png) [@stewart.paul](https://forum.ansible.com/u/stewart.paul)\
**Post date:** [November 30, 2016, 2:29am UTC](https://forum.ansible.com/t/with-items-and-register-in-a-role-main-yml/23133/1 "2016-11-30T02:29:24Z")

</div>

Hi,

I’ve created a small demo below which demonstrates my problem.  
I can’t seem to invoke the with\_items and call {{item}} later on. What am I doing wrong? I’ve tried searching obviously but I can’t find the direct answer, sorry for the newbie question.

My playbook invokes a role, and below is my role:

- name: Mytest to register a variable

find: paths=“/tmp/demo/” patterns=“\*.txt”  
register: r

- name: mytest to show my results  
debug: msg=“{{item}}”  
with\_items: r

The above doesn’t give the desired result. When i change my task mytest into

- name: mytest  
debug: msg={{r.files.0.path}}

then i do get the first .txt file found, I can’t seem to make it dynamically.

Ultimately I’m trying to create a loop true a directory, and based on the file extension I want to invoke a certain action. For example, if the file extension is .sh, then invoke shell. If it’s .sql, then invoke sqlplus, and more extensions. If the extension is not listed then copy the file. Perhaps my whole approach is wrong in the first place, I’m trying to take it step by step of course, which might lead me in a wrong direction?

Again, thanks so much.

---

<div class="post-metadata">

**Author:** ![frank1](https://avatars.discourse-cdn.com/v4/letter/f/a87d85/32.png) [@frank1](https://forum.ansible.com/u/frank1)\
**Post date:** [December 1, 2016, 4:59pm UTC](https://forum.ansible.com/t/with-items-and-register-in-a-role-main-yml/23133/2 "2016-12-01T16:59:10Z")

</div>

r is avariable after registering.  
So i think you sgould use "{{ r }}"

---

<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:** [December 1, 2016, 7:21pm UTC](https://forum.ansible.com/t/with-items-and-register-in-a-role-main-yml/23133/3 "2016-12-01T19:21:31Z")

</div>

> I've created a small demo below which demonstrates my problem.  
> I can't seem to invoke the with\_items and call {{item}} later on. What am I  
> doing wrong? I've tried searching obviously but I can't find the direct  
> answer, sorry for the newbie question.
> 
> My playbook invokes a role, and below is my role:  
> &nbsp;&nbsp;- name: Mytest to register a variable  
> &nbsp;&nbsp;&nbsp;&nbsp;find: paths="/tmp/demo/" patterns="\*.txt"  
> &nbsp;&nbsp;&nbsp;&nbsp;register: r
> 
> &nbsp;&nbsp;- name: mytest to show my results  
> &nbsp;&nbsp;&nbsp;&nbsp;debug: msg="{{item}}"  
> &nbsp;&nbsp;&nbsp;&nbsp;with\_items: r
> 
> The above doesn't give the desired result. When i change my task mytest  
> into  
> &nbsp;&nbsp;- name: mytest  
> &nbsp;&nbsp;&nbsp;&nbsp;debug: msg={{r.files.0.path}}  
> then i do get the first .txt file found, I can't seem to make it  
> dynamically.

- name: mytest to show my results  
&nbsp;&nbsp;&nbsp;debug: msg="{{ item.path }}"  
&nbsp;&nbsp;&nbsp;with\_items: r.files

> Ultimately I'm trying to create a loop true a directory, and based on the  
> file extension I want to invoke a certain action. For example, if the file  
> extension is .sh, then invoke shell. If it's .sql, then invoke sqlplus, and  
> more extensions. If the extension is not listed then copy the file. Perhaps  
> my whole approach is wrong in the first place, I'm trying to take it step  
> by step of course, which might lead me in a wrong direction?

It sounds like a wrong approach, but that doesn't mean that is wrong.  
But there may be better ways reach your objective.

You could try explaining what you are trying to achieve and maybe someone on the list could help you toward a more correct direction.

---

<div class="post-metadata">

**Author:** ![stewart.paul](https://avatars.discourse-cdn.com/v4/letter/s/dfb087/32.png) [@stewart.paul](https://forum.ansible.com/u/stewart.paul)\
**Post date:** [December 6, 2016, 8:17pm UTC](https://forum.ansible.com/t/with-items-and-register-in-a-role-main-yml/23133/4 "2016-12-06T20:17:14Z")

</div>

Hi,

Thanks for your reply, what I’m trying to do is a simple copy and paste (deploy a release).  
Specifically, I want to list thru the directory and copy all files that are not in the ./ins directory.

problem one: I couldn’t figure that out with the copy command, so i used "command: "find ./demo\_deploy -type f ! -path ‘./demo\_deploy/ins/\*’ "

problem two: i cant seem to figure out how to execute the above in a with\_items, so i thought to place the result in an register. As followed:

- name: Register - Copy files

command: “find ./demo\_deploy -type f ! -path ‘./demo\_deploy/ins/_’ ! -path './demo\_deploy/roles/_’ ! -path ‘./demo\_deploy/host\_vars/\*’”  
register: copy\_files

- name: Copy files

command: cp {{ item }} /tmp/bb/  
with\_items:

- “{{copy\_files.stdout\_lines}}”  
tags:
- copy\_release

This works fine… however, if I place them in a role, then my register isnt loaded when i run my plabook with --copy\_release tag.

I guess I’m getting lost here, where the original challenge is simple: copy all files except 1 directory.

Any help is appreciated!

---

<div class="post-metadata">

**Author:** ![Stankovic\_Marko](https://avatars.discourse-cdn.com/v4/letter/s/e495f1/32.png) [@Stankovic\_Marko](https://forum.ansible.com/u/Stankovic_Marko)\
**Post date:** [December 7, 2016, 4:02pm UTC](https://forum.ansible.com/t/with-items-and-register-in-a-role-main-yml/23133/5 "2016-12-07T16:02:14Z")

</div>

Hi Stewart,

First: including only "copy\_release" tag makes ansible skip the first task, as it's tagged only with "copy\_files".

Second: you can also use with\_filetree\<[https://github.com/ansible/ansible/blob/devel/lib/ansible/plugins/lookup/filetree.py&gt](https://github.com/ansible/ansible/blob/devel/lib/ansible/plugins/lookup/filetree.py&gt); lookup (introduced in Ansible 2.2) or with\_fileglob\<[http://docs.ansible.com/ansible/playbooks\_loops.html#id4&gt](http://docs.ansible.com/ansible/playbooks_loops.html#id4&gt); (if you don't need to iterate a directory recursively).  
Then use separate tasks for different actions/extensions with 'when' directive.  
This way you'll skip the register task.  
Note that these lookups return different data structures for {{ item }}.  
fileglob's item is a string containing the file/directory absolute path, while filetree's item is a dict.  
Some "documentation" can be found in original pull request\<[https://github.com/ansible/ansible/pull/14332&gt;&#39;s](https://github.com/ansible/ansible/pull/14332&gt;&#39;s) description.

When executed as "with\_filetree: /tmp", it looks like this for a file (or a link with "state":"link"):  
&nbsp;&nbsp;&nbsp;&nbsp;"item": {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"ctime": 1479723235.151,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"gid": 501,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"group": "xxx",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"mode": "0664",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"mtime": 1479723235.151,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"owner": "xxx",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"path": "ansible-debug.log",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"root": "/tmp",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"selevel": "s0",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"serole": "object\_r",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"setype": "user\_tmp\_t",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"seuser": "unconfined\_u",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"size": 4104,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"src": "/tmp/ansible-debug.log",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"state": "file",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"uid": 501  
&nbsp;&nbsp;&nbsp;&nbsp;}  
And like this for a directory (there's no "src"):  
&nbsp;&nbsp;&nbsp;&nbsp;"item": {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"ctime": 1479810261.0929983,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"gid": 501,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"group": "xxx",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"mode": "0775",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"mtime": 1479810261.0929983,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"owner": "xxx",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"path": "test",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"root": "/tmp",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"selevel": "s0",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"serole": "object\_r",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"setype": "user\_tmp\_t",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"seuser": "unconfined\_u",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"size": 4096,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"state": "directory",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"uid": 501  
&nbsp;&nbsp;&nbsp;&nbsp;}

This example copies files only:

&nbsp;&nbsp;- name: Copy files  
&nbsp;&nbsp;&nbsp;&nbsp;command: cp {{ item.src }} /tmp/bb/{{ item.path }}  
&nbsp;&nbsp;&nbsp;&nbsp;with\_filetree: ./demo\_deploy  
&nbsp;&nbsp;&nbsp;&nbsp;when: item.state == 'file' and item.path.split('/')[0] not in ['ins', 'roles', 'host\_vars']  
&nbsp;&nbsp;&nbsp;&nbsp;tags:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- copy\_release  
if you need to preserve the same directory structure, then add this before the task above:  
&nbsp;&nbsp;- name: Create directories  
&nbsp;&nbsp;&nbsp;&nbsp;file:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest: /tmp/bb/{{ item.path }}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state: directory  
&nbsp;&nbsp;&nbsp;&nbsp;with\_filetree: ./demo\_deploy  
&nbsp;&nbsp;&nbsp;&nbsp;when: item.state == 'directory' and item.path.split('/')[0] not in ['ins', 'roles', 'host\_vars']  
&nbsp;&nbsp;&nbsp;&nbsp;tags:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- copy\_release

Cheers,  
Marko
