Daniel Andrei Mincă
Daniel Andrei Mincă

Reputation: 5042

Move folders older than 2 days via Ansible

Given the following directories:

    /tmp/testing/test_ansible     
    ├── [Sep 20  8:53]  2014-05-10
    ├── [Sep 20  8:53]  2014-05-11
    ├── [Sep 20  8:53]  2014-05-12
    └── [Sep 22  9:48]  2016-09-22

    4 directories                 

I'm trying to move dirs older than 2 days. In order to achieve that, I'm using Ansible's find module:

    - name: Find the test dirs created in the past
      find:
        paths: /tmp/testing/test_ansible
        age: 2d
        file_type: directory
      register: gold_data

    - debug: var="{{ item }}"
      with_items: "{{ gold_data.files }}"

The above code is outputting 3 results out of 4 folders, I'm showing only 1 result below:

    TASK [debian-linux-move : debug] ***********************************************                                                                               
    ok: [localhost] => (item={u'uid': 1000, u'woth': False, u'mtime': 1474350802.827127, u'inode': 3937540, u'isgid': False, u'size': 4096, u'roth': True, u'isuid'
    : False, u'isreg': False, u'gid': 1000, u'ischr': False, u'wusr': True, u'xoth': True, u'rusr': True, u'nlink': 2, u'issock': False, u'rgrp': True, u'path': u'
    /tmp/testing/test_ansible/2014-05-11', u'xusr': True, u'atime': 1474529596.5034406, u'isdir': True, u'ctime': 1474350802.827127, u'isblk': False, u'xgrp': True
    , u'dev': 2055, u'wgrp': True, u'isfifo': False, u'mode': u'0775', u'islnk': False}) => {                                                                      
        "<type 'dict'>": "VARIABLE IS NOT DEFINED!",                                                                                                               
        "item": {                                                                                                                                                  
            "atime": 1474529596.5034406,                                                                                                                           
            "ctime": 1474350802.827127,                                                                                                                            
            "dev": 2055,                                                                                                                                           
            "gid": 1000,                                                                                                                                           
            "inode": 3937540,                                                                                                                                      
            "isblk": false,                                                                                                                                        
            "ischr": false,                                                                                                                                        
            "isdir": true,                                                                                                                                         
            "isfifo": false,                                                                                                                                       
            "isgid": false,                                                                                                                                        
            "islnk": false,                                                                                                                                        
            "isreg": false,                                                                                                                                        
            "issock": false,                                                                                                                                       
            "isuid": false,                                                                                                                                        
            "mode": "0775",                                                                                                                                        
            "mtime": 1474350802.827127,                                                                                                                            
            "nlink": 2,                                                                                                                                            
            "path": "/tmp/testing/test_ansible/2014-05-11",                                                                                                        
            "rgrp": true,                                                                                                                                          
            "roth": true,                                                                                                                                          
            "rusr": true,                                                                                                                                          
            "size": 4096,                                                                                                                                          
            "uid": 1000,                                                                                                                                           
            "wgrp": true,                                                                                                                                          
            "woth": false,                                                                                                                                         
            "wusr": true,                                                                                                                                          
            "xgrp": true,                                                                                                                                          
            "xoth": true,                                                                                                                                          
            "xusr": true                                                                                                                                           
        }                                                                                                                                                          
    }                                                                                                                                                              

and 2 more results that are somewhat similar to this one.

What I'm trying to achieve

I figured that if I store all the path in a variable, then I could just move those dirs from the stored variable, and then make a symlink back to the dir where they were taken. So I have to loop the items and extract the path.

That's why I need the path. But when I try to access it, I get error:

    (debug) p list(vars['gold_data']['files']['path'])               
    ***TypeError:TypeError('list indices must be integers, not str',)

What are other options? How could I achieve such operation?

Upvotes: 0

Views: 974

Answers (1)

Daniel Andrei Mincă
Daniel Andrei Mincă

Reputation: 5042

Thanks to the awesome #ansible IRC community, manage to fix the error.

I was printing the item wrong in the debug module:

how I did it (bad):

    - debug: var={{ item['path'] }}
      with_items: "{{ gold_data.files }}"

how they suggested (good):

    - debug: var=item.path
      with_items: "{{ gold_data.files }}"

so, by removing the double braces it's now printing the path correctly:

    TASK [debian-linux-move : debug] ***********************************************                                                                               
    ok: [localhost] => (item={u'uid': 1000, u'woth': False, u'mtime': 1474350802.827127, u'inode': 3937540, u'isgid': False, u'size': 4096, u'roth': True, u'isuid'
    : False, u'isreg': False, u'gid': 1000, u'ischr': False, u'wusr': True, u'xoth': True, u'rusr': True, u'nlink': 2, u'issock': False, u'rgrp': True, u'path': u'
    /tmp/testing/test_ansible/2014-05-11', u'xusr': True, u'atime': 1474529596.5034406, u'isdir': True, u'ctime': 1474350802.827127, u'isblk': False, u'xgrp': True
    , u'dev': 2055, u'wgrp': True, u'isfifo': False, u'mode': u'0775', u'islnk': False}) => {                                                                      
        "item": {                                                                                                                                                  
            "atime": 1474529596.5034406,                                                                                                                           
            "ctime": 1474350802.827127,                                                                                                                            
            "dev": 2055,                                                                                                                                           
            "gid": 1000,                                                                                                                                           
            "inode": 3937540,                                                                                                                                      
            "isblk": false,                                                                                                                                        
            "ischr": false,                                                                                                                                        
            "isdir": true,                                                                                                                                         
            "isfifo": false,                                                                                                                                       
            "isgid": false,                                                                                                                                        
            "islnk": false,                                                                                                                                        
            "isreg": false,                                                                                                                                        
            "issock": false,                                                                                                                                       
            "isuid": false,                                                                                                                                        
            "mode": "0775",                                                                                                                                        
            "mtime": 1474350802.827127,                                                                                                                            
            "nlink": 2,                                                                                                                                            
            "path": "/tmp/testing/test_ansible/2014-05-11",                                                                                                        
            "rgrp": true,                                                                                                                                          
            "roth": true,                                                                                                                                          
            "rusr": true,                                                                                                                                          
            "size": 4096,                                                                                                                                          
            "uid": 1000,                                                                                                                                           
            "wgrp": true,                                                                                                                                          
            "woth": false,                                                                                                                                         
            "wusr": true,                                                                                                                                          
            "xgrp": true,                                                                                                                                          
            "xoth": true,                                                                                                                                          
            "xusr": true                                                                                                                                           
        },                                                                                                                                                         
        "item.path": "/tmp/testing/test_ansible/2014-05-11"                                                                                                     
    }                                                                                                                                                              

Upvotes: 1

Related Questions