# Retrieving file names/sizes in dir

**URL:** <https://forum.ansible.com/t/retrieving-file-names-sizes-in-dir/27740>\
**Category:** Ansible Project\
**Created:** [June 7, 2018, 6:43pm UTC](https://forum.ansible.com/t/retrieving-file-names-sizes-in-dir/27740 "2018-06-07T18:43:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_Harmon](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@John\_Harmon](https://forum.ansible.com/u/John_Harmon)\
**Post date:** [June 7, 2018, 6:43pm UTC](https://forum.ansible.com/t/retrieving-file-names-sizes-in-dir/27740/1 "2018-06-07T18:43:50Z")

</div>

I wish to be able to get the name and filesize of all files under /var/spool/mail. I was trying to do this with the following:

`

- name: Stat mail dir  
stat:  
path: “{{ item }}”  
with\_fileglob:
- “/var/spool/mail/\*”  
register: mail\_dir

`

I do not understand how to reference the resulting variable’s output. How would I go about iterating through the various sub items and sizes?

Looking for output such as:

root: 4096  
gomer: 124587  
pyle: 0

I have been reviewing the docs (perhaps the wrong ones), and I don’t quite understand.

---

<div class="post-metadata">

**Author:** ![John\_Harmon](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@John\_Harmon](https://forum.ansible.com/u/John_Harmon)\
**Post date:** [June 7, 2018, 6:46pm UTC](https://forum.ansible.com/t/retrieving-file-names-sizes-in-dir/27740/2 "2018-06-07T18:46:43Z")

</div>

I know I can do this easily with shell, but I am trying to understand how to retrieve information from variables created with a loop.

---

<div class="post-metadata">

**Author:** ![David\_Villasmil](https://avatars.discourse-cdn.com/v4/letter/d/a5b964/32.png) [@David\_Villasmil](https://forum.ansible.com/u/David_Villasmil)\
**Post date:** [June 7, 2018, 7:15pm UTC](https://forum.ansible.com/t/retrieving-file-names-sizes-in-dir/27740/3 "2018-06-07T19:15:49Z")

</div>

Probably there are better ways, but I can do it like so:

- command: “ls {{directory}}”  
register: dir\_out

- debug: var={{item}}  
with\_items: dir\_out.stdout\_lines

- stat:  
path: “{{ directory }}/{{ item }}”  
with\_items: “{{ dir\_out.stdout\_lines }}”  
register: st

- name: porint  
debug: “msg={{ st }}”

- set\_fact:  
mystat: “{{ st.results }}”

- name: pp  
debug: “msg={{ item.stat.path }},{{ item.stat.size }}”  
with\_items: “{{ mystat }}”

you pass the directory like:

ansible-playbook -vv -i hosts pb.yaml -e “directory=/someting/”

![](https://mailfoogae.appspot.com/t?sender=aZGF2aWQudmlsbGFzbWlsLndvcmtAZ21haWwuY29t&type=zerocontent&guid=ed8570e2-2f8a-48a7-bfb1-240f70315020)ᐧ

---

<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:** [June 7, 2018, 7:38pm UTC](https://forum.ansible.com/t/retrieving-file-names-sizes-in-dir/27740/4 "2018-06-07T19:38:41Z")

</div>

> I wish to be able to get the name and filesize of all files under  
> /var/spool/mail. I was trying to do this with the following:  
> &nbsp;&nbsp;&nbsp;&nbsp;- name: Stat mail dir  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;stat:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path: "{{ item }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with\_fileglob:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- "/var/spool/mail/\*"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: mail\_dir

with\_\* only work on ansible controller, not on remote host.  
So you are probably looking for the find module.

> I do not understand how to reference the resulting variable's output. How  
> would I go about iterating through the various sub items and sizes?
> 
> Looking for output such as:
> 
> root: 4096  
> gomer: 124587  
> pyle: 0
> 
> I have been reviewing the docs (perhaps the wrong ones), and I don't quite  
> understand.

&nbsp;&nbsp;&nbsp;- find:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;paths: /var/spool/mail  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: r

&nbsp;&nbsp;&nbsp;- debug: msg='{{ item.path | basename }}: {{ item.size }}'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with\_items: '{{ r.files }}'

If you like the result in the file you could do this

&nbsp;&nbsp;&nbsp;- copy:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;content: |  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{% for i in r.files %}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{ i.path | basename }}: {{ i.size }}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{% endfor %}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest: /path/to/file

---

<div class="post-metadata">

**Author:** ![John\_Harmon](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@John\_Harmon](https://forum.ansible.com/u/John_Harmon)\
**Post date:** [June 7, 2018, 7:51pm UTC](https://forum.ansible.com/t/retrieving-file-names-sizes-in-dir/27740/5 "2018-06-07T19:51:56Z")

</div>

Thanks to both of you. Gave me great ideas. I implemented and tweaked Kai’s suggestion due to simplicity.
