通过Ansible移动超过2天的文件夹

时间:2016-09-22 08:07:21

标签: python bash directory ansible ansible-playbook

给出以下目录:

    /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                 

我正在尝试移动 dirs早于 2天。为了 实现这一点,我正在使用Ansible的find模块:

    - 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 }}"

以上代码在4个文件夹中输出 3个结果,我只显示以下1个结果:

    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                                                                                                                                           
        }                                                                                                                                                          
    }                                                                                                                                                              

还有2个与此类似的结果。

我想要实现的目标

我想如果我将所有路径存储在变量中, 然后我可以从存储的变量移动那些dirs, 然后制作一个符号链接返回到他们被拍摄的目录。所以我必须要 循环项目并提取path

这就是我需要路径的原因。但是当我尝试访问它时,我收到错误:

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

还有什么其他选择?我怎么能实现这样的操作?

1 个答案:

答案 0 :(得分:1)

感谢令人敬畏的#ansible IRC社区,设法解决问题 错误。

我在调试模块中打错了项目:

我是怎么做的(不好):

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

他们的建议(好):

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

所以,通过删除双括号,它现在正确打印路径:

    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"                                                                                                     
    }