2017-07-24 54 views
0

我想在2.3.x中遍歷列表(我有一堆項目返回列表上下文)。我想根據一個寄存器變量的結果採取行動,但是我不能在使用我的with_items時使用sem來訪問基本元素。我已經嘗試了以下代碼中的映射,但是無法訪問註冊結果中的屬性「路徑」(這是通過with_items子迭代進行處理的)。無法訪問帶有with_items子循環的列表的結果ansible

#!/bin/ansible-playbook 
--- 
- name: Check state of repos and prevent updates 
    hosts: localhost 
    connection: local 
    vars: 
    basecomponents: 
     - cbr 
     - slc 
     - gts 
     - vep 
     - mcp 
     - lse 

    tasks: 

    - name: Create and enable repo if necessary 
    yum_repository: 
     name: MyRepo 
     description: My Repo 
     baseurl: http://127.0.0.1/repo/head/myrepo/x86_64/ 
     enabled: yes 
     gpgcheck: yes 
     mirrorlist_expire: 60 
     state: present 
    - name: remove update from last version 
    find: 
     paths: "/home/{{item}}/update/" 
     patterns: "currentVersion.*" 
    with_items: "{{basecomponents}}" 
    register: findresult 

    - name: remove current version 
    file: 
     path: "{{item.path}}" 
     state: absent 
    with_items: findresult.results. 

    - name: Debug result 
    debug: 
     var: "{{item.files}}" 
    with_items: 
     - findresult.results 
     # | map(attribute='path')| list}} 

下面是結果,當我運行的東西:

ok: [localhost] => (item=findresult.results) => { 
    "findresult.results": [ 
     { 
      "_ansible_item_result": true, 
      "_ansible_no_log": false, 
      "_ansible_parsed": true, 
      "changed": false, 
      "examined": 4, 
      "files": [ 
       { 
        "atime": 1500919313.0, 
        "ctime": 1500919690.9134178, 
        "dev": 64774, 
        "gid": 1002, 
        "inode": 94371970, 
        "isblk": false, 
        "ischr": false, 
        "isdir": false, 
        "isfifo": false, 
        "isgid": false, 
        "islnk": false, 
        "isreg": true, 
        "issock": false, 
        "isuid": false, 
        "mode": "0664", 
        "mtime": 1500919313.0, 
        "nlink": 1, 
        "path": "/home/cbr/update/currentVersion.1500918030", 
        "rgrp": true, 
        "roth": true, 
        "rusr": true, 
        "size": 0, 
        "uid": 1001, 
        "wgrp": true, 
        "woth": false, 
        "wusr": true, 
        "xgrp": false, 
        "xoth": false, 
        "xusr": false 
       }, 
       { 
        "atime": 1500919313.0, 
        "ctime": 1500919690.9134178, 
        "dev": 64774, 
        "gid": 1002, 
        "inode": 94371969, 
        "isblk": false, 
        "ischr": false, 
        "isdir": false, 
        "isfifo": false, 
        "isgid": false, 
        "islnk": false, 
        "isreg": true, 
        "issock": false, 
        "isuid": false, 
        "mode": "0664", 
        "mtime": 1500919313.0, 
        "nlink": 1, 
        "path": "/home/cbr/update/currentVersion.1500919311", 
        "rgrp": true, 
        "roth": true, 
        "rusr": true, 
        "size": 0, 
        "uid": 1001, 
        "wgrp": true, 
        "woth": false, 
        "wusr": true, 
        "xgrp": false, 
        "xoth": false, 
        "xusr": false 
       } 
      ], 
      "invocation": { 
       "module_args": { 
        "age": null, 
        "age_stamp": "mtime", 
        "contains": null, 
        "file_type": "file", 
        "follow": false, 
        "get_checksum": false, 
        "hidden": false, 
        "paths": [ 
         "/home/cbr/update/" 
        ], 
        "patterns": [ 
         "currentVersion.*" 
        ], 
        "recurse": false, 
        "size": null, 
        "use_regex": false 
       } 
      }, 
      "item": "cbr", 
      "matched": 2, 
      "msg": "" 
     } 

回答

0

你的最後一個任務是非常接近的。你只需要翻轉路徑和文件。

- name: Debug result 
    debug: 
    var: "{{item.path}}" 
    with_items: "{{ findresult.results | map(attribute='files') | list}} 

另外,您可以使用with_subelements

- name: Debug result 
    debug: 
    msg: "{{ item.1.path }}" 
    with_subelements: 
    - "{{ findresult.results }}" 
    - files