获取Ansible Playbook中寄存器变量匹配条件的计数
我有一个示例剧本,其中我使用获取Ansible Playbook中寄存器变量匹配条件的计数,ansible,ansible-playbook,Ansible,Ansible Playbook,我有一个示例剧本,其中我使用和_nested从两个ansible变量匹配主机。这两个变量是json格式的。记录匹配正确。但由于ansible 2.x的变化,它在寄存器变量中同时显示匹配和不匹配的记录 --- - hosts: 127.0.0.1 connection: local tasks: - shell: "echo {{ item[0] }}" with_nested: - [{"host": "host1","descrip
和_nested
从两个ansible变量匹配主机。这两个变量是json格式的。记录匹配正确。但由于ansible 2.x的变化,它在寄存器变量中同时显示匹配和不匹配的记录
---
- 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.results
所有主机的输出
"all_hosts.results": [
{
"_ansible_no_log": false,
"changed": true,
"cmd": "echo {u'host': u'host1', u'description': u'This is host1 server'}",
"delta": "0:00:00.005456",
"end": "2016-04-07 02:34:37.151824",
"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-07 02:34:37.146368",
"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.005470",
"end": "2016-04-07 02:34:37.310095",
"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-07 02:34:37.304625",
"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
}
]
所以有两个匹配的记录。有什么方法可以得到这些匹配记录的数量吗
某物
- debug: msg="Task if matching record count more than one"
when: all_hosts.changed | length > 0
您真的需要计数还是要检查是否至少有一条匹配记录?那么:
- debug: msg="Task if matching record count more than one"
when: all_hosts.changed
我只想检查一个条件是否有任何匹配的记录。但是由于它的数组类型,所以所有的主机都改变了,我不工作。要基于匹配计数执行任何任务,我需要将
所有\u主机
与\u项所有\u主机
再次放入中,并且所有\u主机
是一个dict,并且有一个名为更改的键<代码>所有主机。结果
是一个数组。你试过了吗?在Ansible 1.9.2Ah中对我很好。。是的,你又一次救了我:)我很封闭。