# Problem using results of 'find' module...

**URL:** <https://forum.ansible.com/t/problem-using-results-of-find-module/22969>\
**Category:** Ansible Project\
**Created:** [November 6, 2016, 3:18am UTC](https://forum.ansible.com/t/problem-using-results-of-find-module/22969 "2016-11-06T03:18:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Will\_Dennis](https://avatars.discourse-cdn.com/v4/letter/w/919ad9/32.png) [@Will\_Dennis](https://forum.ansible.com/u/Will_Dennis)\
**Post date:** [November 6, 2016, 3:18am UTC](https://forum.ansible.com/t/problem-using-results-of-find-module/22969/1 "2016-11-06T03:18:07Z")

</div>

Hi all,

Trying to use the ‘find’ module to collect a list of files that I will then operate on. To test this out, I wrote a simple playbook as follows:

- name: test playbook  
#remote\_user: root  
hosts: localhost

tasks:

- name: find all vbox SysV init scripts in /etc/rc[0-6].d  
find:  
paths: “{{ item }}”  
patterns: “^.\*virtualbox$”  
use\_regex: True  
with\_items:

- /etc/rc0.d

- /etc/rc1.d

- /etc/rc2.d

- /etc/rc3.d

- /etc/rc4.d

- /etc/rc5.d

- /etc/rc6.d  
register: vbox\_sysv\_scripts

- debug: var=vbox\_sysv\_scripts verbosity=2

- name: Show me the files!  
command: ls -l {{ item.path }}  
with\_items: vbox\_sysv\_scripts.files

However, when I run the playbook, I get an error on the “Show me the files!” task, which uses the result of the find registered var:

bash-4.3$ ansible-playbook -i inventory/testing -k test-find.yml  
SSH password:

PLAY [test playbook] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

TASK [setup] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost]

TASK [find all vbox SysV init scripts in /etc/rc[0-6].d] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> (item=/etc/rc0.d)  
ok: [localhost] =\> (item=/etc/rc1.d)  
ok: [localhost] =\> (item=/etc/rc2.d)  
ok: [localhost] =\> (item=/etc/rc3.d)  
ok: [localhost] =\> (item=/etc/rc4.d)  
ok: [localhost] =\> (item=/etc/rc5.d)  
ok: [localhost] =\> (item=/etc/rc6.d)

TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
skipping: [localhost]

TASK [Show me the files!] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
fatal: [localhost]: FAILED! =\> {“failed”: true, “msg”: “the field ‘args’ has an invalid value, which appears to include a variable that is undefined. The error was: ‘unicode object’ has no attribute ‘path’\n\nThe error appears to have been in ‘/home/istgroup/Documents/ansible-stuff/test-find.yml’: line 24, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: Show me the files!\n ^ here\n”}

If I print the “vbox\_sysv\_scripts” var via the debug statement, I get the following:  
[https://gist.githubusercontent.com/wdennis/13197fe430a66da28b03de809474c294/raw/ae9d77ddc64bc3c3f55f46e0a7469bb5d1848de6/vbox\_sysv\_scripts](https://gist.githubusercontent.com/wdennis/13197fe430a66da28b03de809474c294/raw/ae9d77ddc64bc3c3f55f46e0a7469bb5d1848de6/vbox_sysv_scripts)

Can someone tell me where I’m going wrong here?

---

<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:** [November 6, 2016, 8:34am UTC](https://forum.ansible.com/t/problem-using-results-of-find-module/22969/2 "2016-11-06T08:34:32Z")

</div>

When using with\_ and register the result is always a list stored in the \<variable\>.results, for you that is vbox\_sysv\_scripts.results, as shown by you debug

&nbsp;&nbsp;&nbsp;ok: [localhost] =\> {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"vbox\_sysv\_scripts": {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"changed": false,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"msg": "All items completed",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"results": [  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"\_ansible\_item\_result": true,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"\_ansible\_no\_log": false,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"\_ansible\_parsed": true,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"changed": false,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"examined": 17,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"files": [

The find module add all the files it find per item to a list stored in  
vbox\_sysv\_scripts.results.0.files for the fist item  
vbox\_sysv\_scripts.results.1.files for the second item  
vbox\_sysv\_scripts.results.2.files for the third item and so on.

To get the paths you will have to use the with\_subelements: to get them in your "Show me the files!"  
[https://docs.ansible.com/ansible/playbooks\_loops.html#looping-over-subelements](https://docs.ansible.com/ansible/playbooks_loops.html#looping-over-subelements)

&nbsp;&nbsp;&nbsp;- name: Show me the files!  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;command: ls -l {{ item.1.path }}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with\_subelements:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- "{{ vbox\_sysv\_scripts.results }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- files

An alternative is to use a list in the module finds paths, then your "Show me the files!" will work

&nbsp;&nbsp;&nbsp;- name: find all vbox SysV init scripts in /etc/rc[0-6].d  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;find:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;paths: ['/etc/rc0.d', '/etc/rc1.d', '/etc/rc2.d',  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'/etc/rc3.d', '/etc/rc4.d', '/etc/rc5.d', '/etc/rc6.d']  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;patterns: "^.\*virtualbox$"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;use\_regex: True  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: vbox\_sysv\_scripts

---

<div class="post-metadata">

**Author:** ![Will\_Dennis](https://avatars.discourse-cdn.com/v4/letter/w/919ad9/32.png) [@Will\_Dennis](https://forum.ansible.com/u/Will_Dennis)\
**Post date:** [November 7, 2016, 1:41am UTC](https://forum.ansible.com/t/problem-using-results-of-find-module/22969/3 "2016-11-07T01:41:40Z")

</div>

Thanks, Kai, for your kind help. I’ve never had to use “with\_subelements” before, and so it didn’t come to mind… I did see that there was a “results” list in the vbox\_sysv\_scripts var debug output, and thought that I may have to specify vbox\_sysv\_scripts.results to get at the files, but didn’t know how Ansible operates on that sort of structure… Also did not know that you could pass the file mod’s paths parameter a list, that’s pretty cool. So in any case, I rewrote the playbook as follows, which works:

`

- name: test playbook  
#remote\_user: root  
hosts: localhost

tasks:

- name: find all vbox SysV init scripts in /etc/rc[0-6].d  
find:  
patterns: “^.\*virtualbox$”  
use\_regex: True  
paths:  
[ ‘/etc/rc0.d’,  
‘/etc/rc1.d’,  
‘/etc/rc2.d’,  
‘/etc/rc3.d’,  
‘/etc/rc4.d’,  
‘/etc/rc5.d’,  
‘/etc/rc6.d’ ]  
register: vbox\_sysv\_scripts

- debug: var=vbox\_sysv\_scripts verbosity=2

- name: Show me the files!  
command: ls -l {{ item.path }}  
with\_items: “{{ vbox\_sysv\_scripts.files }}”  
`
