Move folders older than 2 days via Ansible
<p>Given the following directories:</p>

<pre><code>    /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                 
</code></pre>

<p>I'm trying to <strong>move</strong> dirs older than <strong>2 days</strong>. In order to
achieve that, I'm using Ansible's <code>find</code> module:</p>

<pre><code>    - 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 }}"
</code></pre>

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

<pre class="lang-sh prettyprint-override"><code>    TASK [debian-linux-move : debug] ***********************************************                                                                               
    ok: [localhost] =&gt; (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}) =&gt; {                                                                      
        "&lt;type 'dict'&gt;": "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                                                                                                                                           
        }                                                                                                                                                          
    }                                                                                                                                                              
</code></pre>

<p>and 2 more results that are somewhat similar to this one.</p>

<h3>What I'm trying to achieve</h3>

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

<p>That's why I need the <strong>path</strong>. But when I try to access it, I get error:</p>

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

<p>What are other options? How could I achieve such operation?</p>

<p>Thanks to the awesome #ansible IRC community, manage to fix the
error.</p>

<p>I was printing the item wrong in the debug module:</p>

<p>how I did it (bad):</p>

<pre><code>    - debug: var={{ item['path'] }}
      with_items: "{{ gold_data.files }}"
</code></pre>

<p>how they suggested (good):</p>

<pre><code>    - debug: var=item.path
      with_items: "{{ gold_data.files }}"
</code></pre>

<p>so, by removing the double braces it's now printing the path correctly:</p>

<pre class="lang-sh prettyprint-override"><code>    TASK [debian-linux-move : debug] ***********************************************                                                                               
    ok: [localhost] =&gt; (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}) =&gt; {                                                                      
        "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"                                                                                                     
    }                                                                                                                                                              
</code></pre>

python bash directory ansible ansible-playbook