2016-04-05 32 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