# Can't access registered variable

**URL:** <https://forum.ansible.com/t/cant-access-registered-variable/24299>\
**Category:** Ansible Project\
**Created:** [April 24, 2017, 7:46pm UTC](https://forum.ansible.com/t/cant-access-registered-variable/24299 "2017-04-24T19:46:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cann](https://avatars.discourse-cdn.com/v4/letter/c/2bfe46/32.png) [@Cann](https://forum.ansible.com/u/Cann)\
**Post date:** [April 24, 2017, 7:46pm UTC](https://forum.ansible.com/t/cant-access-registered-variable/24299/1 "2017-04-24T19:46:38Z")

</div>

Hello Everyone

I have been using Ansible for about a year now and I can’t wrap my head around on idea of “registered variables”  
I bought books, read online docs and looked at examples… but I still can’t use this important future properly.

Let me give you a example:

I want to find files that are older than 7 days:

`

- name: Find backups that are older than 7 days  
find: paths=“/db-backups” age=“7d”  
register: list\_of\_db\_backups

`

and I want to delete the files that I have found:

`

- name: Cleanup backups that are older than 7 days  
file:  
name: “{{ item.path }}”  
state: absent  
with\_items: list\_of\_db\_backups.files

`

and I get error right away saying that “the field ‘args’ has an invalid value, which appears to include a variable that is undefined. The error was: ‘ansible.vars.unsafe\_proxy.AnsibleUnsafeText object’ has no attribute ‘path’”

To be able to understand why ansible says _object has no attribute “path”_, I add a debug line to my playbook:

`

- debug: var=list\_of\_db\_backups

`

and I get this result:

`

TASK [ServerSetup/dev-mysql : debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [db] =\> {  
“changed”: false,  
“list\_of\_db\_backups”: {  
“changed”: false,  
“examined”: 9,  
“files”: [  
{  
“atime”: 1493059112.0,  
“ctime”: 1493059112.0,  
“dev”: 28,  
“gid”: 1000,  
“inode”: 136,  
“isblk”: false,  
“ischr”: false,  
“isdir”: false,  
“isfifo”: false,  
“isgid”: false,  
“islnk”: false,  
“isreg”: true,  
“issock”: false,  
“isuid”: false,  
“mode”: “0644”,  
“mtime”: 1493059112.0,  
“nlink”: 1,  
“path”: “/db-backups/dev-2017-04-24T18:38:30Z.sql”,  
“rgrp”: true,  
“roth”: true,  
“rusr”: true,  
“size”: 397954,  
“uid”: 1000,  
“wgrp”: false,  
“woth”: false,  
“wusr”: true,  
“xgrp”: false,  
“xoth”: false,  
“xusr”: false  
}  
],  
“matched”: 1,  
“msg”: “”  
}  
}

`

so clearly there is a variable named “path”

What am I doing wrong here? Can someone please clarify?  
This is really important for me to take my ansible skills to the next level.

thank you kindly.

---

<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:** [April 24, 2017, 8:02pm UTC](https://forum.ansible.com/t/cant-access-registered-variable/24299/2 "2017-04-24T20:02:38Z")

</div>

> Hello Everyone
> 
> I have been using Ansible for about a year now and I can't wrap my head  
> around on idea of "registered variables"  
> I bought books, read online docs and looked at examples... but I still  
> can't use this important future properly.

You are almost there.

> Let me give you a example:
> 
> I want to find files that are older than 7 days:
> 
> - name: Find backups that are older than 7 days  
> &nbsp;&nbsp;find: paths="/db-backups" age="7d"  
> &nbsp;&nbsp;register: list\_of\_db\_backups
> 
> and I want to delete the files that I have found:
> 
> - name: Cleanup backups that are older than 7 days  
> &nbsp;&nbsp;file:  
> &nbsp;&nbsp;&nbsp;&nbsp;name: "{{ item.path }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;state: absent  
> &nbsp;&nbsp;with\_items: list\_of\_db\_backups.files

Writing variable without {{ }} in with\_items was allowed in older version of Ansible, in newer version {{ }} is required.  
If you leave them out it's treated literally as the string "list\_of\_db\_backups.files".

So with  
&nbsp;&nbsp;&nbsp;with\_items: "{{ list\_of\_db\_backups.files }}"

your code should work.

---

<div class="post-metadata">

**Author:** ![Cann](https://avatars.discourse-cdn.com/v4/letter/c/2bfe46/32.png) [@Cann](https://forum.ansible.com/u/Cann)\
**Post date:** [April 25, 2017, 3:44am UTC](https://forum.ansible.com/t/cant-access-registered-variable/24299/3 "2017-04-25T03:44:37Z")

</div>

Thank you very much Kai Stian Olstad!  
It was really useful.
