2016-04-05 65 views
2

我最近將1.7的完整版本升級到2.0。升級後with_nested循環無法僅註冊可變變量中的匹配記錄。同樣是在ansible 1.7.2如何從可變變量過濾不匹配的值

在這裏工作是例如劇本:

--- 

- hosts: 127.0.0.1 
    connection: local 

    tasks: 

    - shell: "echo {{ item[0] }}" 
     with_nested: 
     - [{"host": "host1","description": "This is host1 server"}, {"host": "host2","description": "This is host2 server"}] 
     - [{"host_name": "host1"},{"host_name": "host2"},{"host_name": "host3"},{"host_name":"host4"}] 
     register: all_hosts 
     when: item[0].host == item[1].host_name 

    - debug: var=all_hosts 

輸出:

PLAY *************************************************************************** 

TASK [setup] ******************************************************************* 
ok: [127.0.0.1] 

TASK [command] ***************************************************************** 
changed: [127.0.0.1] => (item=[{u'host': u'host1', u'description': u'This is host1 server'}, {u'host_name': u'host1'}]) 
skipping: [127.0.0.1] => (item=[{u'host': u'host1', u'description': u'This is host1 server'}, {u'host_name': u'host2'}]) 
skipping: [127.0.0.1] => (item=[{u'host': u'host1', u'description': u'This is host1 server'}, {u'host_name': u'host3'}]) 
skipping: [127.0.0.1] => (item=[{u'host': u'host1', u'description': u'This is host1 server'}, {u'host_name': u'host4'}]) 
skipping: [127.0.0.1] => (item=[{u'host': u'host2', u'description': u'This is host2 server'}, {u'host_name': u'host1'}]) 
changed: [127.0.0.1] => (item=[{u'host': u'host2', u'description': u'This is host2 server'}, {u'host_name': u'host2'}]) 
skipping: [127.0.0.1] => (item=[{u'host': u'host2', u'description': u'This is host2 server'}, {u'host_name': u'host3'}]) 
skipping: [127.0.0.1] => (item=[{u'host': u'host2', u'description': u'This is host2 server'}, {u'host_name': u'host4'}]) 

TASK [debug] ******************************************************************* 
ok: [127.0.0.1] => { 
    "all_hosts": { 
     "changed": true, 
     "msg": "All items completed", 
     "results": [ 
      { 
       "_ansible_no_log": false, 
       "changed": true, 
       "cmd": "echo {u'host': u'host1', u'description': u'This is host1 server'}", 
       "delta": "0:00:00.005613", 
       "end": "2016-04-05 04:09:05.269437", 
       "invocation": { 
        "module_args": { 
         "_raw_params": "echo {u'host': u'host1', u'description': u'This is host1 server'}", 
         "_uses_shell": true, 
         "chdir": null, 
         "creates": null, 
         "executable": null, 
         "removes": null, 
         "warn": true 
        }, 
        "module_name": "command" 
       }, 
       "item": [ 
        { 
         "description": "This is host1 server", 
         "host": "host1" 
        }, 
        { 
         "host_name": "host1" 
        } 
       ], 
       "rc": 0, 
       "start": "2016-04-05 04:09:05.263824", 
       "stderr": "", 
       "stdout": "{uhost: uhost1, udescription: uThis is host1 server}", 
       "stdout_lines": [ 
        "{uhost: uhost1, udescription: uThis is host1 server}" 
       ], 
       "warnings": [] 
      }, 
      { 
       "_ansible_no_log": false, 
       "changed": false, 
       "item": [ 
        { 
         "description": "This is host1 server", 
         "host": "host1" 
        }, 
        { 
         "host_name": "host2" 
        } 
       ], 
       "skip_reason": "Conditional check failed", 
       "skipped": true 
      }, 
      { 
       "_ansible_no_log": false, 
       "changed": false, 
       "item": [ 
        { 
         "description": "This is host1 server", 
         "host": "host1" 
        }, 
        { 
         "host_name": "host3" 
        } 
       ], 
       "skip_reason": "Conditional check failed", 
       "skipped": true 
      }, 
      { 
       "_ansible_no_log": false, 
       "changed": false, 
       "item": [ 
        { 
         "description": "This is host1 server", 
         "host": "host1" 
        }, 
        { 
         "host_name": "host4" 
        } 
       ], 
       "skip_reason": "Conditional check failed", 
       "skipped": true 
      }, 
      { 
       "_ansible_no_log": false, 
       "changed": false, 
       "item": [ 
        { 
         "description": "This is host2 server", 
         "host": "host2" 
        }, 
        { 
         "host_name": "host1" 
        } 
       ], 
       "skip_reason": "Conditional check failed", 
       "skipped": true 
      }, 
      { 
       "_ansible_no_log": false, 
       "changed": true, 
       "cmd": "echo {u'host': u'host2', u'description': u'This is host2 server'}", 
       "delta": "0:00:00.005463", 
       "end": "2016-04-05 04:09:05.425793", 
       "invocation": { 
        "module_args": { 
         "_raw_params": "echo {u'host': u'host2', u'description': u'This is host2 server'}", 
         "_uses_shell": true, 
         "chdir": null, 
         "creates": null, 
         "executable": null, 
         "removes": null, 
         "warn": true 
        }, 
        "module_name": "command" 
       }, 
       "item": [ 
        { 
         "description": "This is host2 server", 
         "host": "host2" 
        }, 
        { 
         "host_name": "host2" 
        } 
       ], 
       "rc": 0, 
       "start": "2016-04-05 04:09:05.420330", 
       "stderr": "", 
       "stdout": "{uhost: uhost2, udescription: uThis is host2 server}", 
       "stdout_lines": [ 
        "{uhost: uhost2, udescription: uThis is host2 server}" 
       ], 
       "warnings": [] 
      }, 
      { 
       "_ansible_no_log": false, 
       "changed": false, 
       "item": [ 
        { 
         "description": "This is host2 server", 
         "host": "host2" 
        }, 
        { 
         "host_name": "host3" 
        } 
       ], 
       "skip_reason": "Conditional check failed", 
       "skipped": true 
      }, 
      { 
       "_ansible_no_log": false, 
       "changed": false, 
       "item": [ 
        { 
         "description": "This is host2 server", 
         "host": "host2" 
        }, 
        { 
         "host_name": "host4" 
        } 
       ], 
       "skip_reason": "Conditional check failed", 
       "skipped": true 
      } 
     ] 
    } 
} 

PLAY RECAP ********************************************************************* 
127.0.0.1     : ok=3 changed=1 unreachable=0 failed=0 

在我all_hosts它也給了我無與倫比的與跳躍消息"skip_reason": "Conditional check failed"記錄。我不想在all_hosts變量中包含不匹配的記錄。

預期輸出:

TASK [debug] ******************************************************************* 
ok: [127.0.0.1] => { 
    "all_hosts": { 
     "changed": true, 
     "msg": "All items completed", 
     "results": [ 
      { 
       "_ansible_no_log": false, 
       "changed": true, 
       "cmd": "echo {u'host': u'host1', u'description': u'This is host1 server'}", 
       "delta": "0:00:00.005613", 
       "end": "2016-04-05 04:09:05.269437", 
       "invocation": { 
        "module_args": { 
         "_raw_params": "echo {u'host': u'host1', u'description': u'This is host1 server'}", 
         "_uses_shell": true, 
         "chdir": null, 
         "creates": null, 
         "executable": null, 
         "removes": null, 
         "warn": true 
        }, 
        "module_name": "command" 
       }, 
       "item": [ 
        { 
         "description": "This is host1 server", 
         "host": "host1" 
        }, 
        { 
         "host_name": "host1" 
        } 
       ], 
       "rc": 0, 
       "start": "2016-04-05 04:09:05.263824", 
       "stderr": "", 
       "stdout": "{uhost: uhost1, udescription: uThis is host1 server}", 
       "stdout_lines": [ 
        "{uhost: uhost1, udescription: uThis is host1 server}" 
       ], 
       "warnings": [] 
      }, 
      { 
       "_ansible_no_log": false, 
       "changed": true, 
       "cmd": "echo {u'host': u'host2', u'description': u'This is host2 server'}", 
       "delta": "0:00:00.005463", 
       "end": "2016-04-05 04:09:05.425793", 
       "invocation": { 
        "module_args": { 
         "_raw_params": "echo {u'host': u'host2', u'description': u'This is host2 server'}", 
         "_uses_shell": true, 
         "chdir": null, 
         "creates": null, 
         "executable": null, 
         "removes": null, 
         "warn": true 
        }, 
        "module_name": "command" 
       }, 
       "item": [ 
        { 
         "description": "This is host2 server", 
         "host": "host2" 
        }, 
        { 
         "host_name": "host2" 
        } 
       ], 
       "rc": 0, 
       "start": "2016-04-05 04:09:05.420330", 
       "stderr": "", 
       "stdout": "{uhost: uhost2, udescription: uThis is host2 server}", 
       "stdout_lines": [ 
        "{uhost: uhost2, udescription: uThis is host2 server}" 
       ], 
       "warnings": [] 
      } 
     ] 
    } 
} 

PLAY RECAP ********************************************************************* 
127.0.0.1     : ok=3 changed=1 unreachable=0 failed=0 

我能夠過濾使用item|changed值象下面

- debug: var={{ item }} 
    with_items: all_hosts.results 
    when: item|changed 

但是有什麼辦法排除變量(all_hosts)這些價值本身?因爲它不必要的增加迭代。

有什麼建議嗎?

回答

1

不幸的是,這是預期的行爲。很多人都抱怨這一點,但避免第二個循環是不可能的。

Registered Variables

如果任務失敗或跳過,變量仍註冊 以失敗或跳過狀態,以避免註冊 變量使用標籤的唯一途徑。

+0

是的,我在官方文檔中看到。感謝您的回覆。 – Roopendra