# Find register directory seems to not registered

**URL:** <https://forum.ansible.com/t/find-register-directory-seems-to-not-registered/23013>\
**Category:** Ansible Project\
**Created:** [November 12, 2016, 1:25am UTC](https://forum.ansible.com/t/find-register-directory-seems-to-not-registered/23013 "2016-11-12T01:25:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Felix\_Gao](https://avatars.discourse-cdn.com/v4/letter/f/258eb7/32.png) [@Felix\_Gao](https://forum.ansible.com/u/Felix_Gao)\
**Post date:** [November 12, 2016, 1:25am UTC](https://forum.ansible.com/t/find-register-directory-seems-to-not-registered/23013/1 "2016-11-12T01:25:03Z")

</div>

- hosts: test  
remote\_user: ec2-user  
sudo: yes  
tasks:  
#using the shell command because we need \* expansion, otherwise if we know the exact directory we can use command module instead

- name: list log directory to find main directory name  
find:  
paths: [“/var/log/”]  
patterns: “file-main\*”  
file\_type: directory  
register: out\_directories

- debug: var=“{{ item }}”  
with\_items: “{{ out\_directories }}”

The output is

ansible-playbook -vv -i hosts -u ec2-user directory\_cleanup.yml  
Using /Users/ggao/Projects/ansible-playbooks/test/ansible.cfg as config file  
[DEPRECATION WARNING]: Instead of sudo/sudo\_user, use become/become\_user and make sure become\_method is ‘sudo’ (default).  
This feature will be removed  
in a future release. Deprecation warnings can be disabled by setting deprecation\_warnings=False in ansible.cfg.

PLAYBOOK: directory\_cleanup.yml \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
1 plays in directory\_cleanup.yml

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

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

TASK [list log directory to find wasabi main directory name] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
task path: /Users/ggao/Projects/ansible-playbooks/test/directory\_cleanup.yml:8  
ok: [10.81.108.140] =\> {“changed”: false, “examined”: 30, “files”: [{“atime”: 1478891483.1474261, “ctime”: 1478891479.107535, “dev”: 51713, “gid”: 50002, “inode”: 264540, “isblk”: false, “ischr”: false, “isdir”: true, “isfifo”: false, “isgid”: false, “islnk”: false, “isreg”: false, “issock”: false, “isuid”: false, “mode”: “0755”, “mtime”: 1478891479.107535, “nlink”: 2, “path”: “/var/log/file-main-20161108022522”, “rgrp”: true, “roth”: true, “rusr”: true, “size”: 4096, “uid”: 50002, “wgrp”: false, “woth”: false, “wusr”: true, “xgrp”: true, “xoth”: true, “xusr”: true}], “matched”: 1, “msg”: “”}

TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
task path: /Users/ggao/Projects/ansible-playbooks/test/directory\_cleanup.yml:16  
ok: [10.81.108.140] =\> (item=files) =\> {  
“files”: “VARIABLE IS NOT DEFINED!”,  
“item”: “files”  
}  
ok: [10.81.108.140] =\> (item=msg) =\> {  
“item”: “msg”,  
“msg”: “VARIABLE IS NOT DEFINED!”  
}  
changed: [10.81.108.140] =\> (item=changed) =\> {  
“changed”: “VARIABLE IS NOT DEFINED!”,  
“item”: “changed”  
}  
ok: [10.81.108.140] =\> (item=examined) =\> {  
“examined”: “VARIABLE IS NOT DEFINED!”,  
“item”: “examined”  
}  
ok: [10.81.108.140] =\> (item=matched) =\> {  
“item”: “matched”,  
“matched”: “VARIABLE IS NOT DEFINED!”  
}

PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
10.81.108.140 : ok=3 changed=1 unreachable=0 failed=0

using python 2.7 on ansible-playbook 2.2.0.0

Thanks

---

<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 12, 2016, 9:09pm UTC](https://forum.ansible.com/t/find-register-directory-seems-to-not-registered/23013/2 "2016-11-12T21:09:01Z")

</div>

> - hosts: test  
> &nbsp;&nbsp;remote\_user: ec2-user  
> &nbsp;&nbsp;sudo: yes  
> &nbsp;&nbsp;tasks:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#using the shell command because we need \* expansion, otherwise if we  
> know the exact directory we can use command module instead  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- name: list log directory to find main directory name  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;find:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;paths: ["/var/log/"]  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;patterns: "file-main\*"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;file\_type: directory  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: out\_directories

When you use register with find the result is a list in files.  
You can check the content of out\_directories with  
- debug: var=out\_directories

> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- debug: var="{{ item }}"

Var takes the variable and not the content of the variable, correct use is var=item.

> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with\_items: "{{ out\_directories }}"

Register with find, the result is list of dictionaries in out\_directories.files, so you'll need to write it like this

- debug: var=item.path  
&nbsp;&nbsp;&nbsp;with\_items: "{{ out\_directories.files }}"

---

<div class="post-metadata">

**Author:** ![Felix\_Gao](https://avatars.discourse-cdn.com/v4/letter/f/258eb7/32.png) [@Felix\_Gao](https://forum.ansible.com/u/Felix_Gao)\
**Post date:** [November 14, 2016, 11:15pm UTC](https://forum.ansible.com/t/find-register-directory-seems-to-not-registered/23013/3 "2016-11-14T23:15:26Z")

</div>

Kai,

thanks for your explanation and I have made some progress but I am further stopped by the next stage.  
My problem is that I have a dynamic directory that generate log file in certain directories with the following format  
/var/log/app-YYYYMMDD-RANDOM\_HASH/log\_file\_name-YYYYMMDDMMSS-[audit,console,access,].log and I am trying to find all the logs in that directory that are more than 1GB then trim it.

I have these in my tasks

tasks:  
#using the shell command because we need \* expansion, otherwise if we know the exact directory we can use command module instead

- name: list log directory to find wasabi main directory name  
find:  
paths: [“/var/log/”]  
patterns: “wasabi-intuit-main\*”  
file\_type: directory  
register: out\_directories  
ignore\_errors: True

- name: list log files for wasabi intuit main  
find:  
paths: “{{item.path}}”  
patterns: “wasabi-intuit-main\*.log”  
file\_type: file  
register: out\_files  
with\_items: “{{ out\_directories.files }}”  
ignore\_errors: True

but it seems the returned out\_files variable is a dict. the key is another dict of the previous job and the value is a dict from find return values with added properties like “changed”,“examined”, and “msg”. now I am confused on how to iterate that object so I can filter the result.

I have tried “{{ out\_files.values().files }}” and “{{ out\_files.results.files }}” which does not seems to work

---

<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 15, 2016, 5:06pm UTC](https://forum.ansible.com/t/find-register-directory-seems-to-not-registered/23013/4 "2016-11-15T17:06:27Z")

</div>

> thanks for your explanation and I have made some progress but I am further  
> stopped by the next stage.  
> My problem is that I have a dynamic directory that generate log file in  
> certain directories with the following format  
> /var/log/app-YYYYMMDD-RANDOM\_HASH/log\_file\_name-YYYYMMDDMMSS-[audit,console,access,].log  
> and I am trying to find all the logs in that directory that are more than  
> 1GB then trim it.

Can't you just do this in one find?

- name: Find file \>=1GB  
&nbsp;&nbsp;&nbsp;find:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path: /var/log  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;patterns: "wasabi-intuit-main\*.log"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;file\_type: file  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;size: 1g  
&nbsp;&nbsp;&nbsp;register: files\_too\_large

> I have these in my tasks
> 
> &nbsp;&nbsp;tasks:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#using the shell command because we need \* expansion, otherwise if we  
> know the exact directory we can use command module instead  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- name: list log directory to find wasabi main directory name  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;find:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;paths: ["/var/log/"]  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;patterns: "wasabi-intuit-main\*"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;file\_type: directory  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: out\_directories  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ignore\_errors: True
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- name: list log files for wasabi intuit main  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;find:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;paths: "{{item.path}}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;patterns: "wasabi-intuit-main\*.log"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;file\_type: file  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: out\_files  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with\_items: "{{ out\_directories.files }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ignore\_errors: True
> 
> but it seems the returned out\_files variable is a dict. the key is another  
> dict of the previous job and the value is a dict from find return values  
> with added properties like "changed","examined", and "msg". now I am  
> confused on how to iterate that object so I can filter the result.
> 
> I have tried "{{ out\_files.values().files }}" and "{{  
> out\_files.results.files }}" which does not seems to work

This is going to be somewhat complicated, I do recommend looking at doing it with less loops.

out\_file.results.0.files will contain all files in the fist directory from out\_directories  
out\_file.results.1.files will contain all files in the second directory from out\_directories  
...

If you still want to iterate on out\_files you will have to look av with\_subelements.

Probably someting like this  
&nbsp;&nbsp;&nbsp;- debug: var=item.1.path  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with\_subelements:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- "{{ out\_file.results }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- files

---

<div class="post-metadata">

**Author:** ![Felix\_Gao](https://avatars.discourse-cdn.com/v4/letter/f/258eb7/32.png) [@Felix\_Gao](https://forum.ansible.com/u/Felix_Gao)\
**Post date:** [November 16, 2016, 1:08am UTC](https://forum.ansible.com/t/find-register-directory-seems-to-not-registered/23013/5 "2016-11-16T01:08:39Z")

</div>

Thanks for the reply, I ended using

- name: Find file \>=1GB  
find:  
path: /var/log  
patterns: “wasabi-intuit-main\*.log”  
file\_type: file  
size: 1g  
recursive: true  
register: files\_too\_large

because the file is in another level deep than /var/log and the nested loops seems to be too complicated. I would really love is find’s path can also take a regex instead of just the pattern on the file matching.
