Can't access registered variable

440 views
Skip to first unread message

Cann

unread,
Apr 24, 2017, 3:46:38 PM4/24/17
to Ansible Project
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.





Kai Stian Olstad

unread,
Apr 24, 2017, 4:03:02 PM4/24/17
to ansible...@googlegroups.com
On 24. april 2017 21:46, Cann wrote:
> 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
> 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

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
with_items: "{{ list_of_db_backups.files }}"

your code should work.

--
Kai Stian Olstad

Cann

unread,
Apr 24, 2017, 11:44:37 PM4/24/17
to Ansible Project, ansible-pr...@olstad.com
Thank you very much Kai Stian Olstad!
It was really useful.
Reply all
Reply to author
Forward
0 new messages