我最近将ansible版本从2.0
升级到with_nested
。升级后ansible 1.7.2
循环无法仅在ansible变量中注册匹配的记录。这同样适用于---
- 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
但有没有办法从变量(ViewResult result = controller.TestAction() as ViewResult;
)本身中排除这些值?因为它不必要地增加迭代。
有什么建议吗?
答案 0 :(得分:1)
不幸的是,这是预期的行为。很多人抱怨这一点,但是避免第二次循环是不可能的。
请注意
如果任务失败或被跳过,则仍会注册该变量 失败或跳过状态,唯一的方法是避免注册 变量正在使用标签。